|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift OpenAPI Lambda open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 Amazon.com, Inc. or its affiliates |
| 6 | +// and the Swift OpenAPI Lambda project authors |
| 7 | +// Licensed under Apache License v2.0 |
| 8 | +// |
| 9 | +// See LICENSE.txt for license information |
| 10 | +// See CONTRIBUTORS.txt for the list of Swift OpenAPI Lambda project authors |
| 11 | +// |
| 12 | +// SPDX-License-Identifier: Apache-2.0 |
| 13 | +// |
| 14 | +//===----------------------------------------------------------------------===// |
| 15 | + |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | +// |
| 18 | +// This source file is part of the SwiftAWSLambdaRuntime open source project |
| 19 | +// |
| 20 | +// Copyright (c) 2024 Apple Inc. and the SwiftAWSLambdaRuntime project authors |
| 21 | +// Licensed under Apache License v2.0 |
| 22 | +// |
| 23 | +// See LICENSE.txt for license information |
| 24 | +// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors |
| 25 | +// |
| 26 | +// SPDX-License-Identifier: Apache-2.0 |
| 27 | +// |
| 28 | +//===----------------------------------------------------------------------===// |
| 29 | + |
| 30 | +import AWSLambdaEvents |
| 31 | +import AWSLambdaRuntime |
| 32 | + |
| 33 | +// |
| 34 | +// This is an example of a policy authorizer that always authorizes the request. |
| 35 | +// The policy authorizer returns an IAM policy document that defines what the Lambda function caller can do and optional context key-value pairs |
| 36 | +// |
| 37 | +// This code is shown for the example only and is not used in this demo. |
| 38 | +// 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 |
| 42 | + |
| 43 | +// context.logger.debug("+++ Policy Authorizer called +++") |
| 44 | + |
| 45 | +// // typically, this function will check the validity of the incoming token received in the request |
| 46 | + |
| 47 | +// // then it creates and returns a response |
| 48 | +// return APIGatewayLambdaAuthorizerPolicyResponse( |
| 49 | +// principalId: "John Appleseed", |
| 50 | + |
| 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 | +// ) |
| 58 | + |
| 59 | +// ]), |
| 60 | + |
| 61 | +// // this is additional context we want to return to the caller |
| 62 | +// context: [ |
| 63 | +// "abc1": "xyz1", |
| 64 | +// "abc2": "xyz2", |
| 65 | +// ] |
| 66 | +// ) |
| 67 | +// } |
| 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 | + authToken == "Bearer 123" |
| 82 | + else { |
| 83 | + context.logger.warning("Missing or invalid Authorization header") |
| 84 | + return .init(isAuthorized: false, context: [:]) |
| 85 | + } |
| 86 | + |
| 87 | + return APIGatewayLambdaAuthorizerSimpleResponse( |
| 88 | + // this is the authorization decision: yes or no |
| 89 | + isAuthorized: true, |
| 90 | + |
| 91 | + // this is additional context we want to return to the caller |
| 92 | + context: ["abc1": "xyz1"] |
| 93 | + ) |
| 94 | + } |
| 95 | + |
| 96 | +// create the runtime and start polling for new events. |
| 97 | +// in this demo we use the simple authorizer handler |
| 98 | +let runtime = LambdaRuntime(body: simpleAuthorizerHandler) |
| 99 | +try await runtime.run() |
0 commit comments