1 | <?php |
||
2 | |||
3 | namespace ByTIC\Hello\Models\AccessTokens; |
||
4 | |||
5 | use ByTIC\Hello\Models\Clients\Client; |
||
6 | use ByTIC\Hello\Models\Clients\Clients; |
||
7 | use ByTIC\Hello\Models\Users\Traits\UserTrait; |
||
8 | use Carbon\Carbon; |
||
9 | use League\OAuth2\Server\Entities\AccessTokenEntityInterface; |
||
10 | use League\OAuth2\Server\Entities\ClientEntityInterface; |
||
11 | use League\OAuth2\Server\Entities\ScopeEntityInterface; |
||
12 | use League\OAuth2\Server\Entities\TokenInterface; |
||
13 | use League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException; |
||
14 | use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; |
||
15 | use Nip\Records\Collections\Collection; |
||
16 | |||
17 | /** |
||
18 | * Class Tokens |
||
19 | * @package ByTIC\Auth\Models\Tokens |
||
20 | * |
||
21 | * @method Token getNew() |
||
22 | */ |
||
23 | class Tokens extends \Nip\Records\RecordManager implements AccessTokenRepositoryInterface |
||
24 | { |
||
25 | /** |
||
26 | * Create a new access token |
||
27 | * |
||
28 | * @param ClientEntityInterface $clientEntity |
||
29 | * @param ScopeEntityInterface[] $scopes |
||
30 | * @param mixed $userIdentifier |
||
31 | * |
||
32 | * @return AccessTokenEntityInterface|Token |
||
33 | */ |
||
34 | 1 | public function getNewToken(ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null) |
|
35 | { |
||
36 | 1 | $token = $this->getNew(); |
|
37 | 1 | $token->populateFromClient($clientEntity); |
|
38 | 1 | $token->setUserIdentifier($userIdentifier); |
|
39 | 1 | $token->addScopes($scopes); |
|
40 | 1 | return $token; |
|
41 | } |
||
42 | |||
43 | /** |
||
44 | * Persists a new access token to permanent storage. |
||
45 | * |
||
46 | * @param AccessTokenEntityInterface|Token $accessTokenEntity |
||
47 | * |
||
48 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
49 | */ |
||
50 | public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity) |
||
51 | { |
||
52 | $accessTokenEntity->save(); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Revoke an access token. |
||
57 | * |
||
58 | * @param string $tokenId |
||
59 | */ |
||
60 | public function revokeAccessToken($tokenId) |
||
61 | { |
||
62 | $token = $this->getByIdentifier($tokenId); |
||
63 | if (!($token instanceof Token)) { |
||
64 | return; |
||
65 | } |
||
66 | $token->revoked = true; |
||
0 ignored issues
–
show
|
|||
67 | $token->save(); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Check if the access token has been revoked. |
||
72 | * |
||
73 | * @param string $tokenId |
||
74 | * |
||
75 | * @return bool Return true if this token has been revoked |
||
76 | */ |
||
77 | public function isAccessTokenRevoked($tokenId) |
||
78 | { |
||
79 | $token = $this->getByIdentifier($tokenId); |
||
80 | if ($token instanceof TokenInterface) { |
||
81 | return $token->revoked; |
||
0 ignored issues
–
show
|
|||
82 | } |
||
83 | return true; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param UserTrait $user |
||
88 | * @param Client $client |
||
89 | * @return Collection|Token[] |
||
90 | */ |
||
91 | 1 | public function getValidUserTokens($user, $client) |
|
92 | { |
||
93 | 1 | $params = []; |
|
94 | 1 | $params['where'] = [ |
|
95 | 1 | ['user_id = ?', $user->getIdentifier()], |
|
96 | 1 | ['client_id = ?', $client->getIdentifier()], |
|
97 | ['revoked = ?', 0], |
||
98 | 1 | ['expires_at > ?', Carbon::now()->toDateString()], |
|
99 | ]; |
||
100 | 1 | return $this->findByParams($params); |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * @param $tokenId |
||
105 | * @return Token|null |
||
106 | */ |
||
107 | public function getByIdentifier($tokenId) |
||
108 | { |
||
109 | $collection = $this->findByField('identifier', $tokenId); |
||
110 | return $collection->current(); |
||
111 | } |
||
112 | |||
113 | 2 | public function getQueryModelData($model) |
|
114 | { |
||
115 | 2 | $data = parent::getQueryModelData($model); |
|
116 | 2 | if (isset($data['scopes']) && is_array($data['scopes'])) { |
|
117 | 1 | $data['scopes'] = implode(',', $data['scopes']); |
|
118 | } |
||
119 | 2 | return $data; |
|
120 | } |
||
121 | |||
122 | 1 | protected function initRelations() |
|
123 | { |
||
124 | 1 | parent::initRelations(); |
|
125 | 1 | $this->belongsTo('Client', ['class' => Clients::class, 'fk' => 'client_id', 'withPK' => 'identifier']); |
|
126 | 1 | } |
|
127 | |||
128 | /** @noinspection PhpMissingParentCallCommonInspection |
||
129 | * @inheritDoc |
||
130 | */ |
||
131 | protected function generateTable() |
||
132 | { |
||
133 | return 'oauth_access_tokens'; |
||
134 | } |
||
135 | } |
||
136 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.