From f0f8302722936a9d828521ab4e56d202401327cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20Ormaechea?= Date: Tue, 24 Sep 2019 19:55:07 -0300 Subject: [PATCH] Added support for AUTHORIZER env var in lambda integration Fixes #816 for v5 --- src/createVelocityContext.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/createVelocityContext.js b/src/createVelocityContext.js index ac8030c23..9a146a9d4 100644 --- a/src/createVelocityContext.js +++ b/src/createVelocityContext.js @@ -27,9 +27,23 @@ function escapeJavaScript(x) { module.exports = function createVelocityContext(request, options, payload) { const path = (x) => jsonPath(payload || {}, x); const authPrincipalId = request.auth && request.auth.credentials && request.auth.credentials.principalId; - let authorizer = request.auth - && request.auth.credentials - && request.auth.credentials.authorizer; + + let authorizer; + + if (process.env.AUTHORIZER) { + try { + authorizer = JSON.parse(process.env.AUTHORIZER); + } catch (error) { + console.error( + 'Serverless-offline: Could not parse process.env.AUTHORIZER, make sure it is correct JSON.', + ); + } + } else { + authorizer = request.auth + && request.auth.credentials + && request.auth.credentials.authorizer; + } + const headers = request.unprocessedHeaders; let token = headers && (headers.Authorization || headers.authorization); @@ -39,7 +53,8 @@ module.exports = function createVelocityContext(request, options, payload) { } if (!authorizer) authorizer = {}; - authorizer.principalId = authPrincipalId + authorizer.principalId = authorizer.principalId + || authPrincipalId || process.env.PRINCIPAL_ID || 'offlineContext_authorizer_principalId'; // See #24