Skip to content

Commit

Permalink
Added support for AUTHORIZER env var in lambda integration
Browse files Browse the repository at this point in the history
Fixes dherault#816 for v5
  • Loading branch information
jormaechea committed Sep 24, 2019
1 parent 40aaaa8 commit f0f8302
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 f0f8302

Please sign in to comment.