Skip to content

Commit 5903baf

Browse files
masl2nhsd-david-wass
authored andcommitted
CCM-11171: simplify the authorizer lambda
1 parent 2e8e2f3 commit 5903baf

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

lambdas/authorizer/src/__tests__/index.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { APIGatewayRequestAuthorizerEvent, Callback, Context } from 'aws-lambda';
12
import { handler } from '../index';
2-
import { APIGatewayRequestAuthorizerEvent, Context, Callback } from 'aws-lambda';
33

44
describe('Authorizer Lambda Function', () => {
55
let mockEvent: APIGatewayRequestAuthorizerEvent;
@@ -81,4 +81,24 @@ describe('Authorizer Lambda Function', () => {
8181
}),
8282
}));
8383
});
84+
85+
it('Should handle additional headers correctly', () => {
86+
mockEvent.headers = {
87+
headerauth1: 'headervalue1' ,
88+
otherheader1: 'headervalue2',
89+
otherheader2: 'headervalue3'
90+
};
91+
92+
handler(mockEvent, mockContext, mockCallback);
93+
94+
expect(mockCallback).toHaveBeenCalledWith(null, expect.objectContaining({
95+
policyDocument: expect.objectContaining({
96+
Statement: expect.arrayContaining([
97+
expect.objectContaining({
98+
Effect: 'Allow',
99+
}),
100+
]),
101+
}),
102+
}));
103+
});
84104
});

lambdas/authorizer/src/index.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,28 @@
1111

1212
// See https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html for the original JS documentation
1313

14-
import { APIGatewayRequestAuthorizerEvent, Context, Callback, APIGatewayAuthorizerResult } from 'aws-lambda';
14+
import { APIGatewayAuthorizerResult, APIGatewayRequestAuthorizerEvent, Callback, Context } from 'aws-lambda';
15+
import pino from 'pino';
1516

1617
export const handler = (
1718
event: APIGatewayRequestAuthorizerEvent,
1819
context: Context,
19-
callback: Callback<APIGatewayAuthorizerResult>
20+
callback: Callback<APIGatewayAuthorizerResult>,
21+
log = pino()
2022
): void => {
21-
console.log('Received event:', JSON.stringify(event, null, 2));
23+
log.info(event, 'Received event');
2224

2325
const headers = event.headers || {};
24-
const tmp = event.methodArn.split(':');
25-
const apiGatewayArnTmp = tmp[5].split('/');
26-
const awsAccountId = tmp[4];
27-
const region = tmp[3];
28-
const restApiId = apiGatewayArnTmp[0];
29-
const stage = apiGatewayArnTmp[1];
30-
const method = apiGatewayArnTmp[2];
31-
let resource = '/'; // root resource
32-
33-
if (apiGatewayArnTmp[3]) {
34-
resource += apiGatewayArnTmp[3];
35-
}
3626

3727
// Perform authorization to return the Allow policy for correct parameters and
3828
// the 'Unauthorized' error, otherwise.
3929
if (
4030
headers['headerauth1'] === 'headervalue1'
4131
) {
32+
log.info('Allow event');
4233
callback(null, generateAllow('me', event.methodArn));
4334
} else {
35+
log.info('Deny event');
4436
callback(null, generateDeny('me', event.methodArn));
4537
}
4638
};

0 commit comments

Comments
 (0)