From 8bbf5c724600569ba135b74c2314152ec87d38af Mon Sep 17 00:00:00 2001 From: Ian Saultz <52051793+atierian@users.noreply.github.com> Date: Wed, 30 Aug 2023 12:19:46 -0400 Subject: [PATCH] fix(auth): hostedui extract error_description query pararm (#3183) --- .../HostedUIASWebAuthenticationSession.swift | 6 +++++- .../AWSAuthHostedUISignInTests.swift | 21 +++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/HostedUI/HostedUIASWebAuthenticationSession.swift b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/HostedUI/HostedUIASWebAuthenticationSession.swift index e60fe6e7dd..cd9760637a 100644 --- a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/HostedUI/HostedUIASWebAuthenticationSession.swift +++ b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/HostedUI/HostedUIASWebAuthenticationSession.swift @@ -31,7 +31,11 @@ class HostedUIASWebAuthenticationSession: NSObject, HostedUISessionBehavior { let queryItems = urlComponents?.queryItems ?? [] if let error = queryItems.first(where: { $0.name == "error" })?.value { - callback(.failure(.serviceMessage(error))) + let errorDescription = queryItems.first( + where: { $0.name == "error_description" } + )?.value?.trim() ?? "" + let message = "\(error) \(errorDescription)" + callback(.failure(.serviceMessage(message))) return } callback(.success(queryItems)) diff --git a/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/HostedUITests/AWSAuthHostedUISignInTests.swift b/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/HostedUITests/AWSAuthHostedUISignInTests.swift index 55200d42d4..7ec961d8dc 100644 --- a/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/HostedUITests/AWSAuthHostedUISignInTests.swift +++ b/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/HostedUITests/AWSAuthHostedUISignInTests.swift @@ -258,15 +258,22 @@ class AWSAuthHostedUISignInTests: XCTestCase { } @MainActor - func testTokenErrorResponse() async { + /// Given: A HostedUI response with `error` and `error_description` query parameters. + /// When: Invoking `signInWithWebUI` + /// Then: The caller should receive an `AuthError.service` where the `errorDescription` + /// is `"\(error) \(error_description)"` + func testTokenErrorResponse() async throws { mockHostedUIResult = .success([ .init(name: "state", value: mockState), .init(name: "code", value: mockProof) ]) + + let (errorMessage, errorDescription) = ("invalid_grant", "Some error") mockTokenResult = [ - "error": "invalid_grant", - "error_description": "Some error"] as [String: Any] - mockJson = try! JSONSerialization.data(withJSONObject: mockTokenResult) + "error": errorMessage, + "error_description": errorDescription + ] + mockJson = try JSONSerialization.data(withJSONObject: mockTokenResult) MockURLProtocol.requestHandler = { _ in return (HTTPURLResponse(), self.mockJson) } @@ -276,13 +283,15 @@ class AWSAuthHostedUISignInTests: XCTestCase { _ = try await plugin.signInWithWebUI(presentationAnchor: ASPresentationAnchor(), options: nil) XCTFail("Should not succeed") } catch { - guard case AuthError.service = error else { + guard case AuthError.service(let message, _, _) = error else { XCTFail("Should not fail with error = \(error)") return } + let expectedErrorDescription = "\(errorMessage) \(errorDescription)" + XCTAssertEqual(expectedErrorDescription, message) expectation.fulfill() } - wait(for: [expectation], timeout: networkTimeout) + await fulfillment(of: [expectation], timeout: networkTimeout) }