|
11 | 11 |
|
12 | 12 | // See https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html for the original JS documentation |
13 | 13 |
|
14 | | -import { APIGatewayRequestAuthorizerEvent, Context, Callback, APIGatewayAuthorizerResult } from 'aws-lambda'; |
| 14 | +import { APIGatewayAuthorizerResult, APIGatewayRequestAuthorizerEvent, Callback, Context } from 'aws-lambda'; |
| 15 | +import pino from 'pino'; |
15 | 16 |
|
16 | 17 | export const handler = ( |
17 | 18 | event: APIGatewayRequestAuthorizerEvent, |
18 | 19 | context: Context, |
19 | | - callback: Callback<APIGatewayAuthorizerResult> |
| 20 | + callback: Callback<APIGatewayAuthorizerResult>, |
| 21 | + log = pino() |
20 | 22 | ): void => { |
21 | | - console.log('Received event:', JSON.stringify(event, null, 2)); |
| 23 | + log.info(event, 'Received event'); |
22 | 24 |
|
23 | 25 | 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 | | - } |
36 | 26 |
|
37 | 27 | // Perform authorization to return the Allow policy for correct parameters and |
38 | 28 | // the 'Unauthorized' error, otherwise. |
39 | 29 | if ( |
40 | 30 | headers['headerauth1'] === 'headervalue1' |
41 | 31 | ) { |
| 32 | + log.info('Allow event'); |
42 | 33 | callback(null, generateAllow('me', event.methodArn)); |
43 | 34 | } else { |
| 35 | + log.info('Deny event'); |
44 | 36 | callback(null, generateDeny('me', event.methodArn)); |
45 | 37 | } |
46 | 38 | }; |
|
0 commit comments