diff --git a/Riot/Modules/Reauthentication/ReauthenticationCoordinator.swift b/Riot/Modules/Reauthentication/ReauthenticationCoordinator.swift index 8a66c66ce..ba805e3de 100644 --- a/Riot/Modules/Reauthentication/ReauthenticationCoordinator.swift +++ b/Riot/Modules/Reauthentication/ReauthenticationCoordinator.swift @@ -63,8 +63,17 @@ final class ReauthenticationCoordinator: ReauthenticationCoordinatorType { // MARK: - Public methods func start() { - - self.userInteractiveAuthenticationService.authenticatedEndpointStatus(for: self.parameters.authenticatedEndpointRequest) { (result) in + if let authenticatedEndpointRequest = self.parameters.authenticatedEndpointRequest { + self.start(with: authenticatedEndpointRequest) + } else if let authenticationSession = self.parameters.authenticationSession { + self.start(with: authenticationSession) + } else { + fatalError("[ReauthenticationCoordinator] Should not happen. Missing authentication parameters") + } + } + + private func start(with authenticatedEndpointRequest: AuthenticatedEndpointRequest) { + self.userInteractiveAuthenticationService.authenticatedEndpointStatus(for: authenticatedEndpointRequest) { (result) in switch result { case .success(let authenticatedEnpointStatus): @@ -74,14 +83,7 @@ final class ReauthenticationCoordinator: ReauthenticationCoordinatorType { NSLog("[ReauthenticationCoordinator] No need to login again") self.delegate?.reauthenticationCoordinatorDidComplete(self, withAuthenticationParameters: nil) case .authenticationNeeded(let authenticationSession): - if self.userInteractiveAuthenticationService.hasPasswordFlow(inFlows: authenticationSession.flows) { - self.showPasswordAuthentication(with: authenticationSession) - } else if let authenticationFallbackURL = self.userInteractiveAuthenticationService.firstUncompletedStageAuthenticationFallbackURL(for: authenticationSession) { - - self.showFallbackAuthentication(with: authenticationFallbackURL, authenticationSession: authenticationSession) - } else { - self.delegate?.reauthenticationCoordinator(self, didFailWithError: UserInteractiveAuthenticationServiceError.flowNotSupported) - } + self.start(with: authenticationSession) } case .failure(let error): self.delegate?.reauthenticationCoordinator(self, didFailWithError: error) @@ -89,6 +91,17 @@ final class ReauthenticationCoordinator: ReauthenticationCoordinatorType { } } + private func start(with authenticationSession: MXAuthenticationSession) { + if self.userInteractiveAuthenticationService.hasPasswordFlow(inFlows: authenticationSession.flows) { + self.showPasswordAuthentication(with: authenticationSession) + } else if let authenticationFallbackURL = self.userInteractiveAuthenticationService.firstUncompletedStageAuthenticationFallbackURL(for: authenticationSession) { + + self.showFallbackAuthentication(with: authenticationFallbackURL, authenticationSession: authenticationSession) + } else { + self.delegate?.reauthenticationCoordinator(self, didFailWithError: UserInteractiveAuthenticationServiceError.flowNotSupported) + } + } + func toPresentable() -> UIViewController { return self.parameters.presenter.toPresentable() } diff --git a/Riot/Modules/Reauthentication/ReauthenticationCoordinatorParameters.swift b/Riot/Modules/Reauthentication/ReauthenticationCoordinatorParameters.swift index 8852481b2..1527d4e79 100644 --- a/Riot/Modules/Reauthentication/ReauthenticationCoordinatorParameters.swift +++ b/Riot/Modules/Reauthentication/ReauthenticationCoordinatorParameters.swift @@ -20,6 +20,8 @@ import Foundation @objcMembers class ReauthenticationCoordinatorParameters: NSObject { + // MARK: - Properties + /// The Matrix session let session: MXSession @@ -33,18 +35,47 @@ class ReauthenticationCoordinatorParameters: NSObject { /// The message to use in the authentication screen if present. let message: String? - /// The authenticated API endpoint request - let authenticatedEndpointRequest: AuthenticatedEndpointRequest + /// The authenticated API endpoint request. + let authenticatedEndpointRequest: AuthenticatedEndpointRequest? - init(session: MXSession, + /// The MXAuthentication session retrieved from a request error. + /// Note: If the property is not nil `authenticatedEndpointRequest` will not be taken into account. + let authenticationSession: MXAuthenticationSession? + + // MARK: - Setup + + convenience init(session: MXSession, presenter: UIViewController, title: String?, message: String?, authenticatedEndpointRequest: AuthenticatedEndpointRequest) { + self.init(session: session, + presenter: presenter, + title: title, + message: message, + authenticatedEndpointRequest: authenticatedEndpointRequest, + authenticationSession: nil) + } + + convenience init(session: MXSession, + presenter: UIViewController, + title: String?, + message: String?, + authenticationSession: MXAuthenticationSession) { + self.init(session: session, presenter: presenter, title: title, message: message, authenticatedEndpointRequest: nil, authenticationSession: authenticationSession) + } + + private init(session: MXSession, + presenter: UIViewController, + title: String?, + message: String?, + authenticatedEndpointRequest: AuthenticatedEndpointRequest?, + authenticationSession: MXAuthenticationSession?) { self.session = session self.presenter = presenter self.title = title self.message = message self.authenticatedEndpointRequest = authenticatedEndpointRequest + self.authenticationSession = authenticationSession } }