Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for AUTHORIZER env var in lambda integration (v5) #817

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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