Skip to content

Commit c658ddb

Browse files
committed
more comments
1 parent af41aaf commit c658ddb

File tree

2 files changed

+48
-36
lines changed

2 files changed

+48
-36
lines changed

Examples/quoteapi/Sources/LambdaAuthorizer/main.swift

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,49 @@
3030
import AWSLambdaEvents
3131
import AWSLambdaRuntime
3232

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
3376
//
3477
// This is an example of a policy authorizer that always authorizes the request.
3578
// 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
65108
// ]
66109
// )
67110
// }
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()

Examples/quoteapi/Sources/QuoteAPI/QuoteService.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,24 @@ struct QuoteServiceImpl: APIProtocol, OpenAPILambdaHttpApi {
2626

2727
func register(transport: OpenAPILambdaTransport) throws {
2828

29+
// OPTIONAL
2930
// you have a chance here to customize the routes, for example
3031
try transport.router.get("/health") { _, _ in
3132
"OK"
3233
}
3334
logger.trace("Available Routes\n\(transport.router)") // print the router tree (for debugging purposes)
3435

36+
// OPTIONAL
3537
// to log all requests and their responses, add a logging middleware
3638
let loggingMiddleware = LoggingMiddleware(logger: logger)
3739

40+
// OPTIONAL
3841
// This app includes a sample authorization middleware
3942
// It transforms the bearer token into a username.
4043
// The user name can be access through a TaskLocal variable.
4144
let authenticationMiddleware = self.authenticationMiddleware()
4245

46+
// MANDATORY (middlewares are optional)
4347
try self.registerHandlers(on: transport, middlewares: [loggingMiddleware, authenticationMiddleware])
4448
}
4549

@@ -100,6 +104,7 @@ struct QuoteServiceImpl: APIProtocol, OpenAPILambdaHttpApi {
100104

101105
func getQuote(_ input: Operations.getQuote.Input) async throws -> Operations.getQuote.Output {
102106

107+
// OPTIONAL
103108
// Check if the Authentication Middleware has been able to authenticate the user
104109
guard let user = AuthenticationServerMiddleware.User.current else { return .unauthorized(.init()) }
105110

0 commit comments

Comments
 (0)