|
30 | 30 | import AWSLambdaEvents
|
31 | 31 | import AWSLambdaRuntime
|
32 | 32 |
|
| 33 | +// |
| 34 | +// This is an example of a simple authorizer that always authorizes the request. |
| 35 | +// A simple authorizer returns a yes/no decision and optional context key-value pairs |
| 36 | +// |
| 37 | +// Warning: this is an overly simplified authentication strategy, checking |
| 38 | +// for the presence of a token. |
| 39 | +// |
| 40 | +// In your project, here you would likely call out to a library that performs |
| 41 | +// a cryptographic validation, or similar. |
| 42 | +// |
| 43 | +// The code is for illustrative purposes only and should not be used directly. |
| 44 | +let simpleAuthorizerHandler: |
| 45 | + (APIGatewayLambdaAuthorizerRequest, LambdaContext) async throws -> APIGatewayLambdaAuthorizerSimpleResponse = { |
| 46 | + (request: APIGatewayLambdaAuthorizerRequest, context: LambdaContext) in |
| 47 | + |
| 48 | + context.logger.debug("+++ Simple Authorizer called +++") |
| 49 | + |
| 50 | + guard let authToken = request.headers["authorization"] |
| 51 | + else { |
| 52 | + context.logger.warning("Missing Authorization header") |
| 53 | + return .init(isAuthorized: false, context: [:]) |
| 54 | + } |
| 55 | + |
| 56 | + // do not take an authorization decision here. |
| 57 | + // bring the token to the OpenAPI service and let the developer |
| 58 | + // verify authorization there. |
| 59 | + |
| 60 | + return APIGatewayLambdaAuthorizerSimpleResponse( |
| 61 | + // this is the authorization decision: yes or no |
| 62 | + isAuthorized: true, |
| 63 | + |
| 64 | + // this is additional context we want to return to the caller |
| 65 | + // these values can be retrieved in requestContext.authorizer of the APIGatewayv2 request |
| 66 | + context: ["token": authToken] |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | +// create the runtime and start polling for new events. |
| 71 | +// in this demo we use the simple authorizer handler |
| 72 | +let runtime = LambdaRuntime(body: simpleAuthorizerHandler) |
| 73 | +try await runtime.run() |
| 74 | + |
| 75 | +// Another, more complex, example |
33 | 76 | //
|
34 | 77 | // This is an example of a policy authorizer that always authorizes the request.
|
35 | 78 | // The policy authorizer returns an IAM policy document that defines what the Lambda function caller can do and optional context key-value pairs
|
@@ -65,39 +108,3 @@ import AWSLambdaRuntime
|
65 | 108 | // ]
|
66 | 109 | // )
|
67 | 110 | // }
|
68 |
| - |
69 |
| -// |
70 |
| -// This is an example of a simple authorizer that always authorizes the request. |
71 |
| -// A simple authorizer returns a yes/no decision and optional context key-value pairs |
72 |
| -// |
73 |
| -// 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 |
| - (request: APIGatewayLambdaAuthorizerRequest, context: LambdaContext) in |
77 |
| - |
78 |
| - context.logger.debug("+++ Simple Authorizer called +++") |
79 |
| - |
80 |
| - guard let authToken = request.headers["authorization"] |
81 |
| - else { |
82 |
| - context.logger.warning("Missing Authorization header") |
83 |
| - return .init(isAuthorized: false, context: [:]) |
84 |
| - } |
85 |
| - |
86 |
| - // do not take an authorization decision here. |
87 |
| - // bring the token to the OpenAPI service and let the developer |
88 |
| - // verify authorization there. |
89 |
| - |
90 |
| - return APIGatewayLambdaAuthorizerSimpleResponse( |
91 |
| - // this is the authorization decision: yes or no |
92 |
| - isAuthorized: true, |
93 |
| - |
94 |
| - // this is additional context we want to return to the caller |
95 |
| - // these values can be retrieved in requestContext.authorizer of the APIGatewayv2 request |
96 |
| - context: ["token": authToken] |
97 |
| - ) |
98 |
| - } |
99 |
| - |
100 |
| -// create the runtime and start polling for new events. |
101 |
| -// in this demo we use the simple authorizer handler |
102 |
| -let runtime = LambdaRuntime(body: simpleAuthorizerHandler) |
103 |
| -try await runtime.run() |
|
0 commit comments