From 7b23f03b54d27728e931809069ce4f2786e0402d Mon Sep 17 00:00:00 2001 From: Di Wu Date: Mon, 18 Mar 2024 14:02:40 -0700 Subject: [PATCH] rename CognitoAuth to AuthToken --- .../AppSyncRealTimeRequestAuth.swift | 8 ++++---- ...hInterceptor.swift => AuthTokenInterceptor.swift} | 12 ++++++------ .../AppSyncRealTimeClientFactory.swift | 6 +++--- .../AppSyncRealTimeRequestAuthTests.swift | 6 +++--- .../CognitoAuthInterceptorTests.swift | 12 ++++++------ 5 files changed, 22 insertions(+), 22 deletions(-) rename AmplifyPlugins/API/Sources/AWSAPIPlugin/Interceptor/SubscriptionInterceptor/{CognitoAuthInterceptor.swift => AuthTokenInterceptor.swift} (90%) diff --git a/AmplifyPlugins/API/Sources/AWSAPIPlugin/AppSyncRealTimeClient/AppSyncRealTimeRequestAuth.swift b/AmplifyPlugins/API/Sources/AWSAPIPlugin/AppSyncRealTimeClient/AppSyncRealTimeRequestAuth.swift index c87d439b62..87e01b1842 100644 --- a/AmplifyPlugins/API/Sources/AWSAPIPlugin/AppSyncRealTimeClient/AppSyncRealTimeRequestAuth.swift +++ b/AmplifyPlugins/API/Sources/AWSAPIPlugin/AppSyncRealTimeClient/AppSyncRealTimeRequestAuth.swift @@ -9,11 +9,11 @@ import Foundation public enum AppSyncRealTimeRequestAuth { - case cognito(CognitoAuth) + case authToken(AuthToken) case apiKey(ApiKey) case iam(IAM) - public struct CognitoAuth { + public struct AuthToken { let host: String let authToken: String } @@ -67,7 +67,7 @@ extension AppSyncRealTimeRequestAuth: Encodable { switch self { case .apiKey(let apiKey): try container.encode(apiKey) - case .cognito(let cognito): + case .authToken(let cognito): try container.encode(cognito) case .iam(let iam): try container.encode(iam) @@ -75,7 +75,7 @@ extension AppSyncRealTimeRequestAuth: Encodable { } } -extension AppSyncRealTimeRequestAuth.CognitoAuth: Encodable { +extension AppSyncRealTimeRequestAuth.AuthToken: Encodable { enum CodingKeys: String, CodingKey { case host case authToken = "Authorization" diff --git a/AmplifyPlugins/API/Sources/AWSAPIPlugin/Interceptor/SubscriptionInterceptor/CognitoAuthInterceptor.swift b/AmplifyPlugins/API/Sources/AWSAPIPlugin/Interceptor/SubscriptionInterceptor/AuthTokenInterceptor.swift similarity index 90% rename from AmplifyPlugins/API/Sources/AWSAPIPlugin/Interceptor/SubscriptionInterceptor/CognitoAuthInterceptor.swift rename to AmplifyPlugins/API/Sources/AWSAPIPlugin/Interceptor/SubscriptionInterceptor/AuthTokenInterceptor.swift index c82046bcec..b0f19ffd78 100644 --- a/AmplifyPlugins/API/Sources/AWSAPIPlugin/Interceptor/SubscriptionInterceptor/CognitoAuthInterceptor.swift +++ b/AmplifyPlugins/API/Sources/AWSAPIPlugin/Interceptor/SubscriptionInterceptor/AuthTokenInterceptor.swift @@ -11,7 +11,7 @@ import Amplify /// General purpose authenticatication subscriptions interceptor for providers whose only /// requirement is to provide an authentication token via the "Authorization" header -class CognitoAuthInterceptor { +class AuthTokenInterceptor { let getLatestAuthToken: () async throws -> String? @@ -37,7 +37,7 @@ class CognitoAuthInterceptor { } } -extension CognitoAuthInterceptor: AppSyncRequestInterceptor { +extension AuthTokenInterceptor: AppSyncRequestInterceptor { func interceptRequest(event: AppSyncRealTimeRequest, url: URL) async -> AppSyncRealTimeRequest { guard case .start(let request) = event else { return event @@ -48,7 +48,7 @@ extension CognitoAuthInterceptor: AppSyncRequestInterceptor { return .start(.init( id: request.id, data: request.data, - auth: .cognito(.init( + auth: .authToken(.init( host: AppSyncRealTimeClientFactory.appSyncApiEndpoint(url).host!, authToken: authToken )) @@ -56,12 +56,12 @@ extension CognitoAuthInterceptor: AppSyncRequestInterceptor { } } -extension CognitoAuthInterceptor: WebSocketInterceptor { +extension AuthTokenInterceptor: WebSocketInterceptor { func interceptConnection(url: URL) async -> URL { let authToken = await getAuthToken() return AppSyncRealTimeRequestAuth.URLQuery( - header: .cognito(.init( + header: .authToken(.init( host: AppSyncRealTimeClientFactory.appSyncApiEndpoint(url).host!, authToken: authToken )) @@ -70,7 +70,7 @@ extension CognitoAuthInterceptor: WebSocketInterceptor { } // MARK: AuthorizationTokenAuthInterceptor + DefaultLogger -extension CognitoAuthInterceptor: DefaultLogger { +extension AuthTokenInterceptor: DefaultLogger { public static var log: Logger { Amplify.Logging.logger(forCategory: CategoryType.api.displayName, forNamespace: String(describing: self)) } diff --git a/AmplifyPlugins/API/Sources/AWSAPIPlugin/SubscriptionFactory/AppSyncRealTimeClientFactory.swift b/AmplifyPlugins/API/Sources/AWSAPIPlugin/SubscriptionFactory/AppSyncRealTimeClientFactory.swift index 0224e1a236..b97459c0e1 100644 --- a/AmplifyPlugins/API/Sources/AWSAPIPlugin/SubscriptionFactory/AppSyncRealTimeClientFactory.swift +++ b/AmplifyPlugins/API/Sources/AWSAPIPlugin/SubscriptionFactory/AppSyncRealTimeClientFactory.swift @@ -95,7 +95,7 @@ actor AppSyncRealTimeClientFactory: AppSyncRealTimeClientFactoryProtocol { return APIKeyAuthInterceptor(apiKey: apiKeyConfiguration.apiKey) case .amazonCognitoUserPools: let provider = AWSOIDCAuthProvider(authService: authService) - return CognitoAuthInterceptor(getLatestAuthToken: provider.getLatestAuthToken) + return AuthTokenInterceptor(getLatestAuthToken: provider.getLatestAuthToken) case .awsIAM(let awsIAMConfiguration): return IAMAuthInterceptor(authService.getCredentialsProvider(), region: awsIAMConfiguration.region) @@ -105,14 +105,14 @@ actor AppSyncRealTimeClientFactory: AppSyncRealTimeClientFactoryProtocol { "Using openIDConnect requires passing in an APIAuthProvider with an OIDC AuthProvider", "When instantiating AWSAPIPlugin pass in an instance of APIAuthProvider", nil) } - return CognitoAuthInterceptor(getLatestAuthToken: oidcAuthProvider.getLatestAuthToken) + return AuthTokenInterceptor(getLatestAuthToken: oidcAuthProvider.getLatestAuthToken) case .function: guard let functionAuthProvider = apiAuthProviderFactory.functionAuthProvider() else { throw APIError.invalidConfiguration( "Using function as auth provider requires passing in an APIAuthProvider with a Function AuthProvider", "When instantiating AWSAPIPlugin pass in an instance of APIAuthProvider", nil) } - return CognitoAuthInterceptor(authTokenProvider: functionAuthProvider) + return AuthTokenInterceptor(authTokenProvider: functionAuthProvider) case .none: throw APIError.unknown("Cannot create AppSync subscription for none auth mode", "") } diff --git a/AmplifyPlugins/API/Tests/AWSAPIPluginTests/AppSyncRealTimeClient/AppSyncRealTimeRequestAuthTests.swift b/AmplifyPlugins/API/Tests/AWSAPIPluginTests/AppSyncRealTimeClient/AppSyncRealTimeRequestAuthTests.swift index 1755543d72..6ab7af0692 100644 --- a/AmplifyPlugins/API/Tests/AWSAPIPluginTests/AppSyncRealTimeClient/AppSyncRealTimeRequestAuthTests.swift +++ b/AmplifyPlugins/API/Tests/AWSAPIPluginTests/AppSyncRealTimeClient/AppSyncRealTimeRequestAuthTests.swift @@ -24,7 +24,7 @@ class AppSyncRealTimeRequestAuthTests: XCTestCase { }() func testAppSyncRealTimeRequestAuth_encodeCognito() { - let cognitoAuth = AppSyncRealTimeRequestAuth.CognitoAuth(host: host, authToken: token) + let cognitoAuth = AppSyncRealTimeRequestAuth.AuthToken(host: host, authToken: token) XCTAssertEqual(toJson(cognitoAuth)?.shrink(), """ { "Authorization": "\(token)", @@ -67,7 +67,7 @@ class AppSyncRealTimeRequestAuthTests: XCTestCase { } func testAppSyncRealTimeRequestAuth_encodeStartRequestWithCognitoAuth() { - let auth: AppSyncRealTimeRequestAuth = .cognito(.init(host: host, authToken: token)) + let auth: AppSyncRealTimeRequestAuth = .authToken(.init(host: host, authToken: token)) let request = AppSyncRealTimeRequest.start( .init(id: id, data: data, auth: auth) ) @@ -155,7 +155,7 @@ class AppSyncRealTimeRequestAuthTests: XCTestCase { &payload=e30%3D """ let encodedURL = AppSyncRealTimeRequestAuth.URLQuery( - header: .cognito(.init( + header: .authToken(.init( host: "example.com", authToken: "49859c7c-7405-4d58-aff7-52be4b473557" )) diff --git a/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Interceptor/SubscriptionInterceptor/CognitoAuthInterceptorTests.swift b/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Interceptor/SubscriptionInterceptor/CognitoAuthInterceptorTests.swift index 6b65516e27..4127f018fd 100644 --- a/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Interceptor/SubscriptionInterceptor/CognitoAuthInterceptorTests.swift +++ b/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Interceptor/SubscriptionInterceptor/CognitoAuthInterceptorTests.swift @@ -15,7 +15,7 @@ class CognitoAuthInterceptorTests: XCTestCase { func testInterceptConnection_withAuthTokenProvider_appendCorrectAuthHeaderToQuery() async { let authTokenProvider = MockAuthTokenProvider() - let interceptor = CognitoAuthInterceptor(authTokenProvider: authTokenProvider) + let interceptor = AuthTokenInterceptor(authTokenProvider: authTokenProvider) let decoratedURL = await interceptor.interceptConnection(url: URL(string: "https://example.com")!) guard let components = URLComponents(url: decoratedURL, resolvingAgainstBaseURL: false) else { @@ -41,7 +41,7 @@ class CognitoAuthInterceptorTests: XCTestCase { func testInterceptConnection_withAuthTokenProviderFailed_appendEmptyAuthHeaderToQuery() async { let authTokenProvider = MockAuthTokenProviderFailed() - let interceptor = CognitoAuthInterceptor(authTokenProvider: authTokenProvider) + let interceptor = AuthTokenInterceptor(authTokenProvider: authTokenProvider) let decoratedURL = await interceptor.interceptConnection(url: URL(string: "https://example.com")!) guard let components = URLComponents(url: decoratedURL, resolvingAgainstBaseURL: false) else { @@ -67,7 +67,7 @@ class CognitoAuthInterceptorTests: XCTestCase { func testInterceptRequest_withAuthTokenProvider_appendCorrectAuthInfoToPayload() async { let authTokenProvider = MockAuthTokenProvider() - let interceptor = CognitoAuthInterceptor(authTokenProvider: authTokenProvider) + let interceptor = AuthTokenInterceptor(authTokenProvider: authTokenProvider) let decoratedRequest = await interceptor.interceptRequest( event: .start(.init(id: UUID().uuidString, data: UUID().uuidString, auth: nil)), url: URL(string: "https://example.com")! @@ -78,7 +78,7 @@ class CognitoAuthInterceptorTests: XCTestCase { return } - guard case let .some(.cognito(authInfo)) = decoratedAuth.auth else { + guard case let .some(.authToken(authInfo)) = decoratedAuth.auth else { XCTFail("Failed to extract authInfo from decoratedAuth") return } @@ -89,7 +89,7 @@ class CognitoAuthInterceptorTests: XCTestCase { func testInterceptRequest_withAuthTokenProviderFailed_appendEmptyAuthInfoToPayload() async { let authTokenProvider = MockAuthTokenProviderFailed() - let interceptor = CognitoAuthInterceptor(authTokenProvider: authTokenProvider) + let interceptor = AuthTokenInterceptor(authTokenProvider: authTokenProvider) let decoratedRequest = await interceptor.interceptRequest( event: .start(.init(id: UUID().uuidString, data: UUID().uuidString, auth: nil)), url: URL(string: "https://example.com")! @@ -100,7 +100,7 @@ class CognitoAuthInterceptorTests: XCTestCase { return } - guard case let .some(.cognito(authInfo)) = decoratedAuth.auth else { + guard case let .some(.authToken(authInfo)) = decoratedAuth.auth else { XCTFail("Failed to extract authInfo from decoratedAuth") return }