diff --git a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/AWSCognitoAuthPlugin+AppSyncSigner.swift b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/AWSCognitoAuthPlugin+AppSyncSigner.swift index 0f7a15d304..c0fb27d26a 100644 --- a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/AWSCognitoAuthPlugin+AppSyncSigner.swift +++ b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/AWSCognitoAuthPlugin+AppSyncSigner.swift @@ -6,23 +6,25 @@ // import Foundation +import Amplify // Amplify.Auth +import AWSPluginsCore // AuthAWSCredentialsProvider import AWSClientRuntime // AWSClientRuntime.CredentialsProviding import ClientRuntime // SdkHttpRequestBuilder -import InternalAmplifyCredentials // AmplifyAWSCredentialsProvider() import AwsCommonRuntimeKit // CommonRuntimeKit.initialize() extension AWSCognitoAuthPlugin { public static func createAppSyncSigner(region: String) -> ((URLRequest) async throws -> URLRequest) { return { request in - try await signAppSyncRequest(request, region: region) + try await signAppSyncRequest(request, + region: region) } } - public static func signAppSyncRequest(_ urlRequest: URLRequest, - region: Swift.String, - credentialsProvider: AWSClientRuntime.CredentialsProviding = AmplifyAWSCredentialsProvider(), - signingName: Swift.String = "appsync", - date: ClientRuntime.Date = Date()) async throws -> URLRequest { + + static func signAppSyncRequest(_ urlRequest: URLRequest, + region: Swift.String, + signingName: Swift.String = "appsync", + date: ClientRuntime.Date = Date()) async throws -> URLRequest { CommonRuntimeKit.initialize() // Convert URLRequest to SDK's HTTPRequest @@ -32,7 +34,15 @@ extension AWSCognitoAuthPlugin { } // Retrieve the credentials from credentials provider - let credentials = try await credentialsProvider.getCredentials() + let credentials: AWSClientRuntime.AWSCredentials + let authSession = try await Amplify.Auth.fetchAuthSession() + if let awsCredentialsProvider = authSession as? AuthAWSCredentialsProvider { + let awsCredentials = try awsCredentialsProvider.getAWSCredentials().get() + credentials = awsCredentials.toAWSSDKCredentials() + } else { + let error = AuthError.unknown("Auth session does not include AWS credentials information") + throw error + } // Prepare signing let flags = SigningFlags(useDoubleURIEncode: true, @@ -100,3 +110,22 @@ extension AWSCognitoAuthPlugin { return requestBuilder } } + +extension AWSPluginsCore.AWSCredentials { + + func toAWSSDKCredentials() -> AWSClientRuntime.AWSCredentials { + if let tempCredentials = self as? AWSTemporaryCredentials { + return AWSClientRuntime.AWSCredentials( + accessKey: tempCredentials.accessKeyId, + secret: tempCredentials.secretAccessKey, + expirationTimeout: tempCredentials.expiration, + sessionToken: tempCredentials.sessionToken) + } else { + return AWSClientRuntime.AWSCredentials( + accessKey: accessKeyId, + secret: secretAccessKey, + expirationTimeout: Date()) + } + + } +} diff --git a/AmplifyPlugins/Auth/Tests/AuthHostApp/AuthIntegrationTests/AppSyncSignerTests/AppSyncSignerTests.swift b/AmplifyPlugins/Auth/Tests/AuthHostApp/AuthIntegrationTests/AppSyncSignerTests/AppSyncSignerTests.swift index d4311b4885..02cedefdfb 100644 --- a/AmplifyPlugins/Auth/Tests/AuthHostApp/AuthIntegrationTests/AppSyncSignerTests/AppSyncSignerTests.swift +++ b/AmplifyPlugins/Auth/Tests/AuthHostApp/AuthIntegrationTests/AppSyncSignerTests/AppSyncSignerTests.swift @@ -15,14 +15,14 @@ class AppSyncSignerTests: AWSAuthBaseTest { /// /// - Given: Base test configures Amplify and adds AWSCognitoAuthPlugin /// - When: - /// - I invoke AWSCognitoAuthPlugin.signAppSyncRequest(request, region) + /// - I invoke AWSCognitoAuthPlugin's AppSync signer /// - Then: /// - I should get a signed request. /// func testSignAppSyncRequest() async throws { let request = URLRequest(url: URL(string: "http://graphql.com")!) - let signedRequest = try await AWSCognitoAuthPlugin.signAppSyncRequest(request, region: "us-east-1") - + let signer = AWSCognitoAuthPlugin.createAppSyncSigner(region: "us-east-1") + let signedRequest = try await signer(request) guard let headers = signedRequest.allHTTPHeaderFields else { XCTFail("Missing headers") return diff --git a/AmplifyPlugins/Core/AmplifyCredentials/AmplifyAWSCredentialsProvider.swift b/AmplifyPlugins/Core/AmplifyCredentials/AmplifyAWSCredentialsProvider.swift index 633ae9e986..8a45c1d64a 100644 --- a/AmplifyPlugins/Core/AmplifyCredentials/AmplifyAWSCredentialsProvider.swift +++ b/AmplifyPlugins/Core/AmplifyCredentials/AmplifyAWSCredentialsProvider.swift @@ -12,10 +12,7 @@ import AWSPluginsCore import Foundation public class AmplifyAWSCredentialsProvider: AWSClientRuntime.CredentialsProviding { - - public init() { - } - + public func getCredentials() async throws -> AWSClientRuntime.AWSCredentials { let authSession = try await Amplify.Auth.fetchAuthSession() if let awsCredentialsProvider = authSession as? AuthAWSCredentialsProvider {