From f567224c91beb559fe11d8adbe7599c623bbc00a Mon Sep 17 00:00:00 2001 From: Luke Kuzmish <42181698+cosmastech@users.noreply.github.com> Date: Sat, 22 Feb 2025 09:44:44 -0500 Subject: [PATCH 1/3] add JWT::resetTime() --- src/JWT.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/JWT.php b/src/JWT.php index dd9292a4..6e753e5c 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -445,6 +445,16 @@ public static function urlsafeB64Encode(string $input): string return \str_replace('=', '', \strtr(\base64_encode($input), '+/', '-_')); } + /** + * Reset leeway and timestamp to their initial values. + * + * @return void + */ + public static function resetTime(): void + { + static::$leeway = 0; + static::$timestamp = null; + } /** * Determine if an algorithm has been provided for each Key From c538903c5fe5d876364a5d3ce32356d7483721e9 Mon Sep 17 00:00:00 2001 From: Luke Kuzmish <42181698+cosmastech@users.noreply.github.com> Date: Sat, 22 Feb 2025 09:51:04 -0500 Subject: [PATCH 2/3] add JWT::maxLeeway() --- src/JWT.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/JWT.php b/src/JWT.php index 6e753e5c..fa6565d4 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -445,6 +445,16 @@ public static function urlsafeB64Encode(string $input): string return \str_replace('=', '', \strtr(\base64_encode($input), '+/', '-_')); } + /** + * Set leeway to max. Useful for unit testing with fixtures. + * + * @return void + */ + public static function maxLeeway(): void + { + static::$leeway = \PHP_INT_MAX; + } + /** * Reset leeway and timestamp to their initial values. * From 05c5502fcabe4e89cdb6b7838ff7af38efdd5a98 Mon Sep 17 00:00:00 2001 From: Luke Kuzmish Date: Sat, 22 Feb 2025 18:45:10 -0500 Subject: [PATCH 3/3] tests --- tests/JWTTest.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/JWTTest.php b/tests/JWTTest.php index d09d43e3..53e235f3 100644 --- a/tests/JWTTest.php +++ b/tests/JWTTest.php @@ -546,4 +546,29 @@ public function testAdditionalHeaderOverrides() $this->assertEquals('my_key_id', $headers->kid, 'key param not overridden'); $this->assertEquals('HS256', $headers->alg, 'alg param not overridden'); } + + public function testReset() + { + JWT::$leeway = 100; + JWT::$timestamp = 7000; + + // Pre-conditions + $this->assertSame(100, JWT::$leeway); + $this->assertSame(7000, JWT::$timestamp); + + JWT::resetTime(); + + $this->assertSame(0, JWT::$leeway); + $this->assertNull(JWT::$timestamp); + } + + public function testMaxLeeway() + { + // Pre-condition + $this->assertSame(0, JWT::$leeway); + + JWT::maxLeeway(); + + $this->assertSame(\PHP_INT_MAX, JWT::$leeway); + } }