Skip to content

Commit ffe22bb

Browse files
authored
Merge commit from fork
* fix: reject expired HMAC tokens * tests: cover HMAC token expiration * docs: clarify HMAC token expiration and lifetime
1 parent db08288 commit ffe22bb

4 files changed

Lines changed: 100 additions & 3 deletions

File tree

‎UPGRADING.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ value. The command does not modify either file. Accounts with older password
2626
hash settings can still have different verification times until their hashes
2727
are upgraded.
2828

29+
### HMAC Token Expiration Enforcement
30+
31+
HMAC tokens with an explicitly configured expiration date are now rejected during authentication once they have expired. Previously, the `HmacSha256` authenticator did not enforce the configured expiration date, allowing expired tokens to remain usable if their signatures were valid.
32+
33+
HMAC tokens without an explicitly configured expiration date retain their existing behavior and remain subject to the configured `unusedTokenLifetime` restriction.
34+
2935
## Version 1.2 to 1.3
3036

3137
### JWT: Minimum Key Length Now Enforced

‎docs/references/authentication/hmac.md‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ $user->revokeAllHmacTokens();
9999

100100
## Expiring HMAC Keys
101101

102-
By default, the HMAC keys don't expire unless they reach the HMAC keys' lifetime expiration after their last use date.
102+
By default, HMAC keys do not have a fixed expiration date. However, they will expire if they exceed the configured lifetime since their last use.
103+
104+
You can also assign an explicit expiration date to an HMAC key. When an expiration date is set, Shield automatically checks it during authentication and rejects the key once it has expired.
103105

104106
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)`. To remove it, use `removeHmacTokenExpiration($hmacTokenID)`.
105107

@@ -287,7 +289,10 @@ public $unusedTokenLifetime = YEAR;
287289

288290
### HMAC Keys Expiration vs Lifetime
289291

290-
Expiration and lifetime are two different concepts. The lifetime is the maximum time allowed for the HMAC Key to exist since its last use. HMAC Key expiration, on the other hand, is a set date in which the HMAC Key will cease to function.
292+
Expiration and lifetime are two different concepts.
293+
294+
The lifetime is the maximum period of inactivity allowed for an HMAC key, measured from its last use. Once this period is exceeded, the key is rejected.
295+
Expiration, on the other hand, is a fixed date and time after which the HMAC key can no longer be used, regardless of its last use.
291296

292297
### Login Attempt Logging
293298

‎src/Authentication/Authenticators/HmacSha256.php‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,16 @@ public function check(array $credentials): Result
177177
]);
178178
}
179179

180+
// Reject expired HMAC tokens
181+
$user = $token->user();
182+
183+
if ($user->isHmacTokenExpired($token)) {
184+
return new Result([
185+
'success' => false,
186+
'reason' => lang('Auth.oldToken'),
187+
]);
188+
}
189+
180190
// Hasn't been used in a long time
181191
if (
182192
isset($token->last_used_at)
@@ -197,7 +207,6 @@ public function check(array $credentials): Result
197207
}
198208

199209
// Ensure the token is set as the current token
200-
$user = $token->user();
201210
$user->setHmacToken($token);
202211

203212
return new Result([

‎tests/Authentication/Authenticators/HmacAuthenticatorTest.php‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,83 @@ public function testCheckMalformedToken(): void
166166
$this->assertSame(lang('Auth.badToken'), $result->reason());
167167
}
168168

169+
public function testCheckExpiredToken(): void
170+
{
171+
/** @var User $user */
172+
$user = fake(UserModel::class);
173+
174+
$token = $user->generateHmacToken(
175+
'foo',
176+
['*'],
177+
Time::now()->subMinutes(1),
178+
);
179+
180+
$rawToken = $this->generateRawHeaderToken(
181+
$token->secret,
182+
$token->rawSecretKey,
183+
'bar',
184+
);
185+
186+
$result = $this->auth->check([
187+
'token' => $rawToken,
188+
'body' => 'bar',
189+
]);
190+
191+
$this->assertFalse($result->isOK());
192+
$this->assertSame(lang('Auth.oldToken'), $result->reason());
193+
}
194+
195+
public function testCheckTokenWithFutureExpiration(): void
196+
{
197+
/** @var User $user */
198+
$user = fake(UserModel::class);
199+
200+
$token = $user->generateHmacToken(
201+
'foo',
202+
['*'],
203+
Time::now()->addDays(1),
204+
);
205+
206+
$rawToken = $this->generateRawHeaderToken(
207+
$token->secret,
208+
$token->rawSecretKey,
209+
'bar',
210+
);
211+
212+
$result = $this->auth->check([
213+
'token' => $rawToken,
214+
'body' => 'bar',
215+
]);
216+
217+
$this->assertTrue($result->isOK());
218+
}
219+
220+
public function testCheckTokenAfterExpirationIsUpdated(): void
221+
{
222+
/** @var User $user */
223+
$user = fake(UserModel::class);
224+
$token = $user->generateHmacToken('foo');
225+
226+
$user->updateHmacTokenExpiration(
227+
$token->id,
228+
Time::now()->subMinutes(1),
229+
);
230+
231+
$rawToken = $this->generateRawHeaderToken(
232+
$token->secret,
233+
$token->rawSecretKey,
234+
'bar',
235+
);
236+
237+
$result = $this->auth->check([
238+
'token' => $rawToken,
239+
'body' => 'bar',
240+
]);
241+
242+
$this->assertFalse($result->isOK());
243+
$this->assertSame(lang('Auth.oldToken'), $result->reason());
244+
}
245+
169246
public function testCheckOldToken(): void
170247
{
171248
$user = fake(UserModel::class);

0 commit comments

Comments
 (0)