Skip to content

Commit 56d5339

Browse files
harsh62sebaland
andauthored
feat(Auth): Adding network preferences (#3379)
* feat(Auth): Adding network preferences * adding tests * Update AWSCognitoNetworkPreferences.swift * Update AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Models/AWSCognitoNetworkPreferences.swift Co-authored-by: Sebastian Villena <[email protected]> * worked on review comments --------- Co-authored-by: Sebastian Villena <[email protected]>
1 parent 046c7aa commit 56d5339

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/AWSCognitoAuthPlugin+Configure.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ extension AWSCognitoAuthPlugin {
102102
configuration.httpClientEngine = .userAgentEngine(for: configuration)
103103
}
104104

105+
if let requestTimeout = networkPreferences?.timeoutIntervalForRequest {
106+
let requestTimeOutMs = requestTimeout * 1_000
107+
configuration.connectTimeoutMs = UInt32(requestTimeOutMs)
108+
}
109+
110+
if let maxRetryUnwrapped = networkPreferences?.maxRetryCount {
111+
configuration.retryStrategyOptions = RetryStrategyOptions(maxRetriesBase: Int(maxRetryUnwrapped))
112+
}
113+
105114
return CognitoIdentityProviderClient(config: configuration)
106115
default:
107116
fatalError()
@@ -116,6 +125,15 @@ extension AWSCognitoAuthPlugin {
116125
)
117126
configuration.httpClientEngine = .userAgentEngine(for: configuration)
118127

128+
if let requestTimeout = networkPreferences?.timeoutIntervalForRequest {
129+
let requestTimeOutMs = requestTimeout * 1_000
130+
configuration.connectTimeoutMs = UInt32(requestTimeOutMs)
131+
}
132+
133+
if let maxRetryUnwrapped = networkPreferences?.maxRetryCount {
134+
configuration.retryStrategyOptions = RetryStrategyOptions(maxRetriesBase: Int(maxRetryUnwrapped))
135+
}
136+
119137
return CognitoIdentityClient(config: configuration)
120138
default:
121139
fatalError()
@@ -129,6 +147,15 @@ extension AWSCognitoAuthPlugin {
129147
private func makeURLSession() -> URLSession {
130148
let configuration = URLSessionConfiguration.default
131149
configuration.urlCache = nil
150+
151+
if let timeoutIntervalForRequest = networkPreferences?.timeoutIntervalForRequest {
152+
configuration.timeoutIntervalForRequest = timeoutIntervalForRequest
153+
}
154+
155+
if let timeoutIntervalForResource = networkPreferences?.timeoutIntervalForResource {
156+
configuration.timeoutIntervalForResource = timeoutIntervalForResource
157+
}
158+
132159
return URLSession(configuration: configuration)
133160
}
134161

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/AWSCognitoAuthPlugin.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public final class AWSCognitoAuthPlugin: AWSCognitoAuthPluginBehavior {
3232

3333
var httpClientEngineProxy: HttpClientEngineProxy?
3434

35+
/// The user network preferences for timeout and retry
36+
let networkPreferences: AWSCognitoNetworkPreferences?
37+
3538
@_spi(InternalAmplifyConfiguration)
3639
internal(set) public var jsonConfiguration: JSONValue?
3740

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

4346
/// Instantiates an instance of the AWSCognitoAuthPlugin.
4447
public init() {
48+
self.networkPreferences = nil
49+
}
50+
51+
/// Instantiates an instance of the AWSCognitoAuthPlugin with custom network preferences
52+
/// - Parameters:
53+
/// - networkPreferences: network preferences
54+
public init(networkPreferences: AWSCognitoNetworkPreferences) {
55+
self.networkPreferences = networkPreferences
4556
}
4657
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import Foundation
9+
10+
public struct AWSCognitoNetworkPreferences {
11+
12+
/// The maximum number of retries for failed requests.
13+
public let maxRetryCount: UInt32
14+
15+
/// The timeout interval to use when waiting for additional data.
16+
public let timeoutIntervalForRequest: TimeInterval
17+
18+
/// The maximum amount of time that a resource request should be allowed to take.
19+
///
20+
/// NOTE: This value is only applicable to HostedUI because the underlying Swift SDK does
21+
/// not support resource timeouts
22+
public let timeoutIntervalForResource: TimeInterval?
23+
24+
public init(maxRetryCount: UInt32,
25+
timeoutIntervalForRequest: TimeInterval,
26+
timeoutIntervalForResource: TimeInterval? = nil) {
27+
self.maxRetryCount = maxRetryCount
28+
self.timeoutIntervalForRequest = timeoutIntervalForRequest
29+
self.timeoutIntervalForResource = timeoutIntervalForResource
30+
}
31+
}

AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/ConfigurationTests/AWSCognitoAuthPluginConfigTests.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,50 @@ class AWSCognitoAuthPluginConfigTests: XCTestCase {
190190
}
191191
}
192192

193+
/// Test Auth configuration with valid config for user pool and identity pool, with network preferences
194+
///
195+
/// - Given: Given valid config for user pool and identity pool, and network preferences
196+
/// - When:
197+
/// - I configure auth with the given configuration and network preferences
198+
/// - Then:
199+
/// - I should not get any error while configuring auth
200+
///
201+
func testConfigWithUserPoolAndIdentityPoolWithNetworkPreferences() throws {
202+
let plugin = AWSCognitoAuthPlugin(
203+
networkPreferences: .init(
204+
maxRetryCount: 2,
205+
timeoutIntervalForRequest: 60,
206+
timeoutIntervalForResource: 60))
207+
try Amplify.add(plugin: plugin)
208+
209+
let categoryConfig = AuthCategoryConfiguration(plugins: [
210+
"awsCognitoAuthPlugin": [
211+
"CredentialsProvider": ["CognitoIdentity": ["Default":
212+
["PoolId": "xx",
213+
"Region": "us-east-1"]
214+
]],
215+
"CognitoUserPool": ["Default": [
216+
"PoolId": "xx",
217+
"Region": "us-east-1",
218+
"AppClientId": "xx",
219+
"AppClientSecret": "xx"]]
220+
]
221+
])
222+
let amplifyConfig = AmplifyConfiguration(auth: categoryConfig)
223+
do {
224+
try Amplify.configure(amplifyConfig)
225+
226+
let escapeHatch = plugin.getEscapeHatch()
227+
guard case .userPoolAndIdentityPool(let userPoolClient, let identityPoolClient) = escapeHatch else {
228+
XCTFail("Expected .userPool, got \(escapeHatch)")
229+
return
230+
}
231+
XCTAssertNotNil(userPoolClient)
232+
XCTAssertNotNil(identityPoolClient)
233+
234+
} catch {
235+
XCTFail("Should not throw error. \(error)")
236+
}
237+
}
238+
193239
}

0 commit comments

Comments
 (0)