Skip to content

Commit 62f8490

Browse files
authored
fix(datastore): require auth plugin if provider is nil in sync-requirements (#1461)
* Revert "fix(datastore): Sync engine fallback to API plugin config (#1460)" This reverts commit d639291. * fix(datastore): require auth plugin if provider is nil in sync-requirements
1 parent d639291 commit 62f8490

File tree

2 files changed

+28
-57
lines changed

2 files changed

+28
-57
lines changed

AmplifyPlugins/DataStore/AWSDataStoreCategoryPlugin/Storage/StorageEngine+SyncRequirement.swift

Lines changed: 10 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extension StorageEngine {
2020
return
2121
}
2222

23-
let authPluginRequired = requiresAuthPlugin(api)
23+
let authPluginRequired = requiresAuthPlugin()
2424

2525
guard authPluginRequired else {
2626
syncEngine?.start(api: api, auth: nil)
@@ -38,7 +38,7 @@ extension StorageEngine {
3838
completion(.successfulVoid)
3939
}
4040

41-
private func tryGetAPIPlugin() -> APICategoryPlugin? {
41+
private func tryGetAPIPlugin() -> APICategoryGraphQLBehavior? {
4242
do {
4343
return try Amplify.API.getPlugin(for: validAPIPluginKey)
4444
} catch {
@@ -54,56 +54,20 @@ extension StorageEngine {
5454
}
5555
}
5656

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
57+
private func requiresAuthPlugin() -> Bool {
58+
let modelsRequireAuthPlugin = ModelRegistry.modelSchemas.contains {
59+
$0.isSyncable && $0.hasAuthenticationRules && $0.authRules.requireAuthPlugin
7660
}
7761
return modelsRequireAuthPlugin
7862
}
7963
}
8064

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 {
65+
internal extension AuthRule {
9266
var requiresAuthPlugin: Bool {
93-
switch self {
94-
case .none, .apiKey, .openIDConnect, .function:
95-
return false
96-
case .awsIAM, .amazonCognitoUserPools:
67+
guard let provider = self.provider else {
9768
return true
9869
}
99-
}
100-
}
10170

102-
internal extension AuthRule {
103-
var requiresAuthPlugin: Bool? {
104-
guard let provider = provider else {
105-
return nil
106-
}
10771
switch provider {
10872
// OIDC, Function and API key providers don't need
10973
// Auth plugin
@@ -117,16 +81,8 @@ internal extension AuthRule {
11781

11882
internal extension AuthRules {
11983
/// Convenience method to check whether we need Auth plugin
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
84+
/// - Returns: true If **any** of the rules uses a provider that requires the Auth plugin
85+
var requireAuthPlugin: Bool {
86+
contains { $0.requiresAuthPlugin }
13187
}
13288
}

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginTests/Sync/StorageEngineSyncRequirementsTests.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class StorageEngineSyncRequirementsTests: XCTestCase {
2121
AuthRule(allow: .private, provider: .iam),
2222
AuthRule(allow: .owner, provider: .userPools)
2323
]
24-
XCTAssertTrue(authRules.requireAuthPlugin!)
24+
XCTAssertTrue(authRules.requireAuthPlugin)
2525
}
2626

2727
/// Given: a list of auth rules
@@ -32,7 +32,7 @@ class StorageEngineSyncRequirementsTests: XCTestCase {
3232
AuthRule(allow: .owner, provider: .function),
3333
AuthRule(allow: .owner, provider: .iam)
3434
]
35-
XCTAssertTrue(authRules.requireAuthPlugin!)
35+
XCTAssertTrue(authRules.requireAuthPlugin)
3636
}
3737

3838
func testDoesNotRequireAuthPlugin() {
@@ -41,6 +41,21 @@ class StorageEngineSyncRequirementsTests: XCTestCase {
4141
AuthRule(allow: .owner, provider: .function),
4242
AuthRule(allow: .public, provider: .apiKey)
4343
]
44-
XCTAssertFalse(authRules.requireAuthPlugin!)
44+
XCTAssertFalse(authRules.requireAuthPlugin)
45+
}
46+
47+
func testRequireAuthPluginIfProviderIsNil() {
48+
let authRules: AuthRules = [
49+
AuthRule(allow: .owner, provider: nil)
50+
]
51+
XCTAssertTrue(authRules.requireAuthPlugin)
52+
}
53+
54+
func testRequireAuthPluginIfOneRulHasProviderNil() {
55+
let authRules: AuthRules = [
56+
AuthRule(allow: .owner, provider: nil),
57+
AuthRule(allow: .public, provider: .apiKey)
58+
]
59+
XCTAssertTrue(authRules.requireAuthPlugin)
4560
}
4661
}

0 commit comments

Comments
 (0)