2022-01-12 17:11:29 +00:00
|
|
|
// File created from ScreenTemplate
|
|
|
|
// $ createScreen.sh Onboarding Authentication
|
|
|
|
/*
|
|
|
|
Copyright 2021 New Vector Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import UIKit
|
2022-04-14 10:06:12 +00:00
|
|
|
|
|
|
|
@available(iOS 14.0, *)
|
|
|
|
struct AuthenticationCoordinatorParameters {
|
|
|
|
let navigationRouter: NavigationRouterType
|
|
|
|
/// The screen that should be shown when starting the flow.
|
|
|
|
let initialScreen: AuthenticationCoordinator.EntryPoint
|
|
|
|
/// Whether or not the coordinator should show the loading spinner, key verification etc.
|
|
|
|
let canPresentAdditionalScreens: Bool
|
|
|
|
}
|
2022-01-12 17:11:29 +00:00
|
|
|
|
2022-01-18 16:48:52 +00:00
|
|
|
/// A coordinator that handles authentication, verification and setting a PIN.
|
2022-04-14 10:06:12 +00:00
|
|
|
@available(iOS 14.0, *)
|
2022-01-12 17:11:29 +00:00
|
|
|
final class AuthenticationCoordinator: NSObject, AuthenticationCoordinatorProtocol {
|
|
|
|
|
2022-04-14 10:06:12 +00:00
|
|
|
enum EntryPoint {
|
|
|
|
case registration
|
|
|
|
case selectServerForRegistration
|
|
|
|
case login
|
|
|
|
}
|
|
|
|
|
2022-01-12 17:11:29 +00:00
|
|
|
// MARK: - Properties
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
2022-02-17 16:50:02 +00:00
|
|
|
private let navigationRouter: NavigationRouterType
|
2022-04-14 10:06:12 +00:00
|
|
|
private let authenticationService = AuthenticationService.shared
|
2022-02-17 16:50:02 +00:00
|
|
|
|
2022-04-14 10:06:12 +00:00
|
|
|
private let initialScreen: EntryPoint
|
2022-02-24 17:24:04 +00:00
|
|
|
private var canPresentAdditionalScreens: Bool
|
|
|
|
private var isWaitingToPresentCompleteSecurity = false
|
2022-04-14 10:06:12 +00:00
|
|
|
|
2022-04-29 18:15:18 +00:00
|
|
|
private var verificationListener: SessionVerificationListener?
|
2022-02-17 16:50:02 +00:00
|
|
|
|
|
|
|
/// The password entered, for use when setting up cross-signing.
|
|
|
|
private var password: String?
|
|
|
|
/// The session created when successfully authenticated.
|
|
|
|
private var session: MXSession?
|
2022-01-12 17:11:29 +00:00
|
|
|
|
|
|
|
// MARK: Public
|
|
|
|
|
|
|
|
// Must be used only internally
|
|
|
|
var childCoordinators: [Coordinator] = []
|
2022-02-17 16:50:02 +00:00
|
|
|
var completion: ((AuthenticationCoordinatorResult) -> Void)?
|
2022-01-12 17:11:29 +00:00
|
|
|
|
|
|
|
// MARK: - Setup
|
|
|
|
|
2022-02-17 16:50:02 +00:00
|
|
|
init(parameters: AuthenticationCoordinatorParameters) {
|
|
|
|
self.navigationRouter = parameters.navigationRouter
|
2022-04-14 10:06:12 +00:00
|
|
|
self.initialScreen = parameters.initialScreen
|
2022-02-24 17:24:04 +00:00
|
|
|
self.canPresentAdditionalScreens = parameters.canPresentAdditionalScreens
|
2022-02-17 16:50:02 +00:00
|
|
|
|
2022-01-12 17:11:29 +00:00
|
|
|
super.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Public
|
|
|
|
|
|
|
|
func start() {
|
2022-04-14 10:06:12 +00:00
|
|
|
Task {
|
2022-04-28 12:06:36 +00:00
|
|
|
do {
|
|
|
|
let flow: AuthenticationFlow = initialScreen == .login ? .login : .registration
|
|
|
|
try await authenticationService.startFlow(flow, for: authenticationService.state.homeserver.address)
|
|
|
|
} catch {
|
2022-04-29 18:15:18 +00:00
|
|
|
MXLog.error("[AuthenticationCoordinator] start: Failed to start")
|
2022-04-28 12:06:36 +00:00
|
|
|
await MainActor.run { displayError(error) }
|
|
|
|
return
|
|
|
|
}
|
2022-04-14 10:06:12 +00:00
|
|
|
|
|
|
|
await MainActor.run {
|
|
|
|
switch initialScreen {
|
|
|
|
case .registration:
|
2022-04-27 16:35:28 +00:00
|
|
|
showRegistrationScreen()
|
2022-04-14 10:06:12 +00:00
|
|
|
case .selectServerForRegistration:
|
|
|
|
showServerSelectionScreen()
|
|
|
|
case .login:
|
|
|
|
showLoginScreen()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-12 17:11:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func toPresentable() -> UIViewController {
|
2022-04-14 10:06:12 +00:00
|
|
|
navigationRouter.toPresentable()
|
2022-01-12 17:11:29 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 10:06:12 +00:00
|
|
|
func presentPendingScreensIfNecessary() {
|
|
|
|
canPresentAdditionalScreens = true
|
|
|
|
|
|
|
|
showLoadingAnimation()
|
|
|
|
|
|
|
|
if isWaitingToPresentCompleteSecurity {
|
|
|
|
isWaitingToPresentCompleteSecurity = false
|
|
|
|
presentCompleteSecurity()
|
|
|
|
}
|
2022-01-28 11:53:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 12:06:36 +00:00
|
|
|
// MARK: - Private
|
|
|
|
|
2022-05-03 09:55:43 +00:00
|
|
|
/// Presents an alert on top of the navigation router, using the supplied error's `localizedDescription`.
|
2022-04-28 12:06:36 +00:00
|
|
|
@MainActor func displayError(_ error: Error) {
|
|
|
|
let alert = UIAlertController(title: VectorL10n.error,
|
|
|
|
message: error.localizedDescription,
|
|
|
|
preferredStyle: .alert)
|
|
|
|
|
|
|
|
alert.addAction(UIAlertAction(title: VectorL10n.ok, style: .default))
|
|
|
|
|
|
|
|
toPresentable().present(alert, animated: true)
|
|
|
|
}
|
|
|
|
|
2022-04-14 10:06:12 +00:00
|
|
|
// MARK: - Registration
|
|
|
|
|
|
|
|
/// Pushes the server selection screen into the flow (other screens may also present it modally later).
|
|
|
|
@MainActor private func showServerSelectionScreen() {
|
|
|
|
MXLog.debug("[AuthenticationCoordinator] showServerSelectionScreen")
|
|
|
|
let parameters = AuthenticationServerSelectionCoordinatorParameters(authenticationService: authenticationService,
|
|
|
|
hasModalPresentation: false)
|
|
|
|
let coordinator = AuthenticationServerSelectionCoordinator(parameters: parameters)
|
|
|
|
coordinator.completion = { [weak self, weak coordinator] result in
|
|
|
|
guard let self = self, let coordinator = coordinator else { return }
|
|
|
|
self.serverSelectionCoordinator(coordinator, didCompleteWith: result)
|
|
|
|
}
|
|
|
|
|
|
|
|
coordinator.start()
|
|
|
|
add(childCoordinator: coordinator)
|
|
|
|
|
|
|
|
if navigationRouter.modules.isEmpty {
|
|
|
|
navigationRouter.setRootModule(coordinator, popCompletion: nil)
|
|
|
|
} else {
|
|
|
|
navigationRouter.push(coordinator, animated: true) { [weak self] in
|
|
|
|
self?.remove(childCoordinator: coordinator)
|
|
|
|
}
|
|
|
|
}
|
2022-01-12 17:11:29 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 10:06:12 +00:00
|
|
|
@available(iOS 14.0, *)
|
|
|
|
/// Shows the next screen in the flow after the server selection screen.
|
|
|
|
@MainActor private func serverSelectionCoordinator(_ coordinator: AuthenticationServerSelectionCoordinator,
|
|
|
|
didCompleteWith result: AuthenticationServerSelectionCoordinatorResult) {
|
|
|
|
switch result {
|
2022-04-27 16:35:28 +00:00
|
|
|
case .updated:
|
|
|
|
showRegistrationScreen()
|
2022-04-14 10:06:12 +00:00
|
|
|
case .dismiss:
|
|
|
|
MXLog.failure("[AuthenticationCoordinator] AuthenticationServerSelectionScreen is requesting dismiss when part of a stack.")
|
|
|
|
}
|
2022-01-28 11:53:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 10:06:12 +00:00
|
|
|
/// Shows the registration screen.
|
2022-04-27 16:35:28 +00:00
|
|
|
@MainActor private func showRegistrationScreen() {
|
2022-04-14 10:06:12 +00:00
|
|
|
MXLog.debug("[AuthenticationCoordinator] showRegistrationScreen")
|
2022-04-27 16:35:28 +00:00
|
|
|
let homeserver = authenticationService.state.homeserver
|
2022-04-14 10:06:12 +00:00
|
|
|
let parameters = AuthenticationRegistrationCoordinatorParameters(navigationRouter: navigationRouter,
|
|
|
|
authenticationService: authenticationService,
|
2022-04-27 16:35:28 +00:00
|
|
|
registrationFlow: homeserver.registrationFlow,
|
|
|
|
loginMode: homeserver.preferredLoginMode)
|
2022-04-14 10:06:12 +00:00
|
|
|
let coordinator = AuthenticationRegistrationCoordinator(parameters: parameters)
|
|
|
|
coordinator.completion = { [weak self, weak coordinator] result in
|
|
|
|
guard let self = self, let coordinator = coordinator else { return }
|
|
|
|
self.registrationCoordinator(coordinator, didCompleteWith: result)
|
|
|
|
}
|
|
|
|
|
|
|
|
coordinator.start()
|
|
|
|
add(childCoordinator: coordinator)
|
|
|
|
|
|
|
|
if navigationRouter.modules.isEmpty {
|
|
|
|
navigationRouter.setRootModule(coordinator, popCompletion: nil)
|
|
|
|
} else {
|
|
|
|
navigationRouter.push(coordinator, animated: true) { [weak self] in
|
|
|
|
self?.remove(childCoordinator: coordinator)
|
|
|
|
}
|
|
|
|
}
|
2022-01-12 17:11:29 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 10:06:12 +00:00
|
|
|
/// Displays the next view in the flow after the registration screen.
|
|
|
|
@available(iOS 14.0, *)
|
2022-05-03 09:16:27 +00:00
|
|
|
@MainActor private func registrationCoordinator(_ coordinator: AuthenticationRegistrationCoordinator,
|
|
|
|
didCompleteWith result: AuthenticationRegistrationCoordinatorResult) {
|
2022-04-14 10:06:12 +00:00
|
|
|
switch result {
|
|
|
|
case .selectServer:
|
|
|
|
showServerSelectionScreen()
|
2022-05-03 09:16:27 +00:00
|
|
|
case .completed(let result):
|
|
|
|
handleRegistrationResult(result)
|
2022-04-14 10:06:12 +00:00
|
|
|
}
|
2022-01-12 17:11:29 +00:00
|
|
|
}
|
2022-02-17 16:50:02 +00:00
|
|
|
|
2022-04-14 10:06:12 +00:00
|
|
|
/// Shows the login screen.
|
|
|
|
@MainActor private func showLoginScreen() {
|
|
|
|
MXLog.debug("[AuthenticationCoordinator] showLoginScreen")
|
2022-02-24 17:24:04 +00:00
|
|
|
|
2022-04-14 10:06:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Registration Handlers
|
|
|
|
/// Determines the next screen to show from the flow result and pushes it.
|
2022-05-03 09:16:27 +00:00
|
|
|
func handleRegistrationResult(_ result: RegistrationResult) {
|
|
|
|
switch result {
|
|
|
|
case .success(let mxSession):
|
|
|
|
onSessionCreated(session: mxSession, isAccountCreated: true)
|
|
|
|
case .flowResponse(let flowResult):
|
|
|
|
// TODO
|
|
|
|
break
|
|
|
|
}
|
2022-04-14 10:06:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Handles the creation of a new session following on from a successful authentication.
|
|
|
|
func onSessionCreated(session: MXSession, isAccountCreated: Bool) {
|
|
|
|
self.session = session
|
|
|
|
// self.password = password
|
|
|
|
|
|
|
|
if canPresentAdditionalScreens {
|
|
|
|
showLoadingAnimation()
|
2022-02-24 17:24:04 +00:00
|
|
|
}
|
2022-04-14 10:06:12 +00:00
|
|
|
|
2022-04-29 18:15:18 +00:00
|
|
|
let verificationListener = SessionVerificationListener(session: session, password: password)
|
|
|
|
|
|
|
|
verificationListener.completion = { [weak self] result in
|
|
|
|
guard let self = self else { return }
|
|
|
|
switch result {
|
|
|
|
case .needsVerification:
|
|
|
|
guard self.canPresentAdditionalScreens else {
|
|
|
|
MXLog.debug("[AuthenticationCoordinator] Delaying presentCompleteSecurity during onboarding.")
|
|
|
|
self.isWaitingToPresentCompleteSecurity = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
MXLog.debug("[AuthenticationCoordinator] Complete security")
|
|
|
|
self.presentCompleteSecurity()
|
|
|
|
case .authenticationIsComplete:
|
|
|
|
self.authenticationDidComplete()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
verificationListener.start()
|
|
|
|
self.verificationListener = verificationListener
|
|
|
|
|
2022-04-14 10:06:12 +00:00
|
|
|
completion?(.didLogin(session: session, authenticationType: isAccountCreated ? .register : .login))
|
2022-02-24 17:24:04 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 10:06:12 +00:00
|
|
|
// MARK: - Additional Screens
|
2022-02-17 16:50:02 +00:00
|
|
|
|
2022-04-14 10:06:12 +00:00
|
|
|
/// Replace the contents of the navigation router with a loading animation.
|
2022-02-17 16:50:02 +00:00
|
|
|
private func showLoadingAnimation() {
|
|
|
|
let loadingViewController = LaunchLoadingViewController()
|
|
|
|
loadingViewController.modalPresentationStyle = .fullScreen
|
|
|
|
|
|
|
|
// Replace the navigation stack with the loading animation
|
|
|
|
// as there is nothing to navigate back to.
|
|
|
|
navigationRouter.setRootModule(loadingViewController)
|
|
|
|
}
|
|
|
|
|
2022-04-14 10:06:12 +00:00
|
|
|
/// Present the key verification screen modally.
|
2022-02-24 17:24:04 +00:00
|
|
|
private func presentCompleteSecurity() {
|
|
|
|
guard let session = session else {
|
2022-04-14 10:06:12 +00:00
|
|
|
MXLog.error("[LegacyAuthenticationCoordinator] presentCompleteSecurity: Unable to present security due to missing session.")
|
2022-02-24 17:24:04 +00:00
|
|
|
authenticationDidComplete()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-17 16:50:02 +00:00
|
|
|
let isNewSignIn = true
|
2022-03-09 09:42:29 +00:00
|
|
|
let cancellable = !session.vc_homeserverConfiguration().encryption.isSecureBackupRequired
|
|
|
|
let keyVerificationCoordinator = KeyVerificationCoordinator(session: session, flow: .completeSecurity(isNewSignIn), cancellable: cancellable)
|
2022-02-17 16:50:02 +00:00
|
|
|
|
|
|
|
keyVerificationCoordinator.delegate = self
|
|
|
|
let presentable = keyVerificationCoordinator.toPresentable()
|
|
|
|
presentable.presentationController?.delegate = self
|
|
|
|
navigationRouter.present(presentable, animated: true)
|
|
|
|
keyVerificationCoordinator.start()
|
|
|
|
add(childCoordinator: keyVerificationCoordinator)
|
|
|
|
}
|
|
|
|
|
2022-04-14 10:06:12 +00:00
|
|
|
/// Complete the authentication flow.
|
2022-02-17 16:50:02 +00:00
|
|
|
private func authenticationDidComplete() {
|
2022-02-24 17:24:04 +00:00
|
|
|
completion?(.didComplete)
|
2022-02-17 16:50:02 +00:00
|
|
|
}
|
2022-01-12 17:11:29 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 16:50:02 +00:00
|
|
|
// MARK: - KeyVerificationCoordinatorDelegate
|
2022-04-14 10:06:12 +00:00
|
|
|
@available(iOS 14.0, *)
|
2022-02-17 16:50:02 +00:00
|
|
|
extension AuthenticationCoordinator: KeyVerificationCoordinatorDelegate {
|
|
|
|
func keyVerificationCoordinatorDidComplete(_ coordinator: KeyVerificationCoordinatorType, otherUserId: String, otherDeviceId: String) {
|
|
|
|
if let crypto = session?.crypto,
|
|
|
|
!crypto.backup.hasPrivateKeyInCryptoStore || !crypto.backup.enabled {
|
2022-04-14 10:06:12 +00:00
|
|
|
MXLog.debug("[LegacyAuthenticationCoordinator][MXKeyVerification] requestAllPrivateKeys: Request key backup private keys")
|
2022-02-17 16:50:02 +00:00
|
|
|
crypto.setOutgoingKeyRequestsEnabled(true, onComplete: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
navigationRouter.dismissModule(animated: true) { [weak self] in
|
|
|
|
self?.authenticationDidComplete()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func keyVerificationCoordinatorDidCancel(_ coordinator: KeyVerificationCoordinatorType) {
|
|
|
|
navigationRouter.dismissModule(animated: true) { [weak self] in
|
|
|
|
self?.authenticationDidComplete()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - UIAdaptivePresentationControllerDelegate
|
2022-04-14 10:06:12 +00:00
|
|
|
@available(iOS 14.0, *)
|
2022-02-17 16:50:02 +00:00
|
|
|
extension AuthenticationCoordinator: UIAdaptivePresentationControllerDelegate {
|
|
|
|
func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool {
|
|
|
|
// Prevent Key Verification from using swipe to dismiss
|
|
|
|
return false
|
2022-01-12 17:11:29 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-14 10:06:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: - Unused conformances
|
|
|
|
@available(iOS 14.0, *)
|
|
|
|
extension AuthenticationCoordinator {
|
|
|
|
var customServerFieldsVisible: Bool {
|
|
|
|
get { false }
|
|
|
|
set { /* no-op */ }
|
|
|
|
}
|
|
|
|
|
|
|
|
func update(authenticationType: MXKAuthenticationType) {
|
|
|
|
// unused
|
|
|
|
}
|
|
|
|
|
|
|
|
func update(externalRegistrationParameters: [AnyHashable: Any]) {
|
|
|
|
// unused
|
|
|
|
}
|
|
|
|
|
|
|
|
func update(softLogoutCredentials: MXCredentials) {
|
|
|
|
// unused
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateHomeserver(_ homeserver: String?, andIdentityServer identityServer: String?) {
|
|
|
|
// unused
|
|
|
|
}
|
|
|
|
|
|
|
|
func continueSSOLogin(withToken loginToken: String, transactionID: String) -> Bool {
|
|
|
|
#warning("To be implemented elsewhere")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|