Skip to content

Commit bbe4d73

Browse files
panvalinuxwolf
authored andcommitted
Fix: coerce "kid" during lookup (#116)
Fixes #109 * Number typed "kid", coerce to string * target all non-strings instead of only numbers
1 parent 3491d88 commit bbe4d73

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

lib/jwk/keystore.js

+10
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,11 @@ var JWKStore = function(registry, parent) {
395395
value: function(props, local) {
396396
props = props || {};
397397

398+
// workaround for issues/109
399+
if (props.kid !== undefined && props.kid !== null && typeof props.kid !== "string") {
400+
props.kid = String(props.kid);
401+
}
402+
398403
var candidates = [];
399404
var matches = function(key) {
400405
// match on 'kty'
@@ -476,6 +481,11 @@ var JWKStore = function(registry, parent) {
476481
props.kid = kid;
477482
}
478483

484+
// workaround for issues/109
485+
if (props.kid !== undefined && props.kid !== null && typeof props.kid !== "string") {
486+
props.kid = String(props.kid);
487+
}
488+
479489
var candidates = this.all(props, true);
480490
if (!candidates.length && parent && !local) {
481491
candidates = parent.get(props, local);

test/jwk/keystore-test.js

+22
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,28 @@ describe("jwk/keystore", function() {
386386

387387
describe("query", function() {
388388
// TODO: testit!
389+
390+
it("handles (albeit off-spec) number kids", function() {
391+
var promise = JWK.store.KeyStore.asKeyStore({
392+
keys: [
393+
{
394+
kty: 'RSA',
395+
alg: 'RS256',
396+
use: 'sig',
397+
n: 'zi9ox5mVK1nS6rASj5VwTqsozmyoHcqOuf2LLvuNzijPx7ybASzUerP-QZCYL3EC66TtmO2T2fxEyfrK0r7OpsJ3QYlZZ4rHm7s_mFc9upxjnTZ-ElJJsAxWhuBZyZTpzfXT7lTm4QN0QZgy3ydmv4W4RFh2tzAZ4wKc4ruoI-SIVSiZZZ_R3-zhu6zu2JfRc6Vt6MapLfgNtaVuzKWeuCC-42-4vngf2TYqJLlRvrywNJ1qtf-dUpB5UutJUIPBeDrVmoJPC7H8cdbOxSV3b4y8cvn0aQQouO3vQGyNg-LA0D-NGuSW-nEOyfuUR0skcUh6VIhEpcw8iF8nJ1X7yQ',
398+
e: 'AQAB',
399+
kid: 1
400+
}
401+
]
402+
});
403+
404+
promise = promise.then(function (keystore) {
405+
assert.ok(keystore.get({ kid: 1 }));
406+
assert.equal(keystore.all({ kid: 1 }).length, 1);
407+
});
408+
409+
return promise;
410+
});
389411
});
390412

391413
describe("export", function() {

0 commit comments

Comments
 (0)