I am using a new API gateway function with Lambda functions to use custom autostart ( https://docs.aws.amazon.com/apigateway/latest/developerguide/use-custom-authorizer.html ).
The authorizer uses JWT tokens to verify the token for the current context and user areas (s). Everything works fine, but there is one concept regarding AWS policies that I cannot understand from the documentation.
The result of the Custom Authorizer function must be an object containing two things:
principalId - policyDocument - a valid policy document with instructions that allows authorized access to Lambda resources, steps, etc.
Now, examples for user authorizers now display almost arbitrary values ββfor the principalId variable. But if I think correctly, should this principalId be unique to each user? And it may have a user-specific unique value (for example, token.userId or token.email ).
If this is true, then for my code provided below, if the JWT token is not valid, then I do not have access to userId or email , and you do not have a hint on what to set principalId to. I temporarily bind it to user to get something for Deny's policy to ensure that 403 Forbidden answer.
Does anyone have a hint on the best ways to configure principalId for custom autostart?
var jwt = require('jsonwebtoken'); var JWT_SECRET = 'My$ecret!'; exports.handler = function(event, context) { var token = event.authorizationToken; try { var decoded = jwt.verify(token, JWT_SECRET); context.done(null, generatePolicy(decoded.id, 'Allow', 'arn:aws:execute-api:*:*:*')); } catch(ex) { console.error(ex.name + ": " + ex.message); context.done(null, generatePolicy('user', 'Deny', 'arn:aws:execute-api:*:*:*')); } }; function generatePolicy(principalId, effect, resource) { var authResponse = {}; authResponse.principalId = principalId; if (effect && resource) { var policyDocument = {}; policyDocument.Version = '2012-10-17';
amazon-web-services aws-lambda jwt aws-api-gateway
Tom pennetta
source share