Skip to content

Commit

Permalink
feat: keystore filtering by JWK Key thumbprint
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Jan 26, 2020
1 parent 9df7d9d commit a9f6f71
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ specified by the parameters are first.
- `crv`: `<string>` Key Curve to filter for. (for EC and OKP keys)
- `alg`: `<string>` Key supported algorithm to filter for.
- `kid`: `<string>` Key ID to filter for.
- `thumbprint`: `<string>` JWK Key thumbprint to filter for.
- `use`: `<string>` Filter keys with the specified use defined. Keys missing "use" parameter will
be matched but rank lower then ones with an exact match.
- `key_ops`: `string[]` Filter keys with specified key_ops defined (if key_ops is defined on the
Expand All @@ -701,6 +702,7 @@ parameters is returned.
- `crv`: `<string>` Key Curve to filter for. (for EC and OKP keys)
- `alg`: `<string>` Key supported algorithm to filter for.
- `kid`: `<string>` Key ID to filter for.
- `thumbprint`: `<string>` JWK Key thumbprint to filter for.
- `use`: `<string>` Filter keys with the specified use defined. Keys missing "use" parameter will
be matched but rank lower then ones with an exact match.
- `key_ops`: `string[]` Filter keys with specified key_ops defined (if key_ops is defined on the
Expand Down
6 changes: 5 additions & 1 deletion lib/jwks/keystore.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class KeyStore {
i(this).keys = new Set(keys)
}

all ({ alg, kid, use, kty, key_ops: ops, x5t, 'x5t#S256': x5t256, crv } = {}) {
all ({ alg, kid, thumbprint, use, kty, key_ops: ops, x5t, 'x5t#S256': x5t256, crv } = {}) {
if (ops !== undefined && (!Array.isArray(ops) || !ops.length || ops.some(x => typeof x !== 'string'))) {
throw new TypeError('`key_ops` must be a non-empty array of strings')
}
Expand All @@ -65,6 +65,10 @@ class KeyStore {
candidate = false
}

if (candidate && thumbprint !== undefined && key.thumbprint !== thumbprint) {
candidate = false
}

if (candidate && x5t !== undefined && key.x5t !== x5t) {
candidate = false
}
Expand Down
9 changes: 9 additions & 0 deletions test/jwks/keystore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ test('.all() and .get() kid filter', t => {
t.is(ks.get({ kid: 'foobar' }), k)
})

test('.all() and .get() thumbprint filter', t => {
const k = generateSync('RSA')
const ks = new KeyStore(k)
t.deepEqual(ks.all({ thumbprint: 'baz' }), [])
t.deepEqual(ks.all({ thumbprint: k.thumbprint }), [k])
t.is(ks.get({ thumbprint: 'baz' }), undefined)
t.is(ks.get({ thumbprint: k.thumbprint }), k)
})

test('.all() and .get() x5t filter and sort', t => {
const k = asKey(withX5C)
const ks = new KeyStore(k)
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export namespace JWKS {
x5t?: string;
'x5t#S256'?: string;
crv?: string;
thumbprint?: string;
}

class KeyStore {
Expand Down

0 comments on commit a9f6f71

Please sign in to comment.