Skip to content

Commit 9dc46a9

Browse files
authored
fix: allow KID of '0' (#505)
2 parents 7970104 + be6eb58 commit 9dc46a9

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

Diff for: src/JWT.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ private static function getKey(
439439
return $keyOrKeyArray;
440440
}
441441

442-
if (empty($kid)) {
442+
if (empty($kid) && $kid !== '0') {
443443
throw new UnexpectedValueException('"kid" empty, unable to lookup correct key');
444444
}
445445

Diff for: tests/JWTTest.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,11 @@ public function testEmptyKeyFails()
204204
public function testKIDChooser()
205205
{
206206
$keys = [
207-
'1' => new Key('my_key', 'HS256'),
207+
'0' => new Key('my_key0', 'HS256'),
208+
'1' => new Key('my_key1', 'HS256'),
208209
'2' => new Key('my_key2', 'HS256')
209210
];
210-
$msg = JWT::encode(['message' => 'abc'], $keys['1']->getKeyMaterial(), 'HS256', '1');
211+
$msg = JWT::encode(['message' => 'abc'], $keys['0']->getKeyMaterial(), 'HS256', '0');
211212
$decoded = JWT::decode($msg, $keys);
212213
$expected = new stdClass();
213214
$expected->message = 'abc';
@@ -217,10 +218,11 @@ public function testKIDChooser()
217218
public function testArrayAccessKIDChooser()
218219
{
219220
$keys = new ArrayObject([
220-
'1' => new Key('my_key', 'HS256'),
221+
'0' => new Key('my_key0', 'HS256'),
222+
'1' => new Key('my_key1', 'HS256'),
221223
'2' => new Key('my_key2', 'HS256'),
222224
]);
223-
$msg = JWT::encode(['message' => 'abc'], $keys['1']->getKeyMaterial(), 'HS256', '1');
225+
$msg = JWT::encode(['message' => 'abc'], $keys['0']->getKeyMaterial(), 'HS256', '0');
224226
$decoded = JWT::decode($msg, $keys);
225227
$expected = new stdClass();
226228
$expected->message = 'abc';

0 commit comments

Comments
 (0)