Skip to content

Commit

Permalink
rename CognitoAuth to AuthToken
Browse files Browse the repository at this point in the history
  • Loading branch information
5d committed Mar 18, 2024
1 parent b5e5c8e commit 7b23f03
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -67,15 +67,15 @@ 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)
}
}
}

extension AppSyncRealTimeRequestAuth.CognitoAuth: Encodable {
extension AppSyncRealTimeRequestAuth.AuthToken: Encodable {
enum CodingKeys: String, CodingKey {
case host
case authToken = "Authorization"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand All @@ -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
Expand All @@ -48,20 +48,20 @@ 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
))
))
}
}

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

0 comments on commit 7b23f03

Please sign in to comment.