Skip to content

Commit d8fd826

Browse files
committed
Added ignore the authorizer for the non custom authorizers.
The endpoints still work as is even tho the authorizer is not supported. Added manual tests for the ARN authorizer and custom function authorizer.
1 parent edf458b commit d8fd826

File tree

3 files changed

+99
-55
lines changed

3 files changed

+99
-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: 59 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-
let authorizerOptions = {};
393-
if (typeof endpoint.authorizer === 'string') {
394-
// serverless 1.x will create default values, so we will to
395-
authorizerOptions.name = authFunctionName;
396-
authorizerOptions.resultTtlInSeconds = '300';
397-
authorizerOptions.identitySource = 'method.request.header.Authorization';
398-
}
399-
else {
400-
authorizerOptions.identitySource = endpoint.authorizer.identitySource ||
401-
'method.request.header.Authorization'; // See #207
402-
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,64 @@ 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+
if (typeof endpoint.authorizer === 'string') {
751+
// serverless 1.x will create default values, so we will to
752+
authorizerOptions.name = authFunctionName;
753+
authorizerOptions.resultTtlInSeconds = '300';
754+
authorizerOptions.identitySource = 'method.request.header.Authorization';
755+
}
756+
else {
757+
authorizerOptions.identitySource = endpoint.authorizer.identitySource ||
758+
'method.request.header.Authorization'; // See #207
759+
authorizerOptions = endpoint.authorizer;
760+
}
761+
762+
// Create a unique scheme per endpoint
763+
// This allows the methodArn on the event property to be set appropriately
764+
const authKey = `${funName}-${authFunctionName}-${method}-${epath}`;
765+
const authSchemeName = `scheme-${authKey}`;
766+
authStrategyName = `strategy-${authKey}`; // set strategy name for the route config
767+
768+
debugLog(`Creating Authorization scheme for ${authKey}`);
769+
770+
// Create the Auth Scheme for the endpoint
771+
const scheme = createAuthScheme(
772+
authFunction,
773+
authorizerOptions,
774+
funName,
775+
epath,
776+
this.options,
777+
this.serverlessLog,
778+
servicePath
779+
);
780+
781+
// Set the auth scheme and strategy on the server
782+
this.server.auth.scheme(authSchemeName, scheme);
783+
this.server.auth.strategy(authStrategyName, authSchemeName);
784+
}
785+
return authStrategyName;
786+
}
787+
784788
// All done, we can listen to incomming requests
785789
_listen() {
786790
return new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)