Skip to content

Commit 8bbf5c7

Browse files
authored
fix(auth): hostedui extract error_description query pararm (#3183)
1 parent 04051b4 commit 8bbf5c7

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/HostedUI/HostedUIASWebAuthenticationSession.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ class HostedUIASWebAuthenticationSession: NSObject, HostedUISessionBehavior {
3131
let queryItems = urlComponents?.queryItems ?? []
3232

3333
if let error = queryItems.first(where: { $0.name == "error" })?.value {
34-
callback(.failure(.serviceMessage(error)))
34+
let errorDescription = queryItems.first(
35+
where: { $0.name == "error_description" }
36+
)?.value?.trim() ?? ""
37+
let message = "\(error) \(errorDescription)"
38+
callback(.failure(.serviceMessage(message)))
3539
return
3640
}
3741
callback(.success(queryItems))

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,22 @@ class AWSAuthHostedUISignInTests: XCTestCase {
258258
}
259259

260260
@MainActor
261-
func testTokenErrorResponse() async {
261+
/// Given: A HostedUI response with `error` and `error_description` query parameters.
262+
/// When: Invoking `signInWithWebUI`
263+
/// Then: The caller should receive an `AuthError.service` where the `errorDescription`
264+
/// is `"\(error) \(error_description)"`
265+
func testTokenErrorResponse() async throws {
262266
mockHostedUIResult = .success([
263267
.init(name: "state", value: mockState),
264268
.init(name: "code", value: mockProof)
265269
])
270+
271+
let (errorMessage, errorDescription) = ("invalid_grant", "Some error")
266272
mockTokenResult = [
267-
"error": "invalid_grant",
268-
"error_description": "Some error"] as [String: Any]
269-
mockJson = try! JSONSerialization.data(withJSONObject: mockTokenResult)
273+
"error": errorMessage,
274+
"error_description": errorDescription
275+
]
276+
mockJson = try JSONSerialization.data(withJSONObject: mockTokenResult)
270277
MockURLProtocol.requestHandler = { _ in
271278
return (HTTPURLResponse(), self.mockJson)
272279
}
@@ -276,13 +283,15 @@ class AWSAuthHostedUISignInTests: XCTestCase {
276283
_ = try await plugin.signInWithWebUI(presentationAnchor: ASPresentationAnchor(), options: nil)
277284
XCTFail("Should not succeed")
278285
} catch {
279-
guard case AuthError.service = error else {
286+
guard case AuthError.service(let message, _, _) = error else {
280287
XCTFail("Should not fail with error = \(error)")
281288
return
282289
}
290+
let expectedErrorDescription = "\(errorMessage) \(errorDescription)"
291+
XCTAssertEqual(expectedErrorDescription, message)
283292
expectation.fulfill()
284293
}
285-
wait(for: [expectation], timeout: networkTimeout)
294+
await fulfillment(of: [expectation], timeout: networkTimeout)
286295
}
287296

288297

0 commit comments

Comments
 (0)