Skip to content

Commit 1b22412

Browse files
committed
fix authentication
1 parent b1cb89a commit 1b22412

File tree

3 files changed

+46
-41
lines changed

3 files changed

+46
-41
lines changed

Examples/quoteapi/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ deploy:
1111
sam deploy
1212

1313
logs:
14-
sam logs --stack-name QuoteService --name QuoteService
14+
sam logs --stack-name QuoteService
1515

1616
tail:
17-
sam logs --stack-name QuoteService --name QuoteService --tail
17+
sam logs --stack-name QuoteService --tail
1818

1919
local:
2020
swift run QuoteService
@@ -24,7 +24,7 @@ local-invoke:
2424

2525
invoke:
2626
## curl -v -H 'Authorization: 123' https://<REPLACE_WITH_YOUR_API_URI>/stocks/AAPL
27-
curl -v -H 'Authorization: 123' https://xb6a6h6x33.execute-api.us-east-1.amazonaws.com/stocks/AAPL
27+
curl -v -H 'Authorization: 123' https://lq2rria2n6.execute-api.us-east-1.amazonaws.com/stocks/AAPL
2828

2929
###################### No Change required below this line ##########################
3030

Examples/quoteapi/Sources/LambdaAuthorizer/main.swift

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
9499
try await runtime.run()

Examples/quoteapi/template.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Globals:
88
CodeUri: .
99
Handler: swift.bootstrap
1010
Runtime: provided.al2
11-
MemorySize: 256
11+
MemorySize: 128
1212
Architectures:
1313
- arm64
1414

@@ -46,7 +46,7 @@ Resources:
4646
# by default, AWS Lambda runtime produces no log
4747
# use `LOG_LEVEL: debug` for for lifecycle and event handling information
4848
# use `LOG_LEVEL: trace` for detailed input event information
49-
LOG_LEVEL: debug
49+
LOG_LEVEL: trace
5050
Metadata:
5151
BuildMethod: makefile
5252

0 commit comments

Comments
 (0)