Skip to content

Commit

Permalink
fix: allow KID of '0' (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer authored May 10, 2023
2 parents 7970104 + be6eb58 commit 9dc46a9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
10 changes: 6 additions & 4 deletions tests/JWTTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down

0 comments on commit 9dc46a9

Please sign in to comment.