Skip to content

Commit 621b4c5

Browse files
authored
Merge pull request #6 from bskim45/invoke_local_auth
Add support for python custom auth handler
2 parents 06e1fcf + 6a71b81 commit 621b4c5

File tree

4 files changed

+13
-8
lines changed

4 files changed

+13
-8
lines changed

src/createAuthScheme.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const utils = require('./utils');
99
const _ = require('lodash');
1010
const authCanExecuteResource = require('./authCanExecuteResource');
1111

12-
module.exports = function createAuthScheme(authFun, authorizerOptions, funName, endpointPath, options, serverlessLog, servicePath, serverless) {
12+
module.exports = function createAuthScheme(authFun, authorizerOptions, funName, endpointPath, options, serverlessLog, servicePath, serverless, serviceRuntime) {
1313
const authFunName = authorizerOptions.name;
1414

1515
let identityHeader = 'authorization';
@@ -21,8 +21,10 @@ module.exports = function createAuthScheme(authFun, authorizerOptions, funName,
2121
}
2222
identityHeader = identitySourceMatch[1].toLowerCase();
2323
}
24-
25-
const funOptions = functionHelper.getFunctionOptions(authFun, funName, servicePath);
24+
25+
// HACK: since handlerName is used to invoke python function locally,
26+
// we have to pass the authFunName instead of funcName
27+
const funOptions = functionHelper.getFunctionOptions(authFun, utils.isPythonRuntime(serviceRuntime) ? authFunName : funName, servicePath, serviceRuntime);
2628

2729
// Create Auth Scheme
2830
return () => ({

src/functionHelper.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const fork = require('child_process').fork;
66
const _ = require('lodash');
77
const path = require('path');
88
const uuid = require('uuid/v4');
9+
const utils = require('./utils');
910

1011
const handlerCache = {};
1112
const messageCallbacks = {};
@@ -33,7 +34,7 @@ function runPythonHandler(funOptions, options) {
3334
} else {
3435
// Search for the start of the JSON result
3536
// https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format
36-
const match = /{[\r\n]\s+"isBase64Encoded"|{[\r\n]\s+"statusCode"|{[\r\n]\s+"headers"|{[\r\n]\s+"body"/.exec(str);
37+
const match = /{[\r\n]\s+"isBase64Encoded"|{[\r\n]\s+"statusCode"|{[\r\n]\s+"headers"|{[\r\n]\s+"body"|{[\r\n]\s+"principalId"/.exec(str);
3738
if (match && match.index > -1) {
3839
// The JSON result was in this chunk so slice it out
3940
hasDetectedJson = true;
@@ -160,7 +161,7 @@ module.exports = {
160161
}
161162
let user_python = true
162163
let handler = null;
163-
if (['python2.7', 'python3.6'].indexOf(funOptions['serviceRuntime']) !== -1){
164+
if (utils.isPythonRuntime(funOptions['serviceRuntime'])) {
164165
handler = runPythonHandler(funOptions, options)
165166
} else {
166167
debugLog(`Loading handler... (${funOptions.handlerPath})`);

src/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ class Offline {
384384
this.serverlessLog(`${method} ${fullPath}`);
385385

386386
// If the endpoint has an authorization function, create an authStrategy for the route
387-
const authStrategyName = this.options.noAuth ? null : this._configureAuthorization(endpoint, funName, method, epath, servicePath);
387+
const authStrategyName = this.options.noAuth ? null : this._configureAuthorization(endpoint, funName, method, epath, servicePath, serviceRuntime);
388388

389389
let cors = null;
390390
if (endpoint.cors) {
@@ -805,7 +805,7 @@ class Offline {
805805
});
806806
}
807807

808-
_configureAuthorization(endpoint, funName, method, epath, servicePath) {
808+
_configureAuthorization(endpoint, funName, method, epath, servicePath, serviceRuntime) {
809809
let authStrategyName = null;
810810
if (endpoint.authorizer) {
811811
let authFunctionName = endpoint.authorizer;
@@ -863,7 +863,8 @@ class Offline {
863863
this.options,
864864
this.serverlessLog,
865865
servicePath,
866-
this.serverless
866+
this.serverless,
867+
serviceRuntime
867868
);
868869

869870
// Set the auth scheme and strategy on the server

src/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ module.exports = {
1717
// Detect the toString encoding from the request headers content-type
1818
// enhance if further content types need to be non utf8 encoded.
1919
detectEncoding: request => _.includes(request.headers['content-type'], 'multipart/form-data') ? 'binary' : 'utf8',
20+
isPythonRuntime: runtime => runtime.startsWith('python')
2021
};

0 commit comments

Comments
 (0)