-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathHasAccessTokens.php
More file actions
222 lines (187 loc) · 6.12 KB
/
Copy pathHasAccessTokens.php
File metadata and controls
222 lines (187 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter Shield.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Shield\Authentication\Traits;
use CodeIgniter\I18n\Time;
use CodeIgniter\Shield\Authentication\Authenticators\AccessTokens;
use CodeIgniter\Shield\Entities\AccessToken;
use CodeIgniter\Shield\Models\UserIdentityModel;
use InvalidArgumentException;
/**
* Trait HasAccessTokens
*
* Provides functionality needed to generate, revoke,
* and retrieve Personal Access Tokens.
*
* Intended to be used with User entities.
*/
trait HasAccessTokens
{
/**
* The current access token for the user.
*/
private ?AccessToken $currentAccessToken = null;
/**
* Generates a new personal access token for this user.
*
* @param string $name Token name
* @param list<string> $scopes Permissions the token grants
* @param Time|null $expiresAt Expiration date
*
* @throws InvalidArgumentException
*/
public function generateAccessToken(string $name, array $scopes = ['*'], ?Time $expiresAt = null): AccessToken
{
$identityModel = model(UserIdentityModel::class);
return $identityModel->generateAccessToken($this, $name, $scopes, $expiresAt);
}
/**
* Delete any access tokens for the given raw token.
*/
public function revokeAccessToken(string $rawToken): void
{
$identityModel = model(UserIdentityModel::class);
$identityModel->revokeAccessToken($this, $rawToken);
}
/**
* Delete any access tokens for the given secret token.
*/
public function revokeAccessTokenBySecret(string $secretToken): void
{
$identityModel = model(UserIdentityModel::class);
$identityModel->revokeAccessTokenBySecret($this, $secretToken);
}
/**
* Revokes all access tokens for this user.
*/
public function revokeAllAccessTokens(): void
{
$identityModel = model(UserIdentityModel::class);
$identityModel->revokeAllAccessTokens($this);
}
/**
* Retrieves all personal access tokens for this user.
*
* @return list<AccessToken>
*/
public function accessTokens(): array
{
$identityModel = model(UserIdentityModel::class);
return $identityModel->getAllAccessTokens($this);
}
/**
* Given a raw token, will hash it and attempt to
* locate it within the system.
*/
public function getAccessToken(?string $rawToken): ?AccessToken
{
if ($rawToken === null || $rawToken === '') {
return null;
}
$identityModel = model(UserIdentityModel::class);
return $identityModel->getAccessToken($this, $rawToken);
}
/**
* Given the ID, returns the given access token.
*/
public function getAccessTokenById(int $id): ?AccessToken
{
$identityModel = model(UserIdentityModel::class);
return $identityModel->getAccessTokenById($id, $this);
}
/**
* Determines whether the user's token grants permissions to $scope.
* First checks against $this->activeToken, which is set during
* authentication. If it hasn't been set, returns false.
*/
public function tokenCan(string $scope): bool
{
if (! $this->currentAccessToken() instanceof AccessToken) {
return false;
}
return $this->currentAccessToken()->can($scope);
}
/**
* Determines whether the user's token does NOT grant permissions to $scope.
* First checks against $this->activeToken, which is set during
* authentication. If it hasn't been set, returns true.
*/
public function tokenCant(string $scope): bool
{
if (! $this->currentAccessToken() instanceof AccessToken) {
return true;
}
return $this->currentAccessToken()->cant($scope);
}
/**
* Returns the current access token for the user.
*/
public function currentAccessToken(): ?AccessToken
{
return $this->currentAccessToken;
}
/**
* Sets the current active token for this user.
*/
public function setAccessToken(?AccessToken $accessToken): self
{
$this->currentAccessToken = $accessToken;
return $this;
}
/**
* Checks if the provided Access Token is expired.
*/
public function isAccessTokenExpired(AccessToken $accessToken): bool
{
return $accessToken->expires instanceof Time && $accessToken->expires->isBefore(Time::now());
}
/**
* Sets an expiration for Access Tokens by ID.
*
* @param int $id AccessTokens ID
* @param Time $expiresAt Expiration date
*
* @return bool Returns true if expiration date is set or updated.
*/
public function updateAccessTokenExpiration(int $id, Time $expiresAt): bool
{
$identityModel = model(UserIdentityModel::class);
$result = $identityModel->setIdentityExpirationById($id, $this, $expiresAt);
if ($result) {
// refresh currentAccessToken with updated data
$this->currentAccessToken = $identityModel->getAccessTokenById($id, $this);
}
return $result;
}
/**
* Removes the expiration date for Access Tokens by ID.
*
* @param int $id AccessTokens ID
*
* @return bool Returns true if expiration date is set or updated.
*/
public function removeAccessTokenExpiration(int $id): bool
{
$identityModel = model(UserIdentityModel::class);
$result = $identityModel->setIdentityExpirationById($id, $this);
if ($result) {
// refresh currentAccessToken with updated data
$this->currentAccessToken = $identityModel->getAccessTokenById($id, $this);
}
return $result;
}
/**
* Checks if the access token has a set expiration date
*/
public function canAccessTokenExpire(AccessToken $accessToken): bool
{
return $accessToken->expires !== null;
}
}