Skip to content

Commit 619867b

Browse files
authored
fix(auth): return configuration error upon invalid redirect URI in hosted ui (#3889)
1 parent 95d8f16 commit 619867b

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Actions/SignIn/HostedUI/ShowHostedUISignIn.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class ShowHostedUISignIn: NSObject, Action {
3636

3737
guard let callbackURL = URL(string: hostedUIConfig.oauth.signInRedirectURI),
3838
let callbackURLScheme = callbackURL.scheme else {
39-
let event = SignInEvent(eventType: .throwAuthError(.hostedUI(.signInURI)))
39+
let event = HostedUIEvent(eventType: .throwError(.hostedUI(.signInURI)))
4040
logVerbose("\(#fileID) Sending event \(event)", environment: environment)
4141
await dispatcher.send(event)
4242
return

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/StateMachine/Resolvers/SignIn/HostedUISignInState+Resolver.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ extension HostedUISignInState {
3030

3131
case .showingUI:
3232
if case .throwError(let error) = event.isHostedUIEvent {
33-
// Remove this?
3433
let action = CancelSignIn()
3534
return .init(newState: .error(error), actions: [action])
3635
}

AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/HostedUITests/AWSAuthHostedUISignInTests.swift

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,43 @@ class AWSAuthHostedUISignInTests: XCTestCase {
4242
return URLSession(configuration: configuration)
4343
}
4444

45+
private func customPlugin(with cusotmConfiguration: HostedUIConfigurationData?) -> AWSCognitoAuthPlugin {
46+
let plugin = AWSCognitoAuthPlugin()
47+
mockJson = try! JSONSerialization.data(withJSONObject: mockTokenResult)
48+
MockURLProtocol.requestHandler = { _ in
49+
return (HTTPURLResponse(), self.mockJson)
50+
}
51+
52+
func sessionFactory() -> HostedUISessionBehavior {
53+
MockHostedUISession(result: mockHostedUIResult)
54+
}
55+
56+
func mockRandomString() -> RandomStringBehavior {
57+
return MockRandomStringGenerator(mockString: mockState, mockUUID: mockState)
58+
}
59+
60+
let environment = BasicHostedUIEnvironment(configuration: cusotmConfiguration ?? configuration,
61+
hostedUISessionFactory: sessionFactory,
62+
urlSessionFactory: urlSessionMock,
63+
randomStringFactory: mockRandomString)
64+
let authEnvironment = Defaults.makeDefaultAuthEnvironment(
65+
userPoolFactory: { self.mockIdentityProvider },
66+
hostedUIEnvironment: environment)
67+
let stateMachine = Defaults.authStateMachineWith(
68+
environment: authEnvironment,
69+
initialState: initialState
70+
)
71+
72+
plugin.configure(
73+
authConfiguration: Defaults.makeDefaultAuthConfigData(withHostedUI: configuration),
74+
authEnvironment: authEnvironment,
75+
authStateMachine: stateMachine,
76+
credentialStoreStateMachine: Defaults.makeDefaultCredentialStateMachine(),
77+
hubEventHandler: MockAuthHubEventBehavior(),
78+
analyticsHandler: MockAnalyticsHandler())
79+
return plugin
80+
}
81+
4582
override func setUp() {
4683
plugin = AWSCognitoAuthPlugin()
4784
mockJson = try! JSONSerialization.data(withJSONObject: mockTokenResult)
@@ -310,6 +347,41 @@ class AWSAuthHostedUISignInTests: XCTestCase {
310347
await fulfillment(of: [expectation], timeout: networkTimeout)
311348
}
312349

350+
@MainActor
351+
func testInvalidRedirectConfigurationFailure() async {
352+
let invalidRedirectConfig = HostedUIConfigurationData(clientId: "clientId", oauth: .init(
353+
domain: "cognitodomain",
354+
scopes: ["name"],
355+
signInRedirectURI: "@#$%junk1343",
356+
signOutRedirectURI: "@3451://"))
357+
let testPlugin = customPlugin(with: invalidRedirectConfig)
358+
359+
mockHostedUIResult = .success([
360+
.init(name: "state", value: mockState),
361+
.init(name: "code", value: mockProof)
362+
])
363+
mockTokenResult = [
364+
"refresh_token": AWSCognitoUserPoolTokens.testData.refreshToken,
365+
"expires_in": 10] as [String: Any]
366+
mockJson = try! JSONSerialization.data(withJSONObject: mockTokenResult)
367+
MockURLProtocol.requestHandler = { _ in
368+
return (HTTPURLResponse(), self.mockJson)
369+
}
370+
371+
let expectation = expectation(description: "SignIn operation should complete")
372+
do {
373+
_ = try await testPlugin.signInWithWebUI(presentationAnchor: ASPresentationAnchor(), options: nil)
374+
XCTFail("Should not succeed")
375+
} catch {
376+
guard case AuthError.configuration = error else {
377+
XCTFail("Should not fail with error = \(error)")
378+
return
379+
}
380+
expectation.fulfill()
381+
}
382+
await fulfillment(of: [expectation], timeout: networkTimeout)
383+
}
384+
313385

314386

315387
/// Test a signIn restart while another sign in is in progress

0 commit comments

Comments
 (0)