Skip to content

Commit fb0cf64

Browse files
authored
feat(auth): Add support of custom endpoint for CognitoUserPool (#1715)
1 parent 9e1b2c3 commit fb0cf64

File tree

3 files changed

+119
-6
lines changed

3 files changed

+119
-6
lines changed

AmplifyPlugins/Auth/AWSCognitoAuthPlugin/AWSCognitoAuthPlugin+Configure.swift

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ extension AWSCognitoAuthPlugin {
6666

6767
func awsMobileClientAdapter(from authConfiguration: JSONValue) throws -> AWSMobileClientBehavior {
6868
let identityPoolConfig = identityPoolServiceConfiguration(from: authConfiguration)
69-
let userPoolConfig = userPoolServiceConfiguration(from: authConfiguration)
69+
let userPoolConfig = try userPoolServiceConfiguration(from: authConfiguration)
7070

7171
// Auth plugin require atleast one of the Cognito service to work. Throw an error if both the service
7272
// configuration are nil.
@@ -100,16 +100,52 @@ extension AWSCognitoAuthPlugin {
100100
return AmplifyAWSServiceConfiguration(region: region, credentialsProvider: anonymousCredentialProvider)
101101
}
102102

103-
func userPoolServiceConfiguration(from authConfiguration: JSONValue) -> AmplifyAWSServiceConfiguration? {
103+
func userPoolServiceConfiguration(from authConfiguration: JSONValue) throws -> AmplifyAWSServiceConfiguration? {
104104
let regionKeyPath = "CognitoUserPool.Default.Region"
105105
guard case .string(let regionString) = authConfiguration.value(at: regionKeyPath) else {
106106
Amplify.Logging.warn("Could not read Cognito user pool information from the configuration.")
107107
return nil
108108
}
109109
let region = (regionString as NSString).aws_regionTypeValue()
110-
return AmplifyAWSServiceConfiguration(region: region)
110+
111+
if let endpoint = try resolveCognitoOverrideEndpoint(using: authConfiguration, region: region) {
112+
return AmplifyAWSServiceConfiguration(region: region, endpoint: endpoint)
113+
} else {
114+
return AmplifyAWSServiceConfiguration(region: region)
115+
}
111116
}
112117

118+
func resolveCognitoOverrideEndpoint(
119+
using authConfiguration: JSONValue,
120+
region: AWSRegionType) throws -> AWSEndpoint? {
121+
122+
let endpointKeyPath = "CognitoUserPool.Default.Endpoint"
123+
guard case .string(let endpointString) = authConfiguration.value(at: endpointKeyPath) else {
124+
return nil
125+
}
126+
127+
let amplifyError = AuthError.configuration(
128+
"Error configuring \(String(describing: self))",
129+
"""
130+
Invalid Endpoint value \(endpointString). Expected a fully-qualified hostname.
131+
""")
132+
133+
guard (URLComponents(string: endpointString)?.scheme ?? "").isEmpty else {
134+
throw amplifyError
135+
}
136+
137+
let endpointStringWithScheme = "https://" + endpointString
138+
guard
139+
let components = URLComponents(string: endpointStringWithScheme),
140+
components.path == "",
141+
let url = components.url
142+
else {
143+
throw amplifyError
144+
}
145+
146+
return AWSEndpoint(region: region, service: .CognitoIdentityProvider, url: url)
147+
}
148+
113149
// MARK: Internal
114150

115151
/// Internal configure method to set the properties of the plugin

AmplifyPlugins/Auth/AWSCognitoAuthPluginTests/ConfigurationTests/AWSCognitoAuthPluginConfigTests.swift

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ class AWSCognitoAuthPluginConfigTests: XCTestCase {
6262
"PoolId": "xx",
6363
"Region": "us-east-1",
6464
"AppClientId": "xx",
65-
"AppClientSecret": "xx"]]
65+
"AppClientSecret": "xx",
66+
"Endpoint": "example.org"]]
6667
]
6768
])
6869
let amplifyConfig = AmplifyConfiguration(auth: categoryConfig)
@@ -119,7 +120,8 @@ class AWSCognitoAuthPluginConfigTests: XCTestCase {
119120
"PoolId": "xx",
120121
"Region": "us-east-1",
121122
"AppClientId": "xx",
122-
"AppClientSecret": "xx"]]
123+
"AppClientSecret": "xx",
124+
"Endpoint": "example.org"]]
123125
]
124126
])
125127
let amplifyConfig = AmplifyConfiguration(auth: categoryConfig)
@@ -166,6 +168,76 @@ class AWSCognitoAuthPluginConfigTests: XCTestCase {
166168
}
167169
}
168170

171+
/// Test Auth configuration with endpoint url containing scheme for user pool
172+
///
173+
/// - Given: Given invalid config with endpoint url containing scheme for user pool
174+
/// - When:
175+
/// - I configure auth with the given configuration
176+
/// - Then:
177+
/// - I should get an exception.
178+
///
179+
func testConfigWithInvalidUserPoolEndpointWithScheme() throws {
180+
let plugin = AWSCognitoAuthPlugin()
181+
try Amplify.add(plugin: plugin)
182+
183+
let categoryConfig = AuthCategoryConfiguration(plugins: [
184+
"awsCognitoAuthPlugin": [
185+
"CognitoUserPool": ["Default": [
186+
"PoolId": "xx",
187+
"Region": "us-east-1",
188+
"AppClientId": "xx",
189+
"AppClientSecret": "xx",
190+
"Endpoint": "https://example.org"]]
191+
]
192+
])
193+
194+
let amplifyConfig = AmplifyConfiguration(auth: categoryConfig)
195+
do {
196+
try Amplify.configure(amplifyConfig)
197+
XCTFail("Should have thrown a AuthError.configuration error for invalid endpoint url")
198+
} catch {
199+
guard case AuthError.configuration = error else {
200+
XCTFail("Should have thrown a AuthError.configuration error for invalid endpoint url")
201+
return
202+
}
203+
}
204+
}
205+
206+
/// Test Auth configuration with endpoint url containing path for user pool
207+
///
208+
/// - Given: Given invalid config with endpoint url containing path for user pool
209+
/// - When:
210+
/// - I configure auth with the given configuration
211+
/// - Then:
212+
/// - I should get an exception.
213+
///
214+
func testConfigWithInvalidUserPoolEndpointWithPath() throws {
215+
let plugin = AWSCognitoAuthPlugin()
216+
try Amplify.add(plugin: plugin)
217+
218+
let categoryConfig = AuthCategoryConfiguration(plugins: [
219+
"awsCognitoAuthPlugin": [
220+
"CognitoUserPool": ["Default": [
221+
"PoolId": "xx",
222+
"Region": "us-east-1",
223+
"AppClientId": "xx",
224+
"AppClientSecret": "xx",
225+
"Endpoint": "example.org/path"]]
226+
]
227+
])
228+
229+
let amplifyConfig = AmplifyConfiguration(auth: categoryConfig)
230+
do {
231+
try Amplify.configure(amplifyConfig)
232+
XCTFail("Should have thrown a AuthError.configuration error for invalid endpoint url")
233+
} catch {
234+
guard case AuthError.configuration = error else {
235+
XCTFail("Should have thrown a AuthError.configuration error for invalid endpoint url")
236+
return
237+
}
238+
}
239+
}
240+
169241
/// Test Auth configuration with nil value
170242
///
171243
/// - Given: Given a nil config for user pool and identity pool
@@ -210,7 +282,8 @@ class AWSCognitoAuthPluginConfigTests: XCTestCase {
210282
"PoolId": "xx",
211283
"Region": "us-east-1",
212284
"AppClientId": "xx",
213-
"AppClientSecret": "xx"]]
285+
"AppClientSecret": "xx",
286+
"Endpoint": "example.org"]]
214287
]
215288
])
216289
let amplifyConfig = AmplifyConfiguration(auth: categoryConfig)

AmplifyPlugins/Core/AWSPluginsCore/ServiceConfiguration/AmplifyAWSServiceConfiguration.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public class AmplifyAWSServiceConfiguration: AWSServiceConfiguration {
4040
super.init(region: regionType, credentialsProvider: nil)
4141
}
4242

43+
public init(region regionType: AWSRegionType, endpoint: AWSEndpoint) {
44+
super.init(region: regionType, endpoint: endpoint, credentialsProvider: nil)
45+
}
46+
4347
override public init(region regionType: AWSRegionType,
4448
endpoint: AWSEndpoint,
4549
credentialsProvider: AWSCredentialsProvider,

0 commit comments

Comments
 (0)