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 Foundation
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
/// AuthenticationCoordinator input parameters
|
|
|
|
struct AuthenticationCoordinatorParameters {
|
|
|
|
/// The initial type of authentication to be shown
|
|
|
|
let authenticationType: MXKAuthenticationType
|
|
|
|
/// The registration parameters.
|
|
|
|
let externalRegistrationParameters: [AnyHashable: Any]?
|
|
|
|
/// The credentials to use after a soft logout has taken place.
|
|
|
|
let softLogoutCredentials: MXCredentials?
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-18 16:48:52 +00:00
|
|
|
/// A coordinator that handles authentication, verification and setting a PIN.
|
2022-01-12 17:11:29 +00:00
|
|
|
final class AuthenticationCoordinator: NSObject, AuthenticationCoordinatorProtocol {
|
|
|
|
|
|
|
|
// MARK: - Properties
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
private let parameters: AuthenticationCoordinatorParameters
|
|
|
|
private let authenticationViewController: AuthenticationViewController
|
|
|
|
|
|
|
|
// MARK: Public
|
|
|
|
|
|
|
|
// Must be used only internally
|
|
|
|
var childCoordinators: [Coordinator] = []
|
2022-01-18 16:48:52 +00:00
|
|
|
var completion: (() -> Void)?
|
2022-01-12 17:11:29 +00:00
|
|
|
|
|
|
|
// MARK: - Setup
|
|
|
|
|
|
|
|
init(parameters: AuthenticationCoordinatorParameters) {
|
|
|
|
self.parameters = parameters
|
|
|
|
|
|
|
|
let authenticationViewController = AuthenticationViewController()
|
|
|
|
self.authenticationViewController = authenticationViewController
|
|
|
|
|
|
|
|
super.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Public
|
|
|
|
|
|
|
|
func start() {
|
|
|
|
// Listen to the end of the authentication flow
|
|
|
|
authenticationViewController.authVCDelegate = self
|
|
|
|
|
2022-01-18 16:48:52 +00:00
|
|
|
// Set authType first as registration parameters or soft logout credentials
|
|
|
|
// may update this afterwards to handle those use cases.
|
2022-01-12 17:11:29 +00:00
|
|
|
authenticationViewController.authType = parameters.authenticationType
|
|
|
|
if let externalRegistrationParameters = parameters.externalRegistrationParameters {
|
|
|
|
authenticationViewController.externalRegistrationParameters = externalRegistrationParameters
|
|
|
|
}
|
|
|
|
if let softLogoutCredentials = parameters.softLogoutCredentials {
|
|
|
|
authenticationViewController.softLogoutCredentials = softLogoutCredentials
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toPresentable() -> UIViewController {
|
|
|
|
return self.authenticationViewController
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:48:52 +00:00
|
|
|
/// Force a registration process based on a predefined set of parameters from a server provisioning link.
|
|
|
|
/// For more information see `AuthenticationViewController.externalRegistrationParameters`.
|
2022-01-12 17:11:29 +00:00
|
|
|
func update(externalRegistrationParameters: [AnyHashable: Any]) {
|
|
|
|
authenticationViewController.externalRegistrationParameters = externalRegistrationParameters
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:48:52 +00:00
|
|
|
/// Set up the authentication screen with the specified homeserver and/or identity server.
|
2022-01-27 14:44:10 +00:00
|
|
|
func updateHomeserver(_ homeserver: String?, andIdentityServer identityServer: String?) {
|
2022-01-12 17:11:29 +00:00
|
|
|
authenticationViewController.showCustomHomeserver(homeserver, andIdentityServer: identityServer)
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:48:52 +00:00
|
|
|
/// When SSO login succeeded, when SFSafariViewController is used, continue login with success parameters.
|
2022-01-12 17:11:29 +00:00
|
|
|
func continueSSOLogin(withToken loginToken: String, transactionID: String) -> Bool {
|
|
|
|
authenticationViewController.continueSSOLogin(withToken: loginToken, txnId: transactionID)
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:48:52 +00:00
|
|
|
/// Preload `AuthenticationViewController` from it's xib file to avoid locking up the UI when before presentation.
|
2022-01-12 17:11:29 +00:00
|
|
|
static func preload() {
|
|
|
|
let authenticationViewController = AuthenticationViewController()
|
|
|
|
authenticationViewController.loadViewIfNeeded()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-17 17:37:59 +00:00
|
|
|
// MARK: - AuthenticationViewControllerDelegate
|
2022-01-12 17:11:29 +00:00
|
|
|
extension AuthenticationCoordinator: AuthenticationViewControllerDelegate {
|
|
|
|
func authenticationViewControllerDidDismiss(_ authenticationViewController: AuthenticationViewController!) {
|
2022-01-18 16:48:52 +00:00
|
|
|
completion?()
|
2022-01-12 17:11:29 +00:00
|
|
|
}
|
|
|
|
}
|