Skip to content

Commit 9682431

Browse files
committed
fix tokens expiration method names.
fix docs and tests.
1 parent b6b6f27 commit 9682431

File tree

8 files changed

+107
-66
lines changed

8 files changed

+107
-66
lines changed

docs/references/authentication/hmac.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,30 +101,30 @@ $user->revokeAllHmacTokens();
101101

102102
By default, the HMAC keys don't expire unless they meet the HMAC Keys lifetime expiration after their last used date.
103103

104-
HMAC keys can be set to expire through the `generateHmacToken()` method. This takes the expiration date as the $expiresAt argument. To update or remove an existing HMAC key expiration date use `setHmacTokenExpirationById($HmacTokenID, $expiresAt)`
104+
HMAC keys can be set to expire through the `generateHmacToken()` method. This takes the expiration date as the `$expiresAt` argument. To update an existing HMAC key expiration date use `updateHmacTokenExpiration($hmacTokenID, $expiresAt)` and to remove `removeHmacTokenExpiration($hmacTokenID)`.
105105

106-
`$expiresAt` [Time](/libraries/time.html) object
106+
`$expiresAt` [Time](https://codeigniter.com/user_guide/libraries/time.html) object
107107

108108
```php
109109
// Expiration date = 2024-11-03 12:00:00
110110
$expiresAt = Time::parse('2024-11-03 12:00:00');
111-
$token = $this->user->generateHmacToken('foo', ['foo:bar'], $expiresAt);
111+
$token = $this->user->generateHmacToken('foo', ['foo.bar'], $expiresAt);
112112

113113
// Expiration date = 2024-11-15 00:00:00
114114
$expiresAt = Time::parse('2024-11-15 00:00:00');
115-
$token = $user->setHmacTokenExpirationById($token->id, $expiresAt);
115+
$user->updateHmacTokenExpiration($token->id, $expiresAt);
116116

117117
// Expiration date = 1 month + 15 days into the future
118118
$expiresAt = Time::now()->addMonths(1)->addDays(15);
119-
$token = $user->setHmacTokenExpirationById($token->id, $expiresAt);
119+
$user->updateHmacTokenExpiration($token->id, $expiresAt);
120120

121121
// Remove the expiration date
122-
$token = $user->setHmacTokenExpirationById($token->id, null);
122+
$user->removeHmacTokenExpiration($token->id);
123123
```
124124

125125
The following support methods are also available:
126126

127-
`hasHmacTokenExpired(AccessToken $HmacToken)` - Checks if the HMAC key has expired. Returns `true` if the HMAC key has expired, `false` if not.
127+
`isHmacTokenExpired(AccessToken $HmacToken)` - Checks if the HMAC key is expired. Returns `true` if the HMAC key is expired, `false` if not.
128128

129129
```php
130130
$expiresAt = Time::parse('2024-11-03 12:00:00');
@@ -133,7 +133,7 @@ $token = $this->user->generateHmacToken('foo', ['foo.bar'], $expiresAt);
133133
$this->user->hasHmacTokenExpired($token); // Returns true
134134
```
135135

136-
`canHmacTokenExpire(AccessToken $HmacToken)` - Checks if HMAC key has an expiration set. Returns `true` or `false` accordingly.
136+
`hasHmacTokenExpiry(AccessToken $HmacToken)` - Checks if HMAC key has an expiration set. Returns `true` or `false` accordingly.
137137

138138
```php
139139
$expiresAt = Time::parse('2024-11-03 12:00:00');
@@ -146,10 +146,12 @@ $this->user->canHmacTokenExpire($token2); // Returns false
146146
```
147147

148148
You can also easily set all existing HMAC keys/tokens as expired with the `spark` command:
149-
```
149+
```console
150150
php spark shield:hmac invalidateAll
151151
```
152-
**Careful!** This command invalidates _all_ keys for _all_ users.
152+
153+
!!! warning
154+
This command invalidates _all_ keys for _all_ users.
153155

154156
## Retrieving HMAC Keys
155157

docs/references/authentication/tokens.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ public $unusedTokenLifetime = YEAR;
142142

143143
By default, the Access Tokens don't expire unless they meet the Access Token lifetime expiration after their last used date.
144144

145-
Access Tokens can be set to expire through the `generateAccessToken()` method. This takes the expiration date as the $expiresAt argument. To update or remove an existing HMAC key expiration date use `setAccessTokenById($HmacTokenID, $expiresAt)`
145+
Access Tokens can be set to expire through the `generateAccessToken()` method. This takes the expiration date as the `$expiresAt` argument. To update an existing HMAC key expiration date use `updateAcessTokenExpiration($hmacTokenID, $expiresAt)` and to remove `removeAccessTokenExpiration($hmacTokenID)`.
146146

147-
`$expiresAt` [Time](/libraries/time.html) object
147+
`$expiresAt` [Time](https://codeigniter.com/user_guide/libraries/time.html) object
148148

149149
```php
150150
// Expiration date = 2024-11-03 12:00:00
@@ -153,19 +153,19 @@ $token = $this->user->generateAccessToken('foo', ['foo.bar'], $expiresAt);
153153

154154
// Expiration date = 2024-11-15 00:00:00
155155
$expiresAt = Time::parse('2024-11-15 00:00:00');
156-
$user->setAccessTokenExpirationById($token->id, $expiresAt);
156+
$user->updateAcessTokenExpiration($token->id, $expiresAt);
157157

158158
// Or Expiration date = 1 month + 15 days into the future
159159
$expiresAt = Time::now()->addMonths(1)->addDays(15);
160-
$user->setAccessTokenExpirationById($token->id, $expiresAt);
160+
$user->updateAcessTokenExpiration($token->id, $expiresAt);
161161

162162
// Remove the expiration date
163-
$token = $user->setAccessTokenExpirationById($token->id, null);
163+
$user->removeAccessTokenExpiration($token->id);
164164
```
165165

166166
The following support methods are also available:
167167

168-
`hasAccessTokenExpired(AccessToken $accessToken)` - Checks if Access Token has expired. Returns `true` if the Access Token has expired, `false` if not.
168+
`isAccessTokenExpired(AccessToken $accessToken)` - Checks if Access Token is expired. Returns `true` if the Access Token is expired, `false` if not.
169169

170170
```php
171171
$expiresAt = Time::parse('2024-11-03 12:00:00');
@@ -175,7 +175,7 @@ $token = $this->user->generateAccessToken('foo', ['foo.bar'], $expiresAt);
175175
$this->user->hasAccessTokenExpired($token); // Returns true
176176
```
177177

178-
`canAccessTokenExpire(AccessToken $HmacToken)` - Checks if Access Token has an expiration set. Returns `true` or `false` accordingly.
178+
`hasAccessTokenExpiry(AccessToken $HmacToken)` - Checks if Access Token has an expiration set. Returns `true` or `false` accordingly.
179179

180180
```php
181181
$expiresAt = Time::parse('2024-11-03 12:00:00');

phpstan-baseline.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@
265265
$ignoreErrors[] = [
266266
'message' => '#^Call to function model with CodeIgniter\\\\Shield\\\\Models\\\\UserIdentityModel\\:\\:class is discouraged\\.$#',
267267
'identifier' => 'codeigniter.factoriesClassConstFetch',
268-
'count' => 21,
268+
'count' => 23,
269269
'path' => __DIR__ . '/src/Entities/User.php',
270270
];
271271
$ignoreErrors[] = [

src/Authentication/Traits/HasAccessTokens.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ trait HasAccessTokens
3939
*
4040
* @param string $name Token name
4141
* @param list<string> $scopes Permissions the token grants
42-
* @param Time $expiresAt Expiration date
42+
* @param Time|null $expiresAt Expiration date
4343
*
4444
* @throws InvalidArgumentException
4545
*/
@@ -173,11 +173,11 @@ public function setAccessToken(?AccessToken $accessToken): self
173173
}
174174

175175
/**
176-
* Checks if the provided Access Token has expired.
176+
* Checks if the provided Access Token is expired.
177177
*
178-
* @return bool Returns true if Access Token has expired, false if not
178+
* @return bool Returns true if Access Token is expired, false if not
179179
*/
180-
public function hasAccessTokenExpired(AccessToken $accessToken): bool
180+
public function isAccessTokenExpired(AccessToken $accessToken): bool
181181
{
182182
return $accessToken->expires !== null && $accessToken->expires->isBefore(Time::now());
183183
}
@@ -190,11 +190,32 @@ public function hasAccessTokenExpired(AccessToken $accessToken): bool
190190
*
191191
* @return bool Returns true if expiration date is set or updated.
192192
*/
193-
public function setAccessTokenExpirationById(int $id, ?Time $expiresAt): bool
193+
public function updateAccessTokenExpiration(int $id, Time $expiresAt): bool
194194
{
195195
/** @var UserIdentityModel $identityModel */
196196
$identityModel = model(UserIdentityModel::class);
197-
$result = $identityModel->setIdentityExpirationById($id, $this, $expiresAt, AccessTokens::ID_TYPE_ACCESS_TOKEN);
197+
$result = $identityModel->setIdentityExpirationById($id, $this, $expiresAt);
198+
199+
if ($result) {
200+
// refresh currentAccessToken with updated data
201+
$this->currentAccessToken = $identityModel->getAccessTokenById($id, $this);
202+
}
203+
204+
return $result;
205+
}
206+
207+
/**
208+
* Removes the expiration date for Access Tokens by ID.
209+
*
210+
* @param int $id AccessTokens ID
211+
*
212+
* @return bool Returns true if expiration date is set or updated.
213+
*/
214+
public function removeAccessTokenExpiration(int $id): bool
215+
{
216+
/** @var UserIdentityModel $identityModel */
217+
$identityModel = model(UserIdentityModel::class);
218+
$result = $identityModel->setIdentityExpirationById($id, $this);
198219

199220
if ($result) {
200221
// refresh currentAccessToken with updated data
@@ -209,7 +230,7 @@ public function setAccessTokenExpirationById(int $id, ?Time $expiresAt): bool
209230
*
210231
* @return bool Returns true if AccessToken can expire.
211232
*/
212-
public function canAccessTokenExpire(AccessToken $accessToken): bool
233+
public function hasAccessTokenExpiry(AccessToken $accessToken): bool
213234
{
214235
return $accessToken->expires !== null;
215236
}

src/Authentication/Traits/HasHmacTokens.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace CodeIgniter\Shield\Authentication\Traits;
1515

1616
use CodeIgniter\I18n\Time;
17-
use CodeIgniter\Shield\Authentication\Authenticators\HmacSha256;
1817
use CodeIgniter\Shield\Entities\AccessToken;
1918
use CodeIgniter\Shield\Models\UserIdentityModel;
2019
use InvalidArgumentException;
@@ -40,7 +39,7 @@ trait HasHmacTokens
4039
*
4140
* @param string $name Token name
4241
* @param list<string> $scopes Permissions the token grants
43-
* @param Time $expiresAt Expiration date
42+
* @param Time|null $expiresAt Expiration date
4443
*
4544
* @throws InvalidArgumentException
4645
* @throws ReflectionException
@@ -163,11 +162,11 @@ public function setHmacToken(?AccessToken $accessToken): self
163162
}
164163

165164
/**
166-
* Checks if the provided Access Token has expired.
165+
* Checks if the provided Access Token is expired.
167166
*
168-
* @return bool Returns true if Access Token has expired, false if not
167+
* @return bool Returns true if Access Token is expired, false if not
169168
*/
170-
public function hasHmacTokenExpired(AccessToken $accessToken): bool
169+
public function isHmacTokenExpired(AccessToken $accessToken): bool
171170
{
172171
return $accessToken->expires !== null && $accessToken->expires->isBefore(Time::now());
173172
}
@@ -180,11 +179,11 @@ public function hasHmacTokenExpired(AccessToken $accessToken): bool
180179
*
181180
* @return bool Returns true if expiration date is set or updated.
182181
*/
183-
public function setHmacTokenExpirationById(int $id, ?Time $expiresAt): bool
182+
public function updateHmacTokenExpiration(int $id, Time $expiresAt): bool
184183
{
185184
/** @var UserIdentityModel $identityModel */
186185
$identityModel = model(UserIdentityModel::class);
187-
$result = $identityModel->setIdentityExpirationById($id, $this, $expiresAt, HmacSha256::ID_TYPE_HMAC_TOKEN);
186+
$result = $identityModel->setIdentityExpirationById($id, $this, $expiresAt);
188187

189188
if ($result) {
190189
// refresh currentAccessToken with updated data
@@ -194,12 +193,33 @@ public function setHmacTokenExpirationById(int $id, ?Time $expiresAt): bool
194193
return $result;
195194
}
196195

196+
/**
197+
* Removes the expiration date for Hmac Key/Token by ID.
198+
*
199+
* @param int $id AccessToken ID
200+
*
201+
* @return bool Returns true if expiration date is removed.
202+
*/
203+
public function removeHmacTokenExpiration(int $id): bool
204+
{
205+
/** @var UserIdentityModel $identityModel */
206+
$identityModel = model(UserIdentityModel::class);
207+
$result = $identityModel->setIdentityExpirationById($id, $this);
208+
209+
if ($result) {
210+
// refresh currentAccessToken with updated data
211+
$this->currentHmacToken = $identityModel->getHmacTokenById($id, $this);
212+
}
213+
214+
return $result;
215+
}
216+
197217
/**
198218
* Checks if the current Hmac token can expire
199219
*
200220
* @return bool Returns true if Hmac Token can expire.
201221
*/
202-
public function canHmacTokenExpire(AccessToken $accessToken): bool
222+
public function hasHmacTokenExpiry(AccessToken $accessToken): bool
203223
{
204224
return $accessToken->expires !== null;
205225
}

src/Models/UserIdentityModel.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,21 +230,21 @@ public function getAllAccessTokens(User $user): array
230230
}
231231

232232
/**
233-
* Updates or sets expiration date of users' AccessToken or HMAC Token by ID. Returns updated row.
233+
* Updates or sets expiration date of users' AccessToken or HMAC Token by ID.
234234
*
235235
* @param Time $expiresAt Expiration date
236236
* @param mixed $id
237237
*
238238
* @return bool Returns true if expiration date was set or updated.
239239
*/
240-
public function setIdentityExpirationById($id, User $user, ?Time $expiresAt = null, ?string $type_token = null): bool
240+
public function setIdentityExpirationById($id, User $user, ?Time $expiresAt = null): bool
241241
{
242242
$this->checkUserId($user);
243243

244244
return $this->where('user_id', $user->id)
245-
->where('type', $type_token)
245+
->where('id', $id)
246246
->set(['expires' => $expiresAt])
247-
->update($id);
247+
->update();
248248
}
249249

250250
// HMAC

tests/Authentication/HasAccessTokensTest.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ public function testGenerateTokenWithExpiration(): void
164164

165165
$this->assertSame($tokenExpiration->format('Y-m-d h:i:s'), $this->user->currentAccessToken()->expires->format('Y-m-d h:i:s'));
166166

167-
$tokenExpiration = $tokenExpiration->addMonths(1);
168-
$tokenExpiration = $tokenExpiration->addYears(1);
167+
$tokenExpiration = $tokenExpiration->addMonths(1)->addYears(1);
169168
$token = $this->user->generateAccessToken('foo', ['foo.bar'], $tokenExpiration);
170169
$this->user->setAccessToken($token);
171170

@@ -185,7 +184,7 @@ public function testSetTokenExpirationById(): void
185184

186185
$tokenExpiration = Time::parse('2024-11-03 12:00:00');
187186

188-
$this->assertTrue($this->user->setAccessTokenExpirationById($token->id, $tokenExpiration));
187+
$this->assertTrue($this->user->updateAccessTokenExpiration($token->id, $tokenExpiration));
189188
$this->assertSame($tokenExpiration->format('Y-m-d h:i:s'), $this->user->currentAccessToken()->expires->format('Y-m-d h:i:s'));
190189
}
191190

@@ -198,16 +197,15 @@ public function testIsTokenExpired(): void
198197
$token = $this->user->generateAccessToken('foo', ['foo.bar'], $tokenExpiration);
199198
$this->user->setAccessToken($token);
200199

201-
$this->assertTrue($this->user->hasAccessTokenExpired($this->user->currentAccessToken()));
200+
$this->assertTrue($this->user->isAccessTokenExpired($this->user->currentAccessToken()));
202201
}
203202

204203
/**
205204
* See https://github.com/codeigniter4/shield/issues/926
206205
*/
207206
public function testTokenTimeToExpired(): void
208207
{
209-
$tokenExpiration = Time::now();
210-
$tokenExpiration = $tokenExpiration->addYears(1);
208+
$tokenExpiration = Time::now()->addYears(1);
211209

212210
$token = $this->user->generateAccessToken('foo', ['foo.bar'], $tokenExpiration);
213211
$this->user->setAccessToken($token);
@@ -218,34 +216,34 @@ public function testTokenTimeToExpired(): void
218216
/**
219217
* See https://github.com/codeigniter4/shield/issues/926
220218
*/
221-
public function testCanHmacTokenExpire(): void
219+
public function testHasHmacTokenExpiry(): void
222220
{
223-
$tokenExpiration = Time::now();
224-
$tokenExpiration = $tokenExpiration->addYears(1);
221+
$tokenExpiration = Time::now()->addYears(1);
225222

226223
$token = $this->user->generateAccessToken('foo', ['foo.bar'], $tokenExpiration);
227224

228-
$this->assertTrue($this->user->canAccessTokenExpire($token));
225+
$this->assertTrue($this->user->hasAccessTokenExpiry($token));
229226

230227
$token = $this->user->generateAccessToken('foo', ['foo.bar']);
231228

232-
$this->assertFalse($this->user->canAccessTokenExpire($token));
229+
$this->assertFalse($this->user->hasAccessTokenExpiry($token));
233230
}
234231

235232
/**
236233
* See https://github.com/codeigniter4/shield/issues/926
237234
*/
238235
public function testAccessTokenRemoveExpiration(): void
239236
{
240-
$tokenExpiration = Time::now();
241-
$tokenExpiration = $tokenExpiration->addYears(1);
237+
$tokenExpiration = Time::now()->addYears(1);
242238

243239
$token = $this->user->generateAccessToken('foo', ['foo.bar'], $tokenExpiration);
244240

245-
$this->assertTrue($this->user->canAccessTokenExpire($token));
241+
$this->user->setAccessToken($token);
242+
243+
$this->assertTrue($this->user->hasAccessTokenExpiry($token));
246244

247-
$this->user->setAccessTokenExpirationById($token->id, null);
245+
$this->assertTrue($this->user->removeAccessTokenExpiration($token->id));
248246

249-
$this->assertFalse($this->user->canAccessTokenExpire($this->user->currentAccessToken()));
247+
$this->assertFalse($this->user->hasAccessTokenExpiry($this->user->currentAccessToken()));
250248
}
251249
}

0 commit comments

Comments
 (0)