@@ -36,59 +36,64 @@ import AWSLambdaRuntime
3636//
3737// This code is shown for the example only and is not used in this demo.
3838// This code doesn't perform any type of token validation. It should be used as a reference only.
39- let policyAuthorizerHandler :
40- ( APIGatewayLambdaAuthorizerRequest , LambdaContext ) async throws -> APIGatewayLambdaAuthorizerPolicyResponse = {
41- ( request: APIGatewayLambdaAuthorizerRequest , context: LambdaContext ) in
39+ // let policyAuthorizerHandler:
40+ // (APIGatewayLambdaAuthorizerRequest, LambdaContext) async throws -> APIGatewayLambdaAuthorizerPolicyResponse = {
41+ // (request: APIGatewayLambdaAuthorizerRequest, context: LambdaContext) in
4242
43- context. logger. debug ( " +++ Policy Authorizer called +++ " )
43+ // context.logger.debug("+++ Policy Authorizer called +++")
4444
45- // typically, this function will check the validity of the incoming token received in the request
45+ // // typically, this function will check the validity of the incoming token received in the request
4646
47- // then it creates and returns a response
48- return APIGatewayLambdaAuthorizerPolicyResponse (
49- principalId: " John Appleseed " ,
47+ // // then it creates and returns a response
48+ // return APIGatewayLambdaAuthorizerPolicyResponse(
49+ // principalId: "John Appleseed",
5050
51- // this policy allows the caller to invoke any API Gateway endpoint
52- policyDocument: . init( statement: [
53- . init(
54- action: " execute-api:Invoke " ,
55- effect: . allow,
56- resource: " * "
57- )
51+ // // this policy allows the caller to invoke any API Gateway endpoint
52+ // policyDocument: .init(statement: [
53+ // .init(
54+ // action: "execute-api:Invoke",
55+ // effect: .allow,
56+ // resource: "*"
57+ // )
5858
59- ] ) ,
59+ // ]),
6060
61- // this is additional context we want to return to the caller
62- context: [
63- " abc1 " : " xyz1 " ,
64- " abc2 " : " xyz2 " ,
65- ]
66- )
67- }
61+ // // this is additional context we want to return to the caller
62+ // context: [
63+ // "abc1": "xyz1",
64+ // "abc2": "xyz2",
65+ // ]
66+ // )
67+ // }
6868
6969//
7070// This is an example of a simple authorizer that always authorizes the request.
7171// A simple authorizer returns a yes/no decision and optional context key-value pairs
7272//
7373// This code doesn't perform any type of token validation. It should be used as a reference only.
74- // let simpleAuthorizerHandler:
75- // (APIGatewayLambdaAuthorizerRequest, LambdaContext) async throws -> APIGatewayLambdaAuthorizerSimpleResponse = {
76- // (_ : APIGatewayLambdaAuthorizerRequest, context: LambdaContext) in
74+ let simpleAuthorizerHandler :
75+ ( APIGatewayLambdaAuthorizerRequest , LambdaContext ) async throws -> APIGatewayLambdaAuthorizerSimpleResponse = {
76+ ( request : APIGatewayLambdaAuthorizerRequest , context: LambdaContext ) in
7777
78- // context.logger.debug("+++ Simple Authorizer called +++")
78+ context. logger. debug ( " +++ Simple Authorizer called +++ " )
7979
80- // // typically, this function will check the validity of the incoming token received in the request
80+ guard let authToken = request. headers [ " authorization " ] ,
81+ authToken == " 123 "
82+ else {
83+ context. logger. warning ( " Missing or invalid Authorization header " )
84+ return . init( isAuthorized: false , context: [ : ] )
85+ }
8186
82- // return APIGatewayLambdaAuthorizerSimpleResponse(
83- // // this is the authorization decision: yes or no
84- // isAuthorized: true,
87+ return APIGatewayLambdaAuthorizerSimpleResponse (
88+ // this is the authorization decision: yes or no
89+ isAuthorized: true ,
8590
86- // // this is additional context we want to return to the caller
87- // context: ["abc1": "xyz1"]
88- // )
89- // }
91+ // this is additional context we want to return to the caller
92+ context: [ " abc1 " : " xyz1 " ]
93+ )
94+ }
9095
9196// create the runtime and start polling for new events.
9297// in this demo we use the simple authorizer handler
93- let runtime = LambdaRuntime ( body: policyAuthorizerHandler )
98+ let runtime = LambdaRuntime ( body: simpleAuthorizerHandler )
9499try await runtime. run ( )
0 commit comments