Skip to content

Commit

Permalink
feat: add lambda proxy authorizer enhancedAuthContext
Browse files Browse the repository at this point in the history
  • Loading branch information
frsechet committed Aug 11, 2019
1 parent 891dfba commit 5b0bea0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/config/offline-default.req.vm
Expand Up @@ -17,6 +17,8 @@
"query": $loop,
#set( $map = $input.params().path )
"path": $loop,
#set( $map = $context.enhancedAuthContext )
"enhancedAuthContext": $loop,
#set( $map = $context.identity )
"identity": $loop,
#set( $map = $stageVariables )
Expand Down
13 changes: 11 additions & 2 deletions src/createAuthScheme.js
Expand Up @@ -132,6 +132,8 @@ module.exports = function createAuthScheme(

event.methodArn = `arn:aws:execute-api:${options.region}:${accountId}:${apiId}/${options.stage}/${httpMethod}${resourcePath}`;

event.enhancedAuthContext = {};

event.requestContext = {
accountId,
apiId,
Expand Down Expand Up @@ -217,7 +219,13 @@ module.exports = function createAuthScheme(

serverlessLog(
`Authorization function returned a successful response: (λ: ${authFunName})`,
)
);

const enhancedAuthContext = {
principalId: policy.principalId,
integrationLatency: '42',
...policy.context,
};

// Set the credentials for the rest of the pipeline
return resolve(
Expand All @@ -226,9 +234,10 @@ module.exports = function createAuthScheme(
context: policy.context,
usageIdentifierKey: policy.usageIdentifierKey,
user: policy.principalId,
enhancedAuthContext,
},
}),
)
);
};

if (result && typeof result.then === 'function') {
Expand Down
13 changes: 9 additions & 4 deletions src/createVelocityContext.js
Expand Up @@ -28,6 +28,10 @@ module.exports = function createVelocityContext(request, options, payload) {
const path = (x) => jsonPath(payload || {}, x);
const authPrincipalId =
request.auth && request.auth.credentials && request.auth.credentials.user;
const enhancedAuthContext =
request.auth &&
request.auth.credentials &&
request.auth.credentials.enhancedAuthContext;
const headers = request.unprocessedHeaders;

let token = headers && (headers.Authorization || headers.authorization);
Expand Down Expand Up @@ -56,6 +60,7 @@ module.exports = function createVelocityContext(request, options, payload) {
'offlineContext_authorizer_principalId', // See #24
claims,
},
enhancedAuthContext: enhancedAuthContext || {},
httpMethod: request.method.toUpperCase(),
identity: {
accountId: 'offlineContext_accountId',
Expand All @@ -82,10 +87,10 @@ module.exports = function createVelocityContext(request, options, payload) {
typeof x === 'string'
? request.params[x] || request.query[x] || headers[x]
: {
header: headers,
path: Object.assign({}, request.params),
querystring: Object.assign({}, request.query),
},
header: headers,
path: Object.assign({}, request.params),
querystring: Object.assign({}, request.query),
},
},
stageVariables: options.stageVariables,
util: {
Expand Down

0 comments on commit 5b0bea0

Please sign in to comment.