From 0a53cf2986e45c2bcbf1a269f313ebf56a154ee4 Mon Sep 17 00:00:00 2001 From: Vishwaraj Anand Date: Fri, 14 Jul 2023 23:19:54 +0530 Subject: [PATCH] chore: better BeforeValidException message for decode (#526) --- src/JWT.php | 4 ++-- tests/JWTTest.php | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/JWT.php b/src/JWT.php index db075ad0..18927452 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -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) ); } @@ -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) ); } diff --git a/tests/JWTTest.php b/tests/JWTTest.php index 5265e471..44b3f049 100644 --- a/tests/JWTTest.php +++ b/tests/JWTTest.php @@ -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')); } @@ -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 @@ -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')); } @@ -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