1- // A simple request-based authorizer example to demonstrate how to use request
2- // parameters to allow or deny a request. In this example, a request is
3- // authorized if the client-supplied HeaderAuth1 header and stage variable of StageVar1
4- // both match specified values of 'headerValue1' and 'stageValue1', respectively.
5- //
6- // Example curl request (replace <api-url> and <stage> as appropriate):
7- //
8- // curl -H "HeaderAuth1: headerValue1" \
9- // "<api-url>/<stage>/your-resource"
10- //
11-
12- // See https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html for the original JS documentation
13-
14- import { APIGatewayRequestAuthorizerEvent , Context , Callback , APIGatewayAuthorizerResult } from 'aws-lambda' ;
15-
16- export const handler = (
17- event : APIGatewayRequestAuthorizerEvent ,
18- context : Context ,
19- callback : Callback < APIGatewayAuthorizerResult >
20- ) : void => {
21- console . log ( 'Received event:' , JSON . stringify ( event , null , 2 ) ) ;
22-
23- // Retrieve request parameters from the Lambda function input:
24- const headers = event . headers || { } ;
25- const pathParameters = event . pathParameters || { } ;
26- const stageVariables = event . stageVariables || { } ;
27-
28- // Parse the input for the parameter values
29- const tmp = event . methodArn . split ( ':' ) ;
30- const apiGatewayArnTmp = tmp [ 5 ] . split ( '/' ) ;
31- const awsAccountId = tmp [ 4 ] ;
32- const region = tmp [ 3 ] ;
33- const restApiId = apiGatewayArnTmp [ 0 ] ;
34- const stage = apiGatewayArnTmp [ 1 ] ;
35- const method = apiGatewayArnTmp [ 2 ] ;
36- let resource = '/' ; // root resource
37- if ( apiGatewayArnTmp [ 3 ] ) {
38- resource += apiGatewayArnTmp [ 3 ] ;
39- }
40-
41- // Perform authorization to return the Allow policy for correct parameters and
42- // the 'Unauthorized' error, otherwise.
43- if (
44- headers [ 'HeaderAuth1' ] === 'headerValue1' &&
45- stageVariables [ 'StageVar1' ] === 'stageValue1'
46- ) {
47- callback ( null , generateAllow ( 'me' , event . methodArn ) ) ;
48- } else {
49- callback ( null , generateDeny ( 'me' , event . methodArn ) ) ;
50- }
51- } ;
52-
53- // Helper function to generate an IAM policy
54- function generatePolicy (
55- principalId : string ,
56- effect : 'Allow' | 'Deny' ,
57- resource : string
58- ) : APIGatewayAuthorizerResult {
59- // Required output:
60- const authResponse : APIGatewayAuthorizerResult = {
61- principalId,
62- policyDocument : {
63- Version : '2012-10-17' ,
64- Statement : [
65- {
66- Action : 'execute-api:Invoke' ,
67- Effect : effect ,
68- Resource : resource ,
69- } ,
70- ] ,
71- } ,
72- context : {
73- stringKey : 'stringval' ,
74- numberKey : 123 ,
75- booleanKey : true ,
76- } ,
77- } ;
78- return authResponse ;
79- }
80-
81- function generateAllow ( principalId : string , resource : string ) : APIGatewayAuthorizerResult {
82- return generatePolicy ( principalId , 'Allow' , resource ) ;
83- }
84-
85- function generateDeny ( principalId : string , resource : string ) : APIGatewayAuthorizerResult {
86- return generatePolicy ( principalId , 'Deny' , resource ) ;
87- }
1+ // A simple request-based authorizer example to demonstrate how to use request
2+ // parameters to allow or deny a request. In this example, a request is
3+ // authorized if the client-supplied HeaderAuth1 header and stage variable of StageVar1
4+ // both match specified values of 'headerValue1' and 'stageValue1', respectively.
5+ //
6+ // Example curl request (replace <api-url> and <stage> as appropriate):
7+ //
8+ // curl -H "HeaderAuth1: headerValue1" \
9+ // "<api-url>/<stage>/your-resource"
10+ //
11+
12+ // See https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html for the original JS documentation
13+
14+ import { APIGatewayRequestAuthorizerEvent , Context , Callback , APIGatewayAuthorizerResult } from 'aws-lambda' ;
15+
16+ export const handler = (
17+ event : APIGatewayRequestAuthorizerEvent ,
18+ context : Context ,
19+ callback : Callback < APIGatewayAuthorizerResult >
20+ ) : void => {
21+ console . log ( 'Received event:' , JSON . stringify ( event , null , 2 ) ) ;
22+
23+ // Retrieve request parameters from the Lambda function input:
24+ const headers = event . headers || { } ;
25+ const pathParameters = event . pathParameters || { } ;
26+ const stageVariables = event . stageVariables || { } ;
27+
28+ // Parse the input for the parameter values
29+ const tmp = event . methodArn . split ( ':' ) ;
30+ const apiGatewayArnTmp = tmp [ 5 ] . split ( '/' ) ;
31+ const awsAccountId = tmp [ 4 ] ;
32+ const region = tmp [ 3 ] ;
33+ const restApiId = apiGatewayArnTmp [ 0 ] ;
34+ const stage = apiGatewayArnTmp [ 1 ] ;
35+ const method = apiGatewayArnTmp [ 2 ] ;
36+ let resource = '/' ; // root resource
37+ if ( apiGatewayArnTmp [ 3 ] ) {
38+ resource += apiGatewayArnTmp [ 3 ] ;
39+ }
40+
41+ // Perform authorization to return the Allow policy for correct parameters and
42+ // the 'Unauthorized' error, otherwise.
43+ if (
44+ headers [ 'HeaderAuth1' ] === 'headerValue1' &&
45+ stageVariables [ 'StageVar1' ] === 'stageValue1'
46+ ) {
47+ callback ( null , generateAllow ( 'me' , event . methodArn ) ) ;
48+ } else {
49+ callback ( null , generateDeny ( 'me' , event . methodArn ) ) ;
50+ }
51+ } ;
52+
53+ // Helper function to generate an IAM policy
54+ function generatePolicy (
55+ principalId : string ,
56+ effect : 'Allow' | 'Deny' ,
57+ resource : string
58+ ) : APIGatewayAuthorizerResult {
59+ // Required output:
60+ const authResponse : APIGatewayAuthorizerResult = {
61+ principalId,
62+ policyDocument : {
63+ Version : '2012-10-17' ,
64+ Statement : [
65+ {
66+ Action : 'execute-api:Invoke' ,
67+ Effect : effect ,
68+ Resource : resource ,
69+ } ,
70+ ] ,
71+ } ,
72+ context : {
73+ stringKey : 'stringval' ,
74+ numberKey : 123 ,
75+ booleanKey : true ,
76+ } ,
77+ } ;
78+ return authResponse ;
79+ }
80+
81+ function generateAllow ( principalId : string , resource : string ) : APIGatewayAuthorizerResult {
82+ return generatePolicy ( principalId , 'Allow' , resource ) ;
83+ }
84+
85+ function generateDeny ( principalId : string , resource : string ) : APIGatewayAuthorizerResult {
86+ return generatePolicy ( principalId , 'Deny' , resource ) ;
87+ }
0 commit comments