feat: allow JWKS.asKeyStore to swallow errors

This commit is contained in:
Filip Skokan 2019-08-24 22:03:37 +02:00
parent 5fad8cc839
commit 78398d3cdf
3 changed files with 17 additions and 3 deletions

View file

@ -692,6 +692,8 @@ Creates a new KeyStore from a JSON Web Key Set.
- `jwks`: `<Object>` JWKS formatted object (`{ keys: [{ kty: '...', ... }, ...] }`)
- `options`: `<Object>`
- `ignoreErrors`: `<boolean>` **Default** 'false'. This will make it so that keys
unsupported by your Node.js runtime version (or otherwise faulty keys) get swallowed.
- `calculateMissingRSAPrimes`: `<boolean>` **Default** 'false'. This option is really only in
effect when the JWKS contains private RSA JWK keys, by default, keys without the optimization
private key parameters (p, q, dp, dq, qi) won't imported because their calculation is heavy and

6
lib/index.d.ts vendored
View file

@ -208,7 +208,11 @@ export namespace JWKS {
static fromJWKS(jwks: JSONWebKeySet): KeyStore
}
export function asKeyStore(jwks: JSONWebKeySet, options?: ImportOptions): KeyStore
interface JWKSImportOptions extends ImportOptions {
ignoreErrors?: boolean
}
export function asKeyStore(jwks: JSONWebKeySet, options?: JWKSImportOptions): KeyStore
}
export namespace JWS {

View file

@ -168,12 +168,20 @@ class KeyStore {
}
}
function asKeyStore (jwks, { calculateMissingRSAPrimes = false } = {}) {
function asKeyStore (jwks, { ignoreErrors = false, calculateMissingRSAPrimes = false } = {}) {
if (!isObject(jwks) || !Array.isArray(jwks.keys) || jwks.keys.some(k => !isObject(k) || !('kty' in k))) {
throw new TypeError('jwks must be a JSON Web Key Set formatted object')
}
const keys = jwks.keys.map((jwk) => importKey(jwk, { calculateMissingRSAPrimes }))
const keys = jwks.keys.map((jwk) => {
try {
return importKey(jwk, { calculateMissingRSAPrimes })
} catch (err) {
if (!ignoreErrors) {
throw err
}
}
}).filter(Boolean)
return new KeyStore(...keys)
}