From d6e3e5c007778fd8ba0ef4916c0548c68e6719ce Mon Sep 17 00:00:00 2001 From: Karim Pinchon Date: Wed, 27 Jul 2022 09:58:30 +0200 Subject: [PATCH 1/9] feat: allow a third param to get headers --- src/JWT.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/JWT.php b/src/JWT.php index 9964073d..38d43383 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -89,7 +89,8 @@ class JWT */ public static function decode( string $jwt, - $keyOrKeyArray + $keyOrKeyArray, + &$headers = null ): stdClass { // Validate JWT $timestamp = \is_null(static::$timestamp) ? \time() : static::$timestamp; @@ -106,6 +107,11 @@ public static function decode( if (null === ($header = static::jsonDecode($headerRaw))) { throw new UnexpectedValueException('Invalid header encoding'); } + + if ($headers !== null) { + $headers = $header; + } + $payloadRaw = static::urlsafeB64Decode($bodyb64); if (null === ($payload = static::jsonDecode($payloadRaw))) { throw new UnexpectedValueException('Invalid claims encoding'); From b85bb03eb523223c633bba3c136abfcaa27768a5 Mon Sep 17 00:00:00 2001 From: Karim Pinchon Date: Wed, 27 Jul 2022 09:59:41 +0200 Subject: [PATCH 2/9] test: allow a third param to get headers --- tests/JWTTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/JWTTest.php b/tests/JWTTest.php index 58c334ed..5367f55c 100644 --- a/tests/JWTTest.php +++ b/tests/JWTTest.php @@ -394,4 +394,19 @@ public function testEncodeDecodeWithResource() $this->assertEquals('bar', $decoded->foo); } + + public function testGetHeaders() + { + $payload = [ + 'message' => 'abc', + 'exp' => time() + JWT::$leeway + 20, // time in the future + ]; + $headers = new stdClass(); + + $encoded = JWT::encode($payload, 'my_key', 'HS256'); + JWT::decode($encoded, new Key('my_key', 'HS256'), $headers); + + $this->assertEquals($headers->typ, 'JWT'); + $this->assertEquals($headers->alg, 'HS256'); + } } From 928e17d5c188e90e44f896a9cef39fcbc5d17721 Mon Sep 17 00:00:00 2001 From: Karim Pinchon Date: Wed, 27 Jul 2022 10:00:00 +0200 Subject: [PATCH 3/9] doc: allow a third param to get headers --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fed1e954..93d44590 100644 --- a/README.md +++ b/README.md @@ -44,9 +44,15 @@ $payload = [ * for a list of spec-compliant algorithms. */ $jwt = JWT::encode($payload, $key, 'HS256'); -$decoded = JWT::decode($jwt, new Key($key, 'HS256')); +/** + * The third parameter allow you to get header values in a stdClass object (you need to instance it before). + * It is optional. + */ +$headers = new stdClass(); +$decoded = JWT::decode($jwt, new Key($key, 'HS256'), $headers); print_r($decoded); +print_r($headers); /* NOTE: This will now be an object instead of an associative array. To get From 621351b5af563c99c9ee903119caa34b9c82d490 Mon Sep 17 00:00:00 2001 From: Vishwaraj Anand Date: Tue, 13 Jun 2023 21:50:26 +0530 Subject: [PATCH 4/9] Update src/JWT.php --- src/JWT.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/JWT.php b/src/JWT.php index ff820371..5ac3dd2a 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -112,7 +112,6 @@ public static function decode( if (null === ($header = static::jsonDecode($headerRaw))) { throw new UnexpectedValueException('Invalid header encoding'); } - if ($headers !== null) { $headers = $header; } From f105a1af9657561f10b802cd5f9c2055ace4e4fd Mon Sep 17 00:00:00 2001 From: Vishwaraj Anand Date: Tue, 13 Jun 2023 21:50:54 +0530 Subject: [PATCH 5/9] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 48f34b80..999f3bc5 100644 --- a/README.md +++ b/README.md @@ -45,8 +45,8 @@ $payload = [ */ $jwt = JWT::encode($payload, $key, 'HS256'); /** - * The third parameter allow you to get header values in a stdClass object (you need to instance it before). - * It is optional. + * Providing headers is optional. If provided with an instantiated object, + * headers will be populated. */ $headers = new stdClass(); $decoded = JWT::decode($jwt, new Key($key, 'HS256'), $headers); From f38f324d2dd99e53ebecbec99ffc2d7ae7f531b5 Mon Sep 17 00:00:00 2001 From: Vishwaraj Anand Date: Tue, 13 Jun 2023 21:53:13 +0530 Subject: [PATCH 6/9] Update src/JWT.php --- src/JWT.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/JWT.php b/src/JWT.php index 5ac3dd2a..9f5bb779 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -115,7 +115,6 @@ public static function decode( if ($headers !== null) { $headers = $header; } - $payloadRaw = static::urlsafeB64Decode($bodyb64); if (null === ($payload = static::jsonDecode($payloadRaw))) { throw new UnexpectedValueException('Invalid claims encoding'); From eb07f632068c0105699df2afbf2aae83bf945e90 Mon Sep 17 00:00:00 2001 From: Vishwaraj Anand Date: Tue, 13 Jun 2023 21:54:45 +0530 Subject: [PATCH 7/9] Update src/JWT.php Co-authored-by: Brent Shaffer --- src/JWT.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JWT.php b/src/JWT.php index 9f5bb779..5387a1b6 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -95,7 +95,7 @@ class JWT public static function decode( string $jwt, $keyOrKeyArray, - &$headers = null + stdClass &$headers = null ): stdClass { // Validate JWT $timestamp = \is_null(static::$timestamp) ? \time() : static::$timestamp; From 3541ab936521e91672b7fd8108e832d31050ce30 Mon Sep 17 00:00:00 2001 From: Vishwaraj Anand Date: Tue, 13 Jun 2023 21:58:21 +0530 Subject: [PATCH 8/9] Update README.md --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 999f3bc5..f2cc5d03 100644 --- a/README.md +++ b/README.md @@ -44,12 +44,12 @@ $payload = [ * for a list of spec-compliant algorithms. */ $jwt = JWT::encode($payload, $key, 'HS256'); -/** - * Providing headers is optional. If provided with an instantiated object, - * headers will be populated. - */ -$headers = new stdClass(); -$decoded = JWT::decode($jwt, new Key($key, 'HS256'), $headers); +$decoded = JWT::decode($jwt, new Key($key, 'HS256')); +print_r($decoded); + +// Pass a stdClass in as the third parameter to get the decoded header values +$decoded = JWT::decode($jwt, new Key($key, 'HS256'), $headers = new stdClass()); +print_r($headers); print_r($decoded); print_r($headers); From d05135647fcd4c1f4c531223376d6952c750a0e8 Mon Sep 17 00:00:00 2001 From: Vishwaraj Anand Date: Tue, 13 Jun 2023 22:02:33 +0530 Subject: [PATCH 9/9] Update JWT.php --- src/JWT.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/JWT.php b/src/JWT.php index 5387a1b6..7ffb9852 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -78,6 +78,7 @@ class JWT * Supported algorithms are 'ES384','ES256', * 'HS256', 'HS384', 'HS512', 'RS256', 'RS384' * and 'RS512'. + * @param stdClass $headers Optional. Populates stdClass with headers. * * @return stdClass The JWT's payload as a PHP object *