Skip to content

Commit a7ed5e8

Browse files
OrKoNdaniel-cottone
authored andcommitted
Improves support authorizer.type == request (dherault#377)
1 parent 56de04c commit a7ed5e8

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

src/createAuthScheme.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ const _ = require('lodash');
1111
module.exports = function createAuthScheme(authFun, authorizerOptions, funName, endpointPath, options, serverlessLog, servicePath, serverless) {
1212
const authFunName = authorizerOptions.name;
1313

14-
const identitySourceMatch = /^method.request.header.((?:\w+-?)+\w+)$/.exec(authorizerOptions.identitySource);
15-
if (!identitySourceMatch || identitySourceMatch.length !== 2) {
16-
throw new Error(`Serverless Offline only supports retrieving tokens from the headers (λ: ${authFunName})`);
14+
let identityHeader = 'authorization';
15+
16+
if (authorizerOptions.type !== 'request') {
17+
const identitySourceMatch = /^method.request.header.((?:\w+-?)+\w+)$/.exec(authorizerOptions.identitySource);
18+
if (!identitySourceMatch || identitySourceMatch.length !== 2) {
19+
throw new Error(`Serverless Offline only supports retrieving tokens from the headers (λ: ${authFunName})`);
20+
}
21+
identityHeader = identitySourceMatch[1].toLowerCase();
1722
}
18-
19-
const identityHeader = identitySourceMatch[1].toLowerCase();
23+
2024
const funOptions = functionHelper.getFunctionOptions(authFun, funName, servicePath);
2125

2226
// Create Auth Scheme
@@ -28,9 +32,6 @@ module.exports = function createAuthScheme(authFun, authorizerOptions, funName,
2832

2933
// Get Authorization header
3034
const req = request.raw.req;
31-
const authorization = req.headers[identityHeader];
32-
33-
debugLog(`Retrieved ${identityHeader} header ${authorization}`);
3435

3536
// Get path params
3637
const pathParams = {};
@@ -49,12 +50,14 @@ module.exports = function createAuthScheme(authFun, authorizerOptions, funName,
4950
type: 'REQUEST',
5051
path: request.path,
5152
httpMethod: request.method.toUpperCase(),
52-
headers: request.headers,
53+
headers: Object.assign(request.headers, utils.capitalizeKeys(request.headers)),
5354
pathParameters: utils.nullIfEmpty(pathParams),
5455
queryStringParameters: utils.nullIfEmpty(request.query),
5556
};
5657
}
5758
else {
59+
const authorization = req.headers[identityHeader];
60+
debugLog(`Retrieved ${identityHeader} header ${authorization}`);
5861
event = {
5962
type: 'TOKEN',
6063
authorizationToken: authorization,

src/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -716,9 +716,9 @@ class Offline {
716716

717717
Object.assign(response.headers, defaultHeaders, result.headers);
718718
if (!_.isUndefined(result.body)) {
719-
if(result.isBase64Encoded) {
719+
if (result.isBase64Encoded) {
720720
response.encoding = 'binary';
721-
response.source = new Buffer(result.body,'base64');
721+
response.source = new Buffer(result.body, 'base64');
722722
response.variety = 'buffer';
723723
}
724724
else {
@@ -874,8 +874,9 @@ class Offline {
874874
this.serverlessLog(message);
875875
if (stackTrace && stackTrace.length > 0) {
876876
console.log(stackTrace);
877-
} else {
878-
console.log(err)
877+
}
878+
else {
879+
console.log(err);
879880
}
880881

881882
/* eslint-disable no-param-reassign */

0 commit comments

Comments
 (0)