From 4543842ca47254de7d2d04b8771a8528690cb00a Mon Sep 17 00:00:00 2001 From: Ajumal Date: Tue, 9 May 2023 20:36:06 +0530 Subject: [PATCH 1/2] fix: Allow KID index 0 --- src/JWT.php | 2 +- tests/JWTTest.php | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/JWT.php b/src/JWT.php index 61524e7b..421c42c2 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -439,7 +439,7 @@ private static function getKey( return $keyOrKeyArray; } - if (empty($kid)) { + if (!isset($kid)) { throw new UnexpectedValueException('"kid" empty, unable to lookup correct key'); } diff --git a/tests/JWTTest.php b/tests/JWTTest.php index 3ce912ed..a5721d98 100644 --- a/tests/JWTTest.php +++ b/tests/JWTTest.php @@ -204,10 +204,11 @@ public function testEmptyKeyFails() public function testKIDChooser() { $keys = [ - '1' => new Key('my_key', 'HS256'), + '0' => new Key('my_key0', 'HS256'), + '1' => new Key('my_key1', 'HS256'), '2' => new Key('my_key2', 'HS256') ]; - $msg = JWT::encode(['message' => 'abc'], $keys['1']->getKeyMaterial(), 'HS256', '1'); + $msg = JWT::encode(['message' => 'abc'], $keys['0']->getKeyMaterial(), 'HS256', '0'); $decoded = JWT::decode($msg, $keys); $expected = new stdClass(); $expected->message = 'abc'; @@ -217,10 +218,11 @@ public function testKIDChooser() public function testArrayAccessKIDChooser() { $keys = new ArrayObject([ - '1' => new Key('my_key', 'HS256'), + '0' => new Key('my_key0', 'HS256'), + '1' => new Key('my_key1', 'HS256'), '2' => new Key('my_key2', 'HS256'), ]); - $msg = JWT::encode(['message' => 'abc'], $keys['1']->getKeyMaterial(), 'HS256', '1'); + $msg = JWT::encode(['message' => 'abc'], $keys['0']->getKeyMaterial(), 'HS256', '0'); $decoded = JWT::decode($msg, $keys); $expected = new stdClass(); $expected->message = 'abc'; From be6eb589e86b0f8ca9e050765b3375dfab5bf91e Mon Sep 17 00:00:00 2001 From: Ajumal Date: Wed, 10 May 2023 18:08:43 +0530 Subject: [PATCH 2/2] fix: Allow KID index 0 --- src/JWT.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JWT.php b/src/JWT.php index 421c42c2..c83ff099 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -439,7 +439,7 @@ private static function getKey( return $keyOrKeyArray; } - if (!isset($kid)) { + if (empty($kid) && $kid !== '0') { throw new UnexpectedValueException('"kid" empty, unable to lookup correct key'); }