Skip to content

Commit

Permalink
remove unnecessary public apis
Browse files Browse the repository at this point in the history
  • Loading branch information
lawmicha committed Aug 21, 2024
1 parent 60e1dc5 commit 9617027
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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())
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9617027

Please sign in to comment.