diff --git a/src/JWT.php b/src/JWT.php index 61524e7b..c83ff099 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -439,7 +439,7 @@ private static function getKey( return $keyOrKeyArray; } - if (empty($kid)) { + if (empty($kid) && $kid !== '0') { 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';