Skip to content

Commit

Permalink
Merge pull request #817 from jormaechea/Issue-816-Authorizer-env-var-…
Browse files Browse the repository at this point in the history
…in-lambda-integration-v5

Added support for AUTHORIZER env var in lambda integration (v5)
  • Loading branch information
dnalborczyk committed Sep 30, 2019
2 parents 40aaaa8 + f0f8302 commit 428d138
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/createVelocityContext.js
Expand Up @@ -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);
Expand All @@ -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

Expand Down

0 comments on commit 428d138

Please sign in to comment.