|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Laravel\Passport\Bridge; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\Date; |
| 6 | +use Laravel\Passport\DeviceCode as DeviceCodeModel; |
| 7 | +use Laravel\Passport\Passport; |
| 8 | +use League\OAuth2\Server\Entities\DeviceCodeEntityInterface; |
| 9 | +use League\OAuth2\Server\Repositories\DeviceCodeRepositoryInterface; |
| 10 | + |
| 11 | +class DeviceCodeRepository implements DeviceCodeRepositoryInterface |
| 12 | +{ |
| 13 | + /** |
| 14 | + * {@inheritdoc} |
| 15 | + */ |
| 16 | + public function getNewDeviceCode(): DeviceCodeEntityInterface |
| 17 | + { |
| 18 | + return new DeviceCode; |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * {@inheritdoc} |
| 23 | + */ |
| 24 | + public function persistDeviceCode(DeviceCodeEntityInterface $deviceCodeEntity): void |
| 25 | + { |
| 26 | + if (! is_null($deviceCodeEntity->getUserIdentifier())) { |
| 27 | + Passport::deviceCode()->newQuery()->whereKey($deviceCodeEntity->getIdentifier())->update([ |
| 28 | + 'user_id' => $deviceCodeEntity->getUserIdentifier(), |
| 29 | + 'user_approved_at' => $deviceCodeEntity->getUserApproved() ? Date::now() : null, |
| 30 | + ]); |
| 31 | + } elseif (! is_null($deviceCodeEntity->getLastPolledAt())) { |
| 32 | + Passport::deviceCode()->newQuery()->whereKey($deviceCodeEntity->getIdentifier())->update([ |
| 33 | + 'last_polled_at' => $deviceCodeEntity->getLastPolledAt(), |
| 34 | + ]); |
| 35 | + } else { |
| 36 | + Passport::deviceCode()->forceFill([ |
| 37 | + 'id' => $deviceCodeEntity->getIdentifier(), |
| 38 | + 'user_id' => null, |
| 39 | + 'client_id' => $deviceCodeEntity->getClient()->getIdentifier(), |
| 40 | + 'user_code' => $deviceCodeEntity->getUserCode(), |
| 41 | + 'scopes' => $deviceCodeEntity->getScopes(), |
| 42 | + 'revoked' => false, |
| 43 | + 'user_approved_at' => null, |
| 44 | + 'last_polled_at' => null, |
| 45 | + 'expires_at' => $deviceCodeEntity->getExpiryDateTime(), |
| 46 | + ])->save(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * {@inheritdoc} |
| 52 | + */ |
| 53 | + public function getDeviceCodeEntityByDeviceCode(string $deviceCode): ?DeviceCodeEntityInterface |
| 54 | + { |
| 55 | + $record = Passport::deviceCode()->newQuery()->whereKey($deviceCode)->where(['revoked' => false])->first(); |
| 56 | + |
| 57 | + return $record ? $this->fromDeviceCodeModel($record) : null; |
| 58 | + } |
| 59 | + |
| 60 | + /* |
| 61 | + * Get the device code entity by the given user code. |
| 62 | + */ |
| 63 | + public function getDeviceCodeEntityByUserCode(string $userCode): ?DeviceCodeEntityInterface |
| 64 | + { |
| 65 | + $record = Passport::deviceCode()->newQuery() |
| 66 | + ->where('user_code', $userCode) |
| 67 | + ->whereNull('user_id') |
| 68 | + ->where('expires_at', '>', Date::now()) |
| 69 | + ->where('revoked', false) |
| 70 | + ->first(); |
| 71 | + |
| 72 | + return $record ? $this->fromDeviceCodeModel($record) : null; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * {@inheritdoc} |
| 77 | + */ |
| 78 | + public function revokeDeviceCode(string $codeId): void |
| 79 | + { |
| 80 | + Passport::deviceCode()->newQuery()->whereKey($codeId)->update(['revoked' => true]); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * {@inheritdoc} |
| 85 | + */ |
| 86 | + public function isDeviceCodeRevoked(string $codeId): bool |
| 87 | + { |
| 88 | + return Passport::deviceCode()->newQuery()->whereKey($codeId)->where('revoked', false)->doesntExist(); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Create a new device code entity from the given device code model instance. |
| 93 | + */ |
| 94 | + protected function fromDeviceCodeModel(DeviceCodeModel $model): DeviceCodeEntityInterface |
| 95 | + { |
| 96 | + return new DeviceCode( |
| 97 | + $model->getKey(), |
| 98 | + $model->user_id, |
| 99 | + $model->client_id, |
| 100 | + $model->scopes, |
| 101 | + ! is_null($model->user_approved_at), |
| 102 | + $model->last_polled_at?->toDateTimeImmutable(), |
| 103 | + $model->expires_at?->toDateTimeImmutable() |
| 104 | + ); |
| 105 | + } |
| 106 | +} |
0 commit comments