Skip to content

Commit afb5b2f

Browse files
Fix Provider.authorizers
1 parent 8258c2b commit afb5b2f

File tree

4 files changed

+60
-19
lines changed

4 files changed

+60
-19
lines changed

Sources/SLSAdapter/Function+Extensions.swift

+28
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,34 @@ public extension YAMLContent {
2121
let layerInternalName = layerName.prefix(1).capitalized + layerName.dropFirst(1).replacingOccurrences(of: "-", with: "Dash").appending("LambdaLayer")
2222
return try YAMLContent(with: [["Ref": layerInternalName]])
2323
}
24+
25+
static func buildJWTAuthorizer(identitySource: String = "$request.header.Authorization",
26+
issuerUrl: String,
27+
audience: [String]) -> YAMLContent {
28+
return .dictionary(
29+
["type": .string("jwt"),
30+
"identitySource": .string(identitySource),
31+
"issuerUrl": .string(issuerUrl),
32+
"audience": .array(audience.compactMap { .string($0)} )])
33+
}
34+
35+
static func buildCustomAuthorizer(name: String,
36+
type: String = "request",
37+
functionName: String,
38+
payloadVersion: String = "2.0",
39+
identitySource: [String],
40+
resultTtlInSeconds: Int = 0,
41+
enableSimpleResponses: Bool = true) -> YAMLContent {
42+
return .dictionary(
43+
["name": .string(name),
44+
"type": .string(type),
45+
"functionName": .string(functionName),
46+
"payloadVersion": .string(payloadVersion),
47+
"identitySource": .array(identitySource.compactMap { .string($0) }),
48+
"resultTtlInSeconds": .string("\(resultTtlInSeconds)"),
49+
"enableSimpleResponses": .string("\(enableSimpleResponses)")
50+
])
51+
}
2452
}
2553

2654
public extension Function {

Sources/SLSAdapter/Provider.swift

+2-18
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ extension Provider {
315315
disableDefaultEndpoint: Bool? = nil,
316316
metrics: Bool? = nil,
317317
cors: Bool?,
318-
authorizers: Provider.Authorizers? = nil
318+
authorizers: YAMLContent? = nil
319319
) {
320320
self.id = id
321321
self.name = name
@@ -347,23 +347,7 @@ extension Provider {
347347
public let cors: Bool?
348348

349349
/// Authorizers
350-
public let authorizers: Authorizers?
351-
}
352-
353-
/// Authorizers
354-
public struct Authorizers: Codable, Equatable {
355-
/// JWT API authorizer
356-
public let someJwtAuthorizer: JwtAuthorizer?
357-
358-
/// CustomLambdaAuthorizer
359-
let someCustomLambdaAuthorizer: YAMLContent?
360-
}
361-
362-
/// JwtAuthorizer
363-
public struct JwtAuthorizer: Codable, Equatable {
364-
public let identitySource: String
365-
public let issuerUrl: String
366-
public let audience: [String]
350+
public let authorizers: YAMLContent?
367351
}
368352
}
369353

Tests/SLSAdapterTests/Fixtures/serverless_no_layer.yml

+17
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@ provider:
1515
httpApi:
1616
payload: '2.0'
1717
cors: true
18+
authorizers:
19+
JWTAuthorizer:
20+
type: jwt
21+
identitySource: $request.header.Authorization
22+
issuerUrl: "https://appleid.apple.com"
23+
audience:
24+
- com.mydomain.myhost
25+
customAuthorizer:
26+
name: "LambdaAuthorizer"
27+
type: request
28+
functionName: lambdaAuthorizer
29+
payloadVersion: '2.0'
30+
identitySource:
31+
- $request.header.SEC-X-API-KEY
32+
- $request.header.User-Agent
33+
resultTtlInSeconds: 0
34+
enableSimpleResponses: true
1835
# Use provided.al2 if you build with M1, provided if you build with x86_64
1936
runtime: provided.al2
2037
# Use arm64 if you build with M1, or x86_64 if you build with intel

Tests/SLSAdapterTests/ServerlessConfig+Exensions.swift

+13-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,19 @@ extension ServerlessConfig {
149149
runtime: runtime,
150150
environment: environment,
151151
architecture: architecture,
152-
httpAPI: .init(payload: "2.0", cors: true),
152+
httpAPI: .init(
153+
payload: "2.0",
154+
cors: true,
155+
authorizers:
156+
.dictionary([
157+
"JWTAuthorizer": .buildJWTAuthorizer(issuerUrl: "https://appleid.apple.com",
158+
audience: ["com.mydomain.myhost"]),
159+
"customAuthorizer": .buildCustomAuthorizer(name: "LambdaAuthorizer",
160+
functionName: "lambdaAuthorizer",
161+
identitySource: ["$request.header.SEC-X-API-KEY",
162+
"$request.header.User-Agent"])
163+
])
164+
),
153165
iam: iam
154166
)
155167
let custom = try YAMLContent(with: ["tableName": "\(dynamoDBTableNamePrefix)-table-${sls:stage}",

0 commit comments

Comments
 (0)