Skip to content

Commit 3723eb0

Browse files
diegocstnlawmicha
andauthored
feat(datastore): support for @auth provider attribute (#1258)
* feat(datastore): support for @auth provider * feat(datastore): support for @auth provider, static factory * Apply suggestions from code review Co-authored-by: Michael Law <[email protected]> * feat(datastore): move AWS providers to AuthRuleProvider Co-authored-by: Michael Law <[email protected]>
1 parent 763f143 commit 3723eb0

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

Amplify.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@
246246
762167D52615435C0033FCD2 /* Record+Schema.swift in Sources */ = {isa = PBXBuildFile; fileRef = 762167D42615435C0033FCD2 /* Record+Schema.swift */; };
247247
762C978526210F6400798FA3 /* RecordCover.swift in Sources */ = {isa = PBXBuildFile; fileRef = 762C978426210F6400798FA3 /* RecordCover.swift */; };
248248
762C978E26210FF100798FA3 /* RecordCover+Schema.swift in Sources */ = {isa = PBXBuildFile; fileRef = 762C978D26210FF100798FA3 /* RecordCover+Schema.swift */; };
249+
762F70A426683EE2001F8252 /* AuthRuleExtensionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 762F70A326683EE2001F8252 /* AuthRuleExtensionTests.swift */; };
249250
7678B38426017D5300B4917F /* AppSyncErrorTypeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7678B38326017D5300B4917F /* AppSyncErrorTypeTests.swift */; };
250251
7678B38526017D5300B4917F /* AppSyncErrorTypeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7678B38326017D5300B4917F /* AppSyncErrorTypeTests.swift */; };
251252
767F85FC2649FF540076D633 /* CustomerOrder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 767F85FB2649FF540076D633 /* CustomerOrder.swift */; };
@@ -1132,6 +1133,7 @@
11321133
762167D42615435C0033FCD2 /* Record+Schema.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Record+Schema.swift"; sourceTree = "<group>"; };
11331134
762C978426210F6400798FA3 /* RecordCover.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecordCover.swift; sourceTree = "<group>"; };
11341135
762C978D26210FF100798FA3 /* RecordCover+Schema.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RecordCover+Schema.swift"; sourceTree = "<group>"; };
1136+
762F70A326683EE2001F8252 /* AuthRuleExtensionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthRuleExtensionTests.swift; sourceTree = "<group>"; };
11351137
7678B38326017D5300B4917F /* AppSyncErrorTypeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppSyncErrorTypeTests.swift; sourceTree = "<group>"; };
11361138
767F85FB2649FF540076D633 /* CustomerOrder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomerOrder.swift; sourceTree = "<group>"; };
11371139
767F85FD2649FFCC0076D633 /* CustomerOrder+Schema.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CustomerOrder+Schema.swift"; sourceTree = "<group>"; };
@@ -1890,6 +1892,7 @@
18901892
children = (
18911893
2129BE2F2394828A006363A1 /* QueryPredicateGraphQLTests.swift */,
18921894
D83C515F248964780091548E /* ModelGraphQLTests.swift */,
1895+
762F70A326683EE2001F8252 /* AuthRuleExtensionTests.swift */,
18931896
);
18941897
path = Support;
18951898
sourceTree = "<group>";
@@ -5271,6 +5274,7 @@
52715274
FAC23564227A056600424678 /* StorageCategoryClientAPITests.swift in Sources */,
52725275
FAAFAF3123904B75002CF932 /* AtomicValue+BoolTests.swift in Sources */,
52735276
B9AF547E23F37DF20059E6C4 /* TemporalOperationTests.swift in Sources */,
5277+
762F70A426683EE2001F8252 /* AuthRuleExtensionTests.swift in Sources */,
52745278
FAD3937D23820D0200463F5E /* DataStoreCategoryConfigurationTests.swift in Sources */,
52755279
FA607FE2233D131B00DFEA24 /* AmplifyOperationHubTests.swift in Sources */,
52765280
B4944D52251C141200BF0BFE /* JSONValueHolderTest.swift in Sources */,

Amplify/Categories/DataStore/Model/Internal/Schema/AuthRule.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ public enum ModelOperation {
2323
case read
2424
}
2525

26+
/// - Warning: Although this has `public` access, it is intended for internal use and should not be used directly
27+
/// by host applications. The behavior of this may change without warning.
28+
public enum AuthRuleProvider {
29+
case apiKey
30+
case oidc
31+
case iam
32+
case userPools
33+
}
34+
2635
/// - Warning: Although this has `public` access, it is intended for internal use and should not be used directly
2736
/// by host applications. The behavior of this may change without warning.
2837
public typealias AuthRules = [AuthRule]
@@ -37,20 +46,23 @@ public struct AuthRule {
3746
public let groups: [String]
3847
public let groupsField: String?
3948
public let operations: [ModelOperation]
49+
public let provider: AuthRuleProvider?
4050

4151
public init(allow: AuthStrategy,
4252
ownerField: String? = nil,
4353
identityClaim: String? = nil,
4454
groupClaim: String? = nil,
4555
groups: [String] = [],
4656
groupsField: String? = nil,
57+
provider: AuthRuleProvider? = nil,
4758
operations: [ModelOperation] = []) {
4859
self.allow = allow
4960
self.ownerField = ownerField
5061
self.identityClaim = identityClaim
5162
self.groupClaim = groupClaim
5263
self.groups = groups
5364
self.groupsField = groupsField
65+
self.provider = provider
5466
self.operations = operations
5567
}
5668
}

Amplify/Categories/DataStore/Model/Internal/Schema/Model+Schema.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ extension Model {
6363
groupClaim: String? = nil,
6464
groups: [String] = [],
6565
groupsField: String? = nil,
66+
provider: AuthRuleProvider? = nil,
6667
operations: [ModelOperation] = []) -> AuthRule {
6768
return AuthRule(allow: allow,
6869
ownerField: ownerField,
6970
identityClaim: identityClaim,
7071
groupClaim: groupClaim,
7172
groups: groups,
7273
groupsField: groupsField,
74+
provider: provider,
7375
operations: operations)
7476
}
7577
}

AmplifyPlugins/Core/AWSPluginsCore/Model/Support/AuthRule+Extension.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@
77

88
import Amplify
99

10+
extension AuthRuleProvider {
11+
public func toAWSAuthorizationType() throws -> AWSAuthorizationType {
12+
var authType: AWSAuthorizationType
13+
switch self {
14+
case .apiKey:
15+
authType = .apiKey
16+
case .oidc:
17+
authType = .openIDConnect
18+
case .iam:
19+
authType = .awsIAM
20+
case .userPools:
21+
authType = .amazonCognitoUserPools
22+
}
23+
return authType
24+
}
25+
}
26+
1027
extension AuthRule {
1128
func getOwnerFieldOrDefault() -> String {
1229
guard let ownerField = ownerField else {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import XCTest
9+
import Amplify
10+
import AWSPluginsCore
11+
12+
class AuthRuleExtensionTests: XCTestCase {
13+
func testAuthRuleProviderToAWSAuth() throws {
14+
let authRuleProviders: [AuthRuleProvider] = [.apiKey, .oidc, .iam, .userPools]
15+
let expectedAuthTypes: [AWSAuthorizationType] = [
16+
.apiKey,
17+
.openIDConnect,
18+
.awsIAM,
19+
.amazonCognitoUserPools
20+
]
21+
22+
for (index, provider) in authRuleProviders.enumerated() {
23+
XCTAssertEqual(try provider.toAWSAuthorizationType(), expectedAuthTypes[index])
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)