From c0873aa799e736903c27761c367e4482ec8e311c Mon Sep 17 00:00:00 2001 From: Valerio Mazzeo Date: Fri, 24 May 2019 15:35:43 +0200 Subject: [PATCH] Global client Both `cache` and `rateLimit` options are worthless if the client is recreated for each lambda execution. Due to the stateless natura of lambda functions, this is not able to ensure proper caching, but at least on hot starts the client will be reused for each lambda container which is better than nothing. Am I missing anything? Is the client cache somehow working even when recreated each time? --- lib.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib.js b/lib.js index 37b7d3d..c1d2aa1 100644 --- a/lib.js +++ b/lib.js @@ -40,6 +40,13 @@ const jwtOptions = { issuer: process.env.TOKEN_ISSUER }; +const client = jwksClient({ + cache: true, + rateLimit: true, + jwksRequestsPerMinute: 10, // Default value + jwksUri: process.env.JWKS_URI +}); + module.exports.authenticate = (params) => { console.log(params); const token = getToken(params); @@ -49,13 +56,6 @@ module.exports.authenticate = (params) => { throw new Error('invalid token'); } - const client = jwksClient({ - cache: true, - rateLimit: true, - jwksRequestsPerMinute: 10, // Default value - jwksUri: process.env.JWKS_URI - }); - const getSigningKey = util.promisify(client.getSigningKey); return getSigningKey(decoded.header.kid) .then((key) => {