Skip to content

Commit

Permalink
chore: better BeforeValidException message for decode (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishwarajanand authored Jul 14, 2023
1 parent 299105a commit 0a53cf2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public static function decode(
// token can actually be used. If it's not yet that time, abort.
if (isset($payload->nbf) && floor($payload->nbf) > ($timestamp + static::$leeway)) {
throw new BeforeValidException(
'Cannot handle token prior to ' . \date(DateTime::ISO8601, (int) $payload->nbf)
'Cannot handle token with nbf prior to ' . \date(DateTime::ISO8601, (int) $payload->nbf)
);
}

Expand All @@ -163,7 +163,7 @@ public static function decode(
// correctly used the nbf claim).
if (!isset($payload->nbf) && isset($payload->iat) && floor($payload->iat) > ($timestamp + static::$leeway)) {
throw new BeforeValidException(
'Cannot handle token prior to ' . \date(DateTime::ISO8601, (int) $payload->iat)
'Cannot handle token with iat prior to ' . \date(DateTime::ISO8601, (int) $payload->iat)
);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/JWTTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public function testInvalidTokenWithNbfLeeway()
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$this->expectException(BeforeValidException::class);
$this->expectExceptionMessage('Cannot handle token with nbf prior to');
JWT::decode($encoded, new Key('my_key', 'HS256'));
}

Expand Down Expand Up @@ -176,6 +177,7 @@ public function testValidTokenWithNbfMicrotime()
public function testInvalidTokenWithNbfMicrotime()
{
$this->expectException(BeforeValidException::class);
$this->expectExceptionMessage('Cannot handle token with nbf prior to');
$payload = [
'message' => 'abc',
'nbf' => microtime(true) + 20, // use microtime in the future
Expand Down Expand Up @@ -211,6 +213,7 @@ public function testInvalidTokenWithIatLeeway()
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$this->expectException(BeforeValidException::class);
$this->expectExceptionMessage('Cannot handle token with iat prior to');
JWT::decode($encoded, new Key('my_key', 'HS256'));
}

Expand All @@ -228,6 +231,7 @@ public function testValidTokenWithIatMicrotime()
public function testInvalidTokenWithIatMicrotime()
{
$this->expectException(BeforeValidException::class);
$this->expectExceptionMessage('Cannot handle token with iat prior to');
$payload = [
'message' => 'abc',
'iat' => microtime(true) + 20, // use microtime in the future
Expand Down

0 comments on commit 0a53cf2

Please sign in to comment.