Skip to content

Commit

Permalink
fix(auth): hostedui extract error_description query pararm (#3183)
Browse files Browse the repository at this point in the history
  • Loading branch information
atierian authored Aug 30, 2023
1 parent 04051b4 commit 8bbf5c7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}


Expand Down

0 comments on commit 8bbf5c7

Please sign in to comment.