-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathAppSyncAuthorization.swift
More file actions
67 lines (59 loc) · 2.33 KB
/
Copy pathAppSyncAuthorization.swift
File metadata and controls
67 lines (59 loc) · 2.33 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
import Foundation
/// Wraps the authorizer(s) that the client uses.
///
/// Supports both single-auth (one authorizer for all requests) and multi-auth
/// (multiple authorizers, selected based on model `@auth` rules or per-request overrides).
public enum AppSyncAuthorization: Sendable {
/// Single authorizer used for all requests.
case single(AppSyncAuthorizer)
/// Multiple authorizers. The client selects the appropriate one based on model
/// `@auth` rules or per-request auth mode overrides. Falls back to `defaultAuthMode`
/// when no rule matches.
///
/// - Parameters:
/// - defaultAuthMode: The auth mode to use when no per-request override or model rule applies.
/// - authorizers: The list of authorizers. Duplicate auth modes are not allowed.
case multi(defaultAuthMode: AppSyncAuthMode, authorizers: [AppSyncAuthorizer])
}
extension AppSyncAuthorization {
/// Resolves the authorizer for a given auth mode.
/// - Returns: The matching authorizer, or nil if not found.
func authorizer(for mode: AppSyncAuthMode) -> AppSyncAuthorizer? {
switch self {
case .single(let authorizer):
return authorizer.authMode == mode ? authorizer : nil
case .multi(_, let authorizers):
return authorizers.first { $0.authMode == mode }
}
}
/// The default authorizer.
var defaultAuthorizer: AppSyncAuthorizer {
switch self {
case .single(let authorizer):
return authorizer
case .multi(let defaultAuthMode, let authorizers):
guard let authorizer = authorizers.first(where: { $0.authMode == defaultAuthMode }) else {
preconditionFailure(
"No authorizer provided for the default auth mode: \(defaultAuthMode). " +
"Ensure the authorizers list contains an entry matching the defaultAuthMode."
)
}
return authorizer
}
}
/// The default auth mode.
var defaultAuthMode: AppSyncAuthMode {
switch self {
case .single(let authorizer):
return authorizer.authMode
case .multi(let defaultAuthMode, _):
return defaultAuthMode
}
}
}