UNPKG

jwks-client

Version:

Library to retrieve public keys from a JWKS endpoint

30 lines (26 loc) 817 B
import ms from 'ms'; import memoizer from 'lru-memoizer'; import debug from 'debug'; export default function(client, { cacheMaxEntries = 5, cacheMaxAge = '10h' }) { const logger = debug('jwks'); const getSigningKey = client.getSigningKey; let cacheAge = 3600000; if (typeof cacheMaxAge === 'string' || cacheMaxAge instanceof String) { cacheAge = ms(cacheMaxAge); } logger(`Configured caching of signing keys. Max: ${cacheMaxEntries} / Age: ${cacheAge}`); return memoizer({ load: (kid, callback) => { getSigningKey(kid, (err, key) => { if (err) { return callback(err); } logger(`Caching signing key for '${kid}':`, key); return callback(null, key); }); }, hash: (kid) => kid, maxAge: cacheAge, max: cacheMaxEntries }); }