hooking the right authorizers

This commit is contained in:
Eslam A. Hefnawy 2016-07-20 19:07:31 +09:00
parent d7ee706496
commit 29f7af0fa2
2 changed files with 42 additions and 46 deletions

View File

@ -5,43 +5,42 @@ const _ = require('lodash');
module.exports = {
compileAuthorizers() {
_.forEach(this.serverless.service.functions, (functionObject) => {
_.forEach(this.serverless.service.functions, (functionObject, functionName) => {
functionObject.events.forEach(event => {
if (event.http && event.http.authorizer) {
let resultTtlInSeconds;
let functionName;
let identitySource;
const serviceName = this.serverless.service.service;
const stageName = this.options.stage;
if (event.http && event.http.authorizers) {
event.http.authorizers.forEach(authorizer => {
let resultTtlInSeconds;
let authorizerName;
let identitySource;
const extractedResourceId = this.resourceLogicalIds[event.http.path].match(/\d+$/)[0];
const normalizedMethod = event.http.method[0].toUpperCase() +
event.http.method.substr(1).toLowerCase();
const extractedResourceId = this.resourceLogicalIds[event.http.path].match(/\d+$/)[0];
const normalizedMethod = event.http.method[0].toUpperCase() +
event.http.method.substr(1).toLowerCase();
if (typeof event.http.authorizer === 'string') {
functionName = event.http.authorizer;
resultTtlInSeconds = '300';
identitySource = 'method.request.header.Auth';
} else if (typeof event.http.authorizer === 'object') {
functionName = event.http.authorizer.name;
resultTtlInSeconds = event.http.authorizer.resultTtlInSeconds || '300';
identitySource = event.http.authorizer.identitySource || 'method.request.header.Auth';
} else {
const errorMessage = [
`Authorizer property in function ${functionName} is not an object nor a string.`,
' The correct syntax is: authorizer: functionName',
' OR an object with "name" property.',
' Please check the docs for more info.',
].join('');
throw new this.serverless.classes
.Error(errorMessage);
}
if (typeof authorizer === 'string') {
authorizerName = authorizer;
resultTtlInSeconds = '300';
identitySource = 'method.request.header.Auth';
} else if (typeof authorizer === 'object') {
authorizerName = authorizer.name;
resultTtlInSeconds = authorizer.resultTtlInSeconds || '300';
identitySource = authorizer.identitySource || 'method.request.header.Auth';
} else {
const errorMessage = [
`Authorizer item in function ${functionName} is not an object nor a string.`,
' Please make sure each authorizer in the "authorizers"',
' array is a string or an object.',
' Please check the docs for more info.',
].join('');
throw new this.serverless.classes
.Error(errorMessage);
}
// validate referenced authorizer
// function exists in service
this.serverless.service.getFunction(functionName);
// validate referenced authorizer
// function exists in service
this.serverless.service.getFunction(authorizerName);
const authorizerTemplate = `
const authorizerTemplate = `
{
"Type" : "AWS::ApiGateway::Authorizer",
"Properties" : {
@ -50,24 +49,24 @@ module.exports = {
"arn:aws:apigateway:",
{"Ref" : "AWS::Region"},
":lambda:path/2015-03-31/functions/",
{"Fn::GetAtt" : ["${serviceName}-${
stageName}-${functionName}", "Arn"]}, "/invocations"
{"Fn::GetAtt" : ["${authorizerName}", "Arn"]}, "/invocations"
]]},
"IdentitySource" : "${identitySource}",
"Name" : "${event.http.method} ${event.http.path} Authorizer",
"Name" : "${authorizerName}",
"RestApiId" : { "Ref": "RestApiApigEvent" },
"Type" : "TOKEN"
}
}
`;
const authorizerObject = {
[`${normalizedMethod}MethodApigEvent${extractedResourceId}Authorizer`]:
JSON.parse(authorizerTemplate),
};
const authorizerObject = {
[`${authorizerName}Authorizer`]:
JSON.parse(authorizerTemplate),
};
_.merge(this.serverless.service.resources.Resources,
authorizerObject);
_.merge(this.serverless.service.resources.Resources,
authorizerObject);
});
}
});
});

View File

@ -118,16 +118,13 @@ module.exports = {
// set authorizer config if available
if (event.http.authorizer) {
const AuthorizerLogicalId = `${normalizedMethod}MethodApigEvent${
extractedResourceId}Authorizer`;
const AuthorizerLogicalId = `${authorizerName}Authorizer`;
methodTemplateJson.Properties.AuthorizationType = 'CUSTOM';
methodTemplateJson.Properties.AuthorizerId = {
Ref: AuthorizerLogicalId,
};
methodTemplateJson.DependsOn = {
Ref: AuthorizerLogicalId,
};
methodTemplateJson.DependsOn = AuthorizerLogicalId;
}
const methodObject = {