1 | <?php |
||
14 | class AccessTokenRepository implements AccessTokenRepositoryInterface |
||
15 | { |
||
16 | /** |
||
17 | * The event dispatcher instance. |
||
18 | * |
||
19 | * @var \Illuminate\Contracts\Events\Dispatcher |
||
20 | */ |
||
21 | protected $events; |
||
22 | |||
23 | /** |
||
24 | * Create a new repository instance. |
||
25 | * |
||
26 | * @param \Illuminate\Contracts\Events\Dispatcher $events |
||
27 | * |
||
28 | * @return void |
||
29 | */ |
||
30 | public function __construct(Dispatcher $events) |
||
31 | { |
||
32 | $this->events = $events; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * {@inheritdoc} |
||
37 | */ |
||
38 | public function getNewToken(ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null) |
||
39 | { |
||
40 | return new AccessToken($userIdentifier, $scopes, $clientEntity); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity) |
||
47 | { |
||
48 | $clientId = $accessTokenEntity->getClient()->getIdentifier(); |
||
49 | [$userType, $userId] = explode(':', $accessTokenEntity->getUserIdentifier()); |
||
50 | |||
51 | $userId = method_exists($user = app('cortex.auth.'.$userType), 'unhashId') ? $user->unhashId($userId) : $userId; |
||
52 | $clientId = method_exists($client = app('rinvex.oauth.client'), 'unhashId') ? $client->unhashId($clientId) : $clientId; |
||
53 | $scopes = array_map(fn ($item) => app('cortex.auth.ability')->unhashId($item->getIdentifier()), $accessTokenEntity->getScopes()); |
||
|
|||
54 | |||
55 | app('rinvex.oauth.access_token')->create([ |
||
56 | 'identifier' => $accessTokenEntity->getIdentifier(), |
||
57 | 'user_id' => $userId, |
||
58 | 'user_type' => $userType, |
||
59 | 'client_id' => $clientId, |
||
60 | 'abilities' => $scopes, |
||
61 | 'is_revoked' => false, |
||
62 | 'created_at' => new DateTime(), |
||
63 | 'updated_at' => new DateTime(), |
||
64 | 'expires_at' => $accessTokenEntity->getExpiryDateTime(), |
||
65 | ]); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Revoke an access token. |
||
70 | * |
||
71 | * @param string $tokenId |
||
72 | * |
||
73 | * @return mixed |
||
74 | */ |
||
75 | public function revokeAccessToken($tokenId) |
||
76 | { |
||
77 | app('rinvex.oauth.access_token')->where('identifier', $tokenId)->update(['is_revoked' => true]); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function isAccessTokenRevoked($tokenId) |
||
84 | { |
||
85 | if ($accessToken = app('rinvex.oauth.access_token')->where('identifier', $tokenId)->first()) { |
||
86 | return $accessToken->is_revoked; |
||
87 | } |
||
88 | |||
92 |