Skip to content

Instantly share code, notes, and snippets.

@pahud
Created May 20, 2019 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pahud/19a0e2d4fad547b9a2f9b140d729b699 to your computer and use it in GitHub Desktop.
Save pahud/19a0e2d4fad547b9a2f9b140d729b699 to your computer and use it in GitHub Desktop.
APIG custom authorizer for header validation
exports.handler = (event, context, callback) => {
console.log(event)
var token = event.authorizationToken.toLowerCase();
switch (token) {
case 'dalvik':
callback(null, generatePolicy('user', 'Allow', event.methodArn, token));
break;
// case 'deny':
// callback(null, generatePolicy('user', 'Deny', event.methodArn));
// break;
// case 'unauthorized':
// callback("Unauthorized"); // Return a 401 Unauthorized response
// break;
// default:
// callback("Error: Invalid User-Agent"); // Return a 500 Invalid token response
default:
callback(null, generatePolicy('user', 'Deny', event.methodArn));
break;
//callback("Unauthorized");
};
};
// Help function to generate an IAM policy
var generatePolicy = (principalId, effect, resource, token) => {
var authResponse = {};
authResponse.principalId = principalId;
if (effect && resource) {
var policyDocument = {};
policyDocument.Version = '2012-10-17';
policyDocument.Statement = [];
var statementOne = {};
statementOne.Action = 'execute-api:Invoke';
statementOne.Effect = effect;
statementOne.Resource = resource;
policyDocument.Statement[0] = statementOne;
authResponse.policyDocument = policyDocument;
}
// Optional output with custom properties of the String, Number or Boolean type.
authResponse.context = {
"UserAgent": token
// "stringKey": "stringval",
// "numberKey": 123,
// "booleanKey": true
};
return authResponse;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment