Skip to content

Commit cd975ee

Browse files
committed
feat: Implement Cognito Credentials Provider
1 parent 99f4724 commit cd975ee

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

AWSSDKSwiftCLI/Tests/AWSSDKSwiftCLITests/Models/PackageManifestBuilderTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class PackageManifestBuilderTests: XCTestCase {
3333
let internalAWSSTSDependencies: [Target.Dependency] = []
3434
let internalAWSSSODependencies: [Target.Dependency] = []
3535
let internalAWSSSOOIDCDependencies: [Target.Dependency] = []
36+
let internalAWSCognitoIdentityDependencies: [Target.Dependency] = []
3637
3738
<contents of base package>
3839
"""
@@ -75,6 +76,7 @@ class PackageManifestBuilderTests: XCTestCase {
7576
let internalAWSSTSDependencies: [Target.Dependency] = []
7677
let internalAWSSSODependencies: [Target.Dependency] = []
7778
let internalAWSSSOOIDCDependencies: [Target.Dependency] = []
79+
let internalAWSCognitoIdentityDependencies: [Target.Dependency] = []
7880
7981
<contents of base package>
8082
"""

Sources/Core/AWSSDKIdentity/Sources/AWSSDKIdentity/AWSCredentialIdentityResolvers/CognitoAWSCredentialIdentityResolver.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,17 @@ import struct Foundation.Date
1616
@_spi(FileBasedConfig) import AWSSDKCommon
1717

1818
protocol CognitoIdentityClientProtocol {
19-
func getId(identityPoolId: String, logins: [String: String]?, region: String) async throws -> String
20-
func getCredentialsForIdentity(identityId: String, logins: [String: String]?, region: String) async throws -> AWSCredentialIdentity
19+
func getId(
20+
identityPoolId: String,
21+
logins: [String: String]?,
22+
region: String
23+
) async throws -> String
24+
25+
func getCredentialsForIdentity(
26+
identityId: String,
27+
logins: [String: String]?,
28+
region: String
29+
) async throws -> AWSCredentialIdentity
2130
}
2231

2332
extension IdentityProvidingCognitoIdentityClient: CognitoIdentityClientProtocol {}
@@ -27,15 +36,15 @@ public actor CognitoAWSCredentialIdentityResolver: AWSCredentialIdentityResolver
2736
private let config: CognitoCredentialsConfiguration
2837
private var logins: [String: String]?
2938
private var cache = CognitoCredentialsCache()
30-
private let client: CognitoIdentityClientProtocol
39+
private nonisolated let client: CognitoIdentityClientProtocol
3140

3241
public init(
3342
identityPoolId: String? = nil,
3443
identityId: String? = nil,
3544
accountId: String? = nil,
3645
logins: [String: String]? = nil,
3746
customRoleArn: String? = nil,
38-
cognitoPoolRegion: String? = nil,
47+
cognitoPoolRegion: String? = nil
3948
) throws {
4049
self.config = try CognitoCredentialsConfiguration(
4150
identityPoolId: identityPoolId,

Sources/Core/AWSSDKIdentity/Sources/AWSSDKIdentity/IdentityClientProvider/IdentityProvidingCognitoIdentityClient.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,43 @@ import Foundation
99
import InternalAWSCognitoIdentity
1010

1111
struct IdentityProvidingCognitoIdentityClient: Swift.Sendable {
12-
1312
func getId(
1413
identityPoolId: String,
1514
logins: [String: String]?,
1615
region: String
1716
) async throws -> String {
1817
let config = try await CognitoIdentityClient.CognitoIdentityClientConfiguration(region: region)
1918
let client = CognitoIdentityClient(config: config)
20-
2119
let input = GetIdInput(identityPoolId: identityPoolId, logins: logins)
2220
let output = try await client.getId(input: input)
23-
21+
2422
guard let identityId = output.identityId else {
2523
throw AWSCredentialIdentityResolverError.failedToResolveAWSCredentials(
2624
"CognitoAWSCredentialIdentityResolver: Failed to get identity ID from Cognito Identity"
2725
)
2826
}
29-
27+
3028
return identityId
3129
}
32-
30+
3331
func getCredentialsForIdentity(
3432
identityId: String,
3533
logins: [String: String]?,
3634
region: String
3735
) async throws -> AWSCredentialIdentity {
3836
let config = try await CognitoIdentityClient.CognitoIdentityClientConfiguration(region: region)
3937
let client = CognitoIdentityClient(config: config)
40-
4138
let input = GetCredentialsForIdentityInput(identityId: identityId, logins: logins)
4239
let output = try await client.getCredentialsForIdentity(input: input)
43-
40+
4441
guard let credentials = output.credentials,
4542
let accessKey = credentials.accessKeyId,
4643
let secretKey = credentials.secretKey else {
4744
throw AWSCredentialIdentityResolverError.failedToResolveAWSCredentials(
4845
"CognitoAWSCredentialIdentityResolver: Failed to get credentials from Cognito Identity"
4946
)
5047
}
51-
48+
5249
return AWSCredentialIdentity(
5350
accessKey: accessKey,
5451
secret: secretKey,

0 commit comments

Comments
 (0)