Skip to content

Commit

Permalink
fix(Auth): Throw error if hosted UI is not presented during sign out
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh62 committed Jul 3, 2024
1 parent dbc4a04 commit 931fa6a
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ class ShowHostedUISignOut: NSObject, Action {
environment: Environment) async {

var hostedUIError: AWSCognitoHostedUIError?
if let hostedUIInternalError = error as? HostedUIError,
case .cancelled = hostedUIInternalError {
let event = SignOutEvent(eventType: .userCancelled)
if let hostedUIInternalError = error as? HostedUIError {
let event = SignOutEvent(eventType: .hostedUISignOutError(hostedUIInternalError))
self.logVerbose("\(#fileID) Sending event \(event.type)", environment: environment)
await dispatcher.send(event)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,30 @@ import Amplify

enum SignOutError: Error {

case userCancelled
case hostedUI(HostedUIError)

case localSignOut
}

extension SignOutError: AuthErrorConvertible {
var authError: AuthError {
switch self {
case .userCancelled:
return AuthError.service("", "", AWSCognitoAuthError.userCancelled)
case .hostedUI(let error):
return error.authError
case .localSignOut:
return AuthError.unknown("", nil)
}
}
}

extension SignOutError: Equatable {
static func == (lhs: SignOutError, rhs: SignOutError) -> Bool {
switch (lhs, rhs) {
case (.hostedUI, .hostedUI),
(.localSignOut, .localSignOut):
return true
default:
return false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct SignOutEvent: StateMachineEvent {

case signedOutFailure(AuthenticationError)

case userCancelled
case hostedUISignOutError(HostedUIError)
}

let id: String
Expand All @@ -66,8 +66,8 @@ struct SignOutEvent: StateMachineEvent {
return "SignOutEvent.globalSignOutError"
case .signOutGuest:
return "SignOutEvent.signOutGuest"
case .userCancelled:
return "SignOutEvent.userCancelled"
case .hostedUISignOutError:
return "SignOutEvent.hostedUISignOutError"
}
}

Expand All @@ -94,7 +94,7 @@ extension SignOutEvent.EventType: Equatable {
(.signedOutFailure, .signedOutFailure),
(.globalSignOutError, .globalSignOutError),
(.signOutGuest, .signOutGuest),
(.userCancelled, .userCancelled):
(.hostedUISignOutError, .hostedUISignOutError):
return true
default:
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ extension HostedUIError: AuthErrorConvertible {
case .unableToStartASWebAuthenticationSession:
return .service(
AuthPluginErrorConstants.hostedUIUnableToStartASWebAuthenticationSession.errorDescription,
AuthPluginErrorConstants.hostedUIUnableToStartASWebAuthenticationSession.recoverySuggestion)
AuthPluginErrorConstants.hostedUIUnableToStartASWebAuthenticationSession.recoverySuggestion,
AWSCognitoAuthError.errorLoadingUI)

case .serviceMessage(let message):
return .service(message, AuthPluginErrorConstants.serviceError)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ extension SignOutState {
newState: SignOutState.revokingToken,
actions: [action]
)
case .userCancelled:
case .hostedUISignOutError(let error):
let action = CancelSignOut(signedInData: signedInData)
return .init(newState: .error(.userCancelled), actions: [action])
return .init(newState: .error(.hostedUI(error)), actions: [action])
default:
return .from(oldState)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class ShowHostedUISignOutTests: XCTestCase {
return
}

XCTAssertEqual(event.eventType, .userCancelled)
XCTAssertEqual(event.eventType, .hostedUISignOutError(.cancelled))
expectation.fulfill()
},
environment: Defaults.makeDefaultAuthEnvironment(
Expand Down Expand Up @@ -185,22 +185,20 @@ class ShowHostedUISignOutTests: XCTestCase {
await action.execute(
withDispatcher: MockDispatcher { event in
guard let event = event as? SignOutEvent,
case .signOutGlobally(let data, let hostedUIError) = event.eventType else {
XCTFail("Expected SignOutEvent.signOutGlobally, got \(event)")
case .hostedUISignOutError(let hostedUIError) = event.eventType else {
XCTFail("Expected SignOutEvent.hostedUISignOutError, got \(event)")
expectation.fulfill()
return
}

guard let hostedUIError = hostedUIError,
case .invalidState(let errorDescription, let recoverySuggestion, let serviceError) = hostedUIError.error else {
guard case .invalidState(let errorDescription, let recoverySuggestion, let serviceError) = hostedUIError.authError else {
XCTFail("Expected AuthError.invalidState")
expectation.fulfill()
return
}

XCTAssertEqual(errorDescription, AuthPluginErrorConstants.hostedUIInvalidPresentation.errorDescription)
XCTAssertEqual(recoverySuggestion, AuthPluginErrorConstants.hostedUIInvalidPresentation.recoverySuggestion)
XCTAssertEqual(data, signInData)
XCTAssertNil(serviceError)
expectation.fulfill()
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SignOutStateNotStartedTests: XCTestCase {
case .signOutLocally,
.signedOutSuccess,
.signedOutFailure,
.userCancelled,
.hostedUISignOutError,
.globalSignOutError:
XCTAssertEqual(
resolver.resolve(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SignOutStateRevokingTokenTests: XCTestCase {
.signedOutSuccess,
.invokeHostedUISignOut,
.signOutGuest,
.userCancelled,
.hostedUISignOutError,
.globalSignOutError:
XCTAssertEqual(
resolver.resolve(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SignOutStateSigningOutGloballyTests: XCTestCase {
.signedOutSuccess,
.invokeHostedUISignOut,
.signOutGuest,
.userCancelled,
.hostedUISignOutError,
.signedOutFailure:
XCTAssertEqual(
resolver.resolve(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SignOutStateSigningOutLocallyTests: XCTestCase {
.signOutLocally,
.invokeHostedUISignOut,
.signOutGuest,
.userCancelled,
.hostedUISignOutError,
.globalSignOutError:
XCTAssertEqual(
resolver.resolve(
Expand Down

0 comments on commit 931fa6a

Please sign in to comment.