forked from aws-amplify/aws-appsync-apollo-extensions-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPIKeyAuthorizer.swift
55 lines (46 loc) · 1.71 KB
/
APIKeyAuthorizer.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
import Apollo
import ApolloAPI
import Foundation
public class APIKeyAuthorizer: AppSyncAuthorizer {
let fetchAPIKey: () async throws -> String
static let apiKeyHeaderName = "x-api-key"
static let amzDateHeaderName = "x-amz-date"
static let AWSDateISO8601DateFormat2 = "yyyyMMdd'T'HHmmss'Z'"
lazy var formatter: DateFormatter = {
let formatter = DateFormatter()
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = APIKeyAuthorizer.AWSDateISO8601DateFormat2
return formatter
}()
public init(apiKey: String) {
self.fetchAPIKey = {
apiKey
}
}
public init(fetchAPIKey: @escaping () async throws -> String) {
self.fetchAPIKey = fetchAPIKey
}
public func getHttpAuthorizationHeaders(request: URLRequest) async throws -> [String: String] {
let apiKey = try await fetchAPIKey()
return [APIKeyAuthorizer.apiKeyHeaderName: apiKey]
}
public func getWebsocketConnectionHeaders(endpoint: URL) async throws -> [String: String] {
try await getWebsocketHeaders()
}
public func getWebSocketSubscriptionPayload(request: URLRequest) async throws -> [String: String] {
try await getWebsocketHeaders()
}
private func getWebsocketHeaders() async throws -> [String: String] {
let apiKey = try await fetchAPIKey()
let date = formatter.string(from: Date())
return [APIKeyAuthorizer.apiKeyHeaderName: apiKey,
APIKeyAuthorizer.amzDateHeaderName: date]
}
}