Skip to content

Commit 12af1b5

Browse files
authored
Merge pull request dherault#241 from garunski/master
Added ignore the authorizer for the non custom authorizers.
2 parents f6c0f42 + 06b6302 commit 12af1b5

File tree

3 files changed

+97
-55
lines changed

3 files changed

+97
-55
lines changed

manual_test/handler.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@ module.exports.hello = (event, context, callback) => {
1212
callback(null, response);
1313
};
1414

15+
module.exports.authFunction = (event, context, callback) => {
16+
context.succeed({
17+
principalId: 'xxxxxxx', // the principal user identification associated with the token send by the client
18+
policyDocument: {
19+
// example policy shown below, but this value is any valid policy
20+
Version: '2012-10-17',
21+
Statement: [
22+
{
23+
Effect: 'Allow',
24+
Action: ['execute-api:Invoke'],
25+
Resource: [event.methodArn],
26+
},
27+
],
28+
},
29+
});
30+
};
31+
1532
module.exports.hello500 = (event, context, callback) => {
1633
const response = {
1734
statusCode: 500,

manual_test/serverless.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,29 @@ functions:
5959
path: hello
6060
method: post
6161

62+
helloAuthorizerWithArn:
63+
handler: handler.hello
64+
events:
65+
- http:
66+
path: helloAuthorizerWithArn
67+
method: get
68+
authorizer:
69+
arn: sometest
70+
71+
helloAuthorizerWithFunctionName:
72+
handler: handler.hello
73+
events:
74+
- http:
75+
path: helloAuthorizerWithCustomFunction
76+
method: get
77+
authorizer:
78+
name: authFunction
79+
identitySource: method.request.header.Authorization #Required for serverless-offline, although it's a default
80+
resultTtlInSeconds: 0 #prevents caching the authorizer
81+
82+
authFunction:
83+
handler: handler.authFunction
84+
6285
hello500:
6386
handler: handler.hello500
6487
events:

src/index.js

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -370,61 +370,7 @@ class Offline {
370370
this.serverlessLog(`${method} ${fullPath}`);
371371

372372
// If the endpoint has an authorization function, create an authStrategy for the route
373-
let authStrategyName = null;
374-
375-
if (endpoint.authorizer) {
376-
let authFunctionName = endpoint.authorizer;
377-
if (typeof endpoint.authorizer === 'object') {
378-
if (endpoint.authorizer.arn) {
379-
this.serverlessLog(`WARNING: Serverless Offline does not support non local authorizers: ${endpoint.authorizer.arn}`);
380-
381-
return;
382-
}
383-
authFunctionName = endpoint.authorizer.name;
384-
}
385-
386-
this.serverlessLog(`Configuring Authorization: ${endpoint.path} ${authFunctionName}`);
387-
388-
const authFunction = this.service.getFunction(authFunctionName);
389-
390-
if (!authFunction) return this.serverlessLog(`WARNING: Authorization function ${authFunctionName} does not exist`);
391-
392-
const authorizerOptions = {};
393-
394-
// serverless 1.x will create default values, so we will to
395-
authorizerOptions.resultTtlInSeconds = '300';
396-
authorizerOptions.identitySource = 'method.request.header.Authorization';
397-
398-
if (typeof endpoint.authorizer === 'string') {
399-
authorizerOptions.name = authFunctionName;
400-
}
401-
else {
402-
Object.assign(authorizerOptions, endpoint.authorizer);
403-
}
404-
405-
// Create a unique scheme per endpoint
406-
// This allows the methodArn on the event property to be set appropriately
407-
const authKey = `${funName}-${authFunctionName}-${method}-${epath}`;
408-
const authSchemeName = `scheme-${authKey}`;
409-
authStrategyName = `strategy-${authKey}`; // set strategy name for the route config
410-
411-
debugLog(`Creating Authorization scheme for ${authKey}`);
412-
413-
// Create the Auth Scheme for the endpoint
414-
const scheme = createAuthScheme(
415-
authFunction,
416-
authorizerOptions,
417-
funName,
418-
epath,
419-
this.options,
420-
this.serverlessLog,
421-
servicePath
422-
);
423-
424-
// Set the auth scheme and strategy on the server
425-
this.server.auth.scheme(authSchemeName, scheme);
426-
this.server.auth.strategy(authStrategyName, authSchemeName);
427-
}
373+
let authStrategyName = this._configureAuthorization(endpoint, funName, method, epath, servicePath);
428374

429375
let cors = null;
430376
if (endpoint.cors) {
@@ -781,6 +727,62 @@ class Offline {
781727
});
782728
}
783729

730+
_configureAuthorization(endpoint, funName, method, epath, servicePath) {
731+
let authStrategyName = null;
732+
if (endpoint.authorizer) {
733+
let authFunctionName = endpoint.authorizer;
734+
if (typeof endpoint.authorizer === 'object') {
735+
if (endpoint.authorizer.arn) {
736+
this.serverlessLog(`WARNING: Serverless Offline does not support non local authorizers: ${endpoint.authorizer.arn}`);
737+
738+
return authStrategyName;
739+
}
740+
authFunctionName = endpoint.authorizer.name;
741+
}
742+
743+
this.serverlessLog(`Configuring Authorization: ${endpoint.path} ${authFunctionName}`);
744+
745+
const authFunction = this.service.getFunction(authFunctionName);
746+
747+
if (!authFunction) return this.serverlessLog(`WARNING: Authorization function ${authFunctionName} does not exist`);
748+
749+
let authorizerOptions = {};
750+
authorizerOptions.resultTtlInSeconds = '300';
751+
authorizerOptions.identitySource = 'method.request.header.Authorization';
752+
753+
if (typeof endpoint.authorizer === 'string') {
754+
authorizerOptions.name = authFunctionName;
755+
}
756+
else {
757+
Object.assign(authorizerOptions, endpoint.authorizer);
758+
}
759+
760+
// Create a unique scheme per endpoint
761+
// This allows the methodArn on the event property to be set appropriately
762+
const authKey = `${funName}-${authFunctionName}-${method}-${epath}`;
763+
const authSchemeName = `scheme-${authKey}`;
764+
authStrategyName = `strategy-${authKey}`; // set strategy name for the route config
765+
766+
debugLog(`Creating Authorization scheme for ${authKey}`);
767+
768+
// Create the Auth Scheme for the endpoint
769+
const scheme = createAuthScheme(
770+
authFunction,
771+
authorizerOptions,
772+
funName,
773+
epath,
774+
this.options,
775+
this.serverlessLog,
776+
servicePath
777+
);
778+
779+
// Set the auth scheme and strategy on the server
780+
this.server.auth.scheme(authSchemeName, scheme);
781+
this.server.auth.strategy(authStrategyName, authSchemeName);
782+
}
783+
return authStrategyName;
784+
}
785+
784786
// All done, we can listen to incomming requests
785787
_listen() {
786788
return new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)