Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use AppSyncRealTimeClient 1.0.2 Interceptors #353

Merged
merged 3 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AWSAppSync.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Pod::Spec.new do |s|
s.dependency 'AWSCore', '~> 2.12.0'
s.dependency 'SQLite.swift', '~> 0.12.2'
s.dependency 'ReachabilitySwift', '~> 5.0.0'
s.dependency 'AppSyncRealTimeClient', '~> 1.0.0'
s.dependency 'AppSyncRealTimeClient', '~> 1.0.2'

s.source_files = 'AWSAppSyncClient/AWSAppSync.h', 'AWSAppSyncClient/*.swift', 'AWSAppSyncClient/Internal/**/*.{h,m,swift}', 'AWSAppSyncClient/Apollo/Sources/Apollo/*.swift'
s.public_header_files = ['AWSAppSyncClient/AWSAppSync.h', 'AWSAppSyncClient/AWSAppSync-Swift.h', 'AWSAppSyncClient/Internal/AppSyncLogHelper.h']
Expand Down
87 changes: 32 additions & 55 deletions AWSAppSyncClient.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

23 changes: 0 additions & 23 deletions AWSAppSyncClient/Internal/AuthInterceptor/AppSyncJSONHelper.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import Foundation
import AppSyncRealTimeClient

class IAMAuthInterceptor: AuthInterceptor {

let authProvider: AWSCredentialsProvider
let region: AWSRegionType

init (_ authProvider: AWSCredentialsProvider, region: AWSRegionType) {
self.authProvider = authProvider
self.region = region
}

func interceptMessage(_ message: AppSyncMessage, for endpoint: URL) -> AppSyncMessage {
switch message.messageType {
case .subscribe:
Expand All @@ -32,7 +32,7 @@ class IAMAuthInterceptor: AuthInterceptor {
}
return message
}

func interceptConnection(_ request: AppSyncConnectionRequest, for endpoint: URL) -> AppSyncConnectionRequest {
let url = endpoint.appendingPathComponent(RealtimeProviderConstants.iamConnectPath)
let payloadString = SubscriptionConstants.emptyPayload
Expand Down Expand Up @@ -119,7 +119,7 @@ private class IAMAuthenticationHeader: AuthenticationHeader {
self.contentType = contentType
super.init(host: host)
}

private enum CodingKeys: String, CodingKey {
case authorization = "Authorization"
case accept
Expand All @@ -128,7 +128,7 @@ private class IAMAuthenticationHeader: AuthenticationHeader {
case date = "x-amz-date"
case securityToken = "x-amz-security-token"
}

override func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(authorization, forKey: .authorization)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,12 @@ class APIKeyBasedConnectionPool: SubscriptionConnectionPool {

func connection(for url: URL, connectionType: SubscriptionConnectionType) -> SubscriptionConnection {

let connectionProvider = endPointToProvider[url.absoluteString] ?? createConnectionProvider(for: url, connectionType: connectionType)
let connectionProvider = endPointToProvider[url.absoluteString] ??
ConnectionProviderFactory.createConnectionProvider(for: url,
authInterceptor: APIKeyAuthInterceptor(apiKeyProvider.getAPIKey()),
connectionType: connectionType)
endPointToProvider[url.absoluteString] = connectionProvider
let connection = AppSyncSubscriptionConnection(provider: connectionProvider)
return connection
}

func createConnectionProvider(for url: URL, connectionType: SubscriptionConnectionType) -> ConnectionProvider {
let provider = ConnectionPoolFactory.createConnectionProvider(for: url, connectionType: connectionType)
if let messageInterceptable = provider as? MessageInterceptable {
messageInterceptable.addInterceptor(APIKeyAuthInterceptor(apiKeyProvider))
}
if let connectionInterceptable = provider as? ConnectionInterceptable {
connectionInterceptable.addInterceptor(RealtimeGatewayURLInterceptor())
connectionInterceptable.addInterceptor(APIKeyAuthInterceptor(apiKeyProvider))
}
return provider
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// Licensed under the Amazon Software License
// http://aws.amazon.com/asl/
//

import Foundation
import AppSyncRealTimeClient

class AppSyncRealTimeClientOIDCAuthProvider: OIDCAuthProvider {

let authProvider: AWSOIDCAuthProvider
init(authProvider: AWSOIDCAuthProvider) {
self.authProvider = authProvider
}

func getLatestAuthToken() -> Swift.Result<String, Error> {
var jwtToken: String?
var authError: Error?

if let asyncAuthProvider = authProvider as? AWSCognitoUserPoolsAuthProviderAsync {
let semaphore = DispatchSemaphore(value: 0)
asyncAuthProvider.getLatestAuthToken { (token, error) in
jwtToken = token
authError = error
semaphore.signal()
}
semaphore.wait()

if let error = authError {
return .failure(error)
}

if let token = jwtToken {
return .success(token)
}
}

if let asyncAuthProvider = authProvider as? AWSOIDCAuthProviderAsync {
let semaphore = DispatchSemaphore(value: 0)
asyncAuthProvider.getLatestAuthToken { (token, error) in
jwtToken = token
authError = error
semaphore.signal()
}
semaphore.wait()
if let error = authError {
return .failure(error)
}

if let token = jwtToken {
return .success(token)
}
}

return .success(authProvider.getLatestAuthToken())
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,14 @@ class IAMBasedConnectionPool: SubscriptionConnectionPool {

func connection(for url: URL, connectionType: SubscriptionConnectionType) -> SubscriptionConnection {

let connectionProvider = endPointToProvider[url.absoluteString] ?? createConnectionProvider(for: url, connectionType: connectionType)
let connectionProvider = endPointToProvider[url.absoluteString] ??
ConnectionProviderFactory.createConnectionProvider(for: url,
authInterceptor: IAMAuthInterceptor(credentialProvider,
region: regionType),
connectionType: connectionType)

endPointToProvider[url.absoluteString] = connectionProvider
let connection = AppSyncSubscriptionConnection(provider: connectionProvider)
return connection
}

func createConnectionProvider(for url: URL, connectionType: SubscriptionConnectionType) -> ConnectionProvider {
let provider = ConnectionPoolFactory.createConnectionProvider(for: url, connectionType: connectionType)
if let messageInterceptable = provider as? MessageInterceptable {
messageInterceptable.addInterceptor(IAMAuthInterceptor(credentialProvider, region: regionType))
}
if let connectionInterceptable = provider as? ConnectionInterceptable {
connectionInterceptable.addInterceptor(RealtimeGatewayURLInterceptor())
connectionInterceptable.addInterceptor(IAMAuthInterceptor(credentialProvider, region: regionType))
}

return provider
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,17 @@ class OIDCBasedConnectionPool: SubscriptionConnectionPool {
}

func connection(for url: URL, connectionType: SubscriptionConnectionType) -> SubscriptionConnection {
if let connectionProvider = endPointToProvider[url.absoluteString] {
return AppSyncSubscriptionConnection(provider: connectionProvider)
}

let connectionProvider = endPointToProvider[url.absoluteString] ?? createConnectionProvider(for: url, connectionType: connectionType)
let authProvider = AppSyncRealTimeClientOIDCAuthProvider(authProvider: tokenProvider)
let authInterceptor = OIDCAuthInterceptor(authProvider)
let connectionProvider = ConnectionProviderFactory.createConnectionProvider(for: url,
authInterceptor: authInterceptor,
connectionType: connectionType)
endPointToProvider[url.absoluteString] = connectionProvider
let connection = AppSyncSubscriptionConnection(provider: connectionProvider)
return connection
}

func createConnectionProvider(for url: URL, connectionType: SubscriptionConnectionType) -> ConnectionProvider {
let provider = ConnectionPoolFactory.createConnectionProvider(for: url, connectionType: connectionType)
if let messageInterceptable = provider as? MessageInterceptable {
messageInterceptable.addInterceptor(CognitoUserPoolsAuthInterceptor(tokenProvider))
}
if let connectionInterceptable = provider as? ConnectionInterceptable {
connectionInterceptable.addInterceptor(RealtimeGatewayURLInterceptor())
connectionInterceptable.addInterceptor(CognitoUserPoolsAuthInterceptor(tokenProvider))
}

return provider
return AppSyncSubscriptionConnection(provider: connectionProvider)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,17 @@ class UserPoolsBasedConnectionPool: SubscriptionConnectionPool {
}

func connection(for url: URL, connectionType: SubscriptionConnectionType) -> SubscriptionConnection {
if let connectionProvider = endPointToProvider[url.absoluteString] {
return AppSyncSubscriptionConnection(provider: connectionProvider)
}

let connectionProvider = endPointToProvider[url.absoluteString] ?? createConnectionProvider(for: url, connectionType: connectionType)
let authProvider = AppSyncRealTimeClientOIDCAuthProvider(authProvider: tokenProvider)
let authInterceptor = OIDCAuthInterceptor(authProvider)
let connectionProvider = ConnectionProviderFactory.createConnectionProvider(for: url,
authInterceptor: authInterceptor,
connectionType: connectionType)
endPointToProvider[url.absoluteString] = connectionProvider
let connection = AppSyncSubscriptionConnection(provider: connectionProvider)
return connection
}

func createConnectionProvider(for url: URL, connectionType: SubscriptionConnectionType) -> ConnectionProvider {
let provider = ConnectionPoolFactory.createConnectionProvider(for: url, connectionType: connectionType)
if let messageInterceptable = provider as? MessageInterceptable {
messageInterceptable.addInterceptor(CognitoUserPoolsAuthInterceptor(tokenProvider))
}
if let connectionInterceptable = provider as? ConnectionInterceptable {
connectionInterceptable.addInterceptor(RealtimeGatewayURLInterceptor())
connectionInterceptable.addInterceptor(CognitoUserPoolsAuthInterceptor(tokenProvider))
}

return provider
return AppSyncSubscriptionConnection(provider: connectionProvider)
}

}

This file was deleted.

This file was deleted.

Loading