@@ -20,7 +20,7 @@ extension StorageEngine {
20
20
return
21
21
}
22
22
23
- let authPluginRequired = requiresAuthPlugin ( )
23
+ let authPluginRequired = requiresAuthPlugin ( api )
24
24
25
25
guard authPluginRequired else {
26
26
syncEngine? . start ( api: api, auth: nil )
@@ -38,7 +38,7 @@ extension StorageEngine {
38
38
completion ( . successfulVoid)
39
39
}
40
40
41
- private func tryGetAPIPlugin( ) -> APICategoryGraphQLBehavior ? {
41
+ private func tryGetAPIPlugin( ) -> APICategoryPlugin ? {
42
42
do {
43
43
return try Amplify . API. getPlugin ( for: validAPIPluginKey)
44
44
} catch {
@@ -54,20 +54,60 @@ extension StorageEngine {
54
54
}
55
55
}
56
56
57
- private func requiresAuthPlugin( ) -> Bool {
58
- let modelsRequireAuthPlugin = ModelRegistry . modelSchemas. contains {
59
- $0. isSyncable && $0. hasAuthenticationRules && $0. authRules. requireAuthPlugin
57
+ private func requiresAuthPlugin( _ apiPlugin: APICategoryPlugin ) -> Bool {
58
+ let modelsRequireAuthPlugin = ModelRegistry . modelSchemas. contains { schema in
59
+ guard schema. isSyncable && schema. hasAuthenticationRules else {
60
+ return false
61
+ }
62
+ if let rulesRequireAuthPlugin = schema. authRules. requireAuthPlugin {
63
+ return rulesRequireAuthPlugin
64
+ }
65
+
66
+ #if canImport(AWSAPIPlugin)
67
+ // Fall back to the plugin configuration if a determination cannot be made from the auth rules.
68
+ guard let awsPlugin = apiPlugin as? AWSAPIPlugin else {
69
+ // No determination can be made. Throw error?
70
+ return false
71
+ }
72
+ return awsPlugin. hasAuthPluginRequirement
73
+ #else
74
+ return false
75
+ #endif
60
76
}
61
77
return modelsRequireAuthPlugin
62
78
}
63
79
}
64
80
65
- internal extension AuthRule {
81
+ #if canImport(AWSAPIPlugin)
82
+ internal extension AWSAPIPlugin {
83
+ var hasAuthPluginRequirement : Bool {
84
+ return pluginConfig. endpoints. values. contains {
85
+ $0. authorizationType. requiresAuthPlugin
86
+ }
87
+ }
88
+ }
89
+ #endif
90
+
91
+ internal extension AWSAuthorizationType {
66
92
var requiresAuthPlugin : Bool {
93
+ switch self {
94
+ case . none, . apiKey, . openIDConnect, . function:
95
+ return false
96
+ case . awsIAM, . amazonCognitoUserPools:
97
+ return true
98
+ }
99
+ }
100
+ }
101
+
102
+ internal extension AuthRule {
103
+ var requiresAuthPlugin : Bool ? {
104
+ guard let provider = provider else {
105
+ return nil
106
+ }
67
107
switch provider {
68
108
// OIDC, Function and API key providers don't need
69
109
// Auth plugin
70
- case . oidc, . function, . apiKey, . none :
110
+ case . oidc, . function, . apiKey:
71
111
return false
72
112
case . userPools, . iam:
73
113
return true
@@ -77,8 +117,16 @@ internal extension AuthRule {
77
117
78
118
internal extension AuthRules {
79
119
/// Convenience method to check whether we need Auth plugin
80
- /// - Returns: true If **any** of the rules uses a provider that requires the Auth plugin
81
- var requireAuthPlugin : Bool {
82
- contains { $0. requiresAuthPlugin }
120
+ /// - Returns: true If **any** of the rules uses a provider that requires the Auth plugin, `nil` if a determination cannot be made
121
+ var requireAuthPlugin : Bool ? {
122
+ for rule in self {
123
+ guard let requiresAuthPlugin = rule. requiresAuthPlugin else {
124
+ return nil
125
+ }
126
+ if requiresAuthPlugin {
127
+ return true
128
+ }
129
+ }
130
+ return false
83
131
}
84
132
}
0 commit comments