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

feat(Auth): Adding network preferences #3379

Merged
merged 5 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ extension AWSCognitoAuthPlugin {
configuration.httpClientEngine = .userAgentEngine(for: configuration)
}

if let requestTimeout = networkPreferences?.timeoutIntervalForRequest {
let requestTimeOutMs = requestTimeout * 1_000
configuration.connectTimeoutMs = UInt32(requestTimeOutMs)
}

if let maxRetryUnwrapped = networkPreferences?.maxRetryCount {
configuration.retryStrategyOptions = RetryStrategyOptions(maxRetriesBase: Int(maxRetryUnwrapped))
lawmicha marked this conversation as resolved.
Show resolved Hide resolved
}

return CognitoIdentityProviderClient(config: configuration)
default:
fatalError()
Expand All @@ -116,6 +125,15 @@ extension AWSCognitoAuthPlugin {
)
configuration.httpClientEngine = .userAgentEngine(for: configuration)

if let requestTimeout = networkPreferences?.timeoutIntervalForRequest {
let requestTimeOutMs = requestTimeout * 1_000
configuration.connectTimeoutMs = UInt32(requestTimeOutMs)
}

if let maxRetryUnwrapped = networkPreferences?.maxRetryCount {
configuration.retryStrategyOptions = RetryStrategyOptions(maxRetriesBase: Int(maxRetryUnwrapped))
}

return CognitoIdentityClient(config: configuration)
default:
fatalError()
Expand All @@ -129,6 +147,15 @@ extension AWSCognitoAuthPlugin {
private func makeURLSession() -> URLSession {
let configuration = URLSessionConfiguration.default
configuration.urlCache = nil

if let timeoutIntervalForRequest = networkPreferences?.timeoutIntervalForRequest {
configuration.timeoutIntervalForRequest = timeoutIntervalForRequest
}

if let timeoutIntervalForResource = networkPreferences?.timeoutIntervalForResource {
configuration.timeoutIntervalForResource = timeoutIntervalForResource
}

return URLSession(configuration: configuration)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public final class AWSCognitoAuthPlugin: AWSCognitoAuthPluginBehavior {

var httpClientEngineProxy: HttpClientEngineProxy?

/// The user network preferences for timeout and retry
let networkPreferences: AWSCognitoNetworkPreferences?

@_spi(InternalAmplifyConfiguration)
internal(set) public var jsonConfiguration: JSONValue?

Expand All @@ -42,5 +45,13 @@ public final class AWSCognitoAuthPlugin: AWSCognitoAuthPluginBehavior {

/// Instantiates an instance of the AWSCognitoAuthPlugin.
public init() {
self.networkPreferences = nil
}

/// Instantiates an instance of the AWSCognitoAuthPlugin with custom network preferences
/// - Parameters:
/// - networkPreferences: network preferences
public init(networkPreferences: AWSCognitoNetworkPreferences) {
self.networkPreferences = networkPreferences
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import Foundation

public struct AWSCognitoNetworkPreferences {

/// The maximum number of retries for failed requests. The value needs to be between 0 and 10 inclusive.
/// If set to higher than 10, it becomes 10.
public let maxRetryCount: UInt32
lawmicha marked this conversation as resolved.
Show resolved Hide resolved

/// The timeout interval to use when waiting for additional data.
public let timeoutIntervalForRequest: Double
lawmicha marked this conversation as resolved.
Show resolved Hide resolved

/// The maximum amount of time that a resource request should be allowed to take.
///
/// NOTE: Resource time out is only applicable to HostedUI ATM, because the underlying Swift SDK does
harsh62 marked this conversation as resolved.
Show resolved Hide resolved
/// not support resource timeouts
public let timeoutIntervalForResource: Double?

public init(maxRetryCount: UInt32,
timeoutIntervalForRequest: Double,
timeoutIntervalForResource: Double? = nil) {
self.maxRetryCount = maxRetryCount
self.timeoutIntervalForRequest = timeoutIntervalForRequest
self.timeoutIntervalForResource = timeoutIntervalForResource
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,50 @@ class AWSCognitoAuthPluginConfigTests: XCTestCase {
}
}

/// Test Auth configuration with valid config for user pool and identity pool, with network preferences
///
/// - Given: Given valid config for user pool and identity pool, and network preferences
/// - When:
/// - I configure auth with the given configuration and network preferences
/// - Then:
/// - I should not get any error while configuring auth
///
func testConfigWithUserPoolAndIdentityPoolWithNetworkPreferences() throws {
let plugin = AWSCognitoAuthPlugin(
networkPreferences: .init(
maxRetryCount: 2,
timeoutIntervalForRequest: 60,
timeoutIntervalForResource: 60))
try Amplify.add(plugin: plugin)

let categoryConfig = AuthCategoryConfiguration(plugins: [
"awsCognitoAuthPlugin": [
"CredentialsProvider": ["CognitoIdentity": ["Default":
["PoolId": "xx",
"Region": "us-east-1"]
]],
"CognitoUserPool": ["Default": [
"PoolId": "xx",
"Region": "us-east-1",
"AppClientId": "xx",
"AppClientSecret": "xx"]]
]
])
let amplifyConfig = AmplifyConfiguration(auth: categoryConfig)
do {
try Amplify.configure(amplifyConfig)

let escapeHatch = plugin.getEscapeHatch()
guard case .userPoolAndIdentityPool(let userPoolClient, let identityPoolClient) = escapeHatch else {
XCTFail("Expected .userPool, got \(escapeHatch)")
return
}
XCTAssertNotNil(userPoolClient)
XCTAssertNotNil(identityPoolClient)

} catch {
XCTFail("Should not throw error. \(error)")
}
}

}
Loading