2019-04-05 07:37:15 +00:00
|
|
|
// File created from FlowTemplate
|
|
|
|
// $ createRootCoordinator.sh DeviceVerification DeviceVerification DeviceVerificationStart
|
|
|
|
/*
|
|
|
|
Copyright 2019 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
|
|
|
|
|
|
|
|
@objcMembers
|
|
|
|
final class DeviceVerificationCoordinator: DeviceVerificationCoordinatorType {
|
|
|
|
|
|
|
|
// MARK: - Properties
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
private let navigationRouter: NavigationRouterType
|
|
|
|
private let session: MXSession
|
2019-04-05 13:15:06 +00:00
|
|
|
private let otherUserId: String
|
|
|
|
private let otherDeviceId: String
|
|
|
|
|
2019-04-12 21:19:07 +00:00
|
|
|
private var incomingTransaction: MXIncomingSASTransaction?
|
2020-01-14 20:51:05 +00:00
|
|
|
private var incomingKeyVerificationRequest: MXKeyVerificationRequest?
|
2019-04-05 13:15:06 +00:00
|
|
|
|
2019-04-05 07:37:15 +00:00
|
|
|
// MARK: Public
|
|
|
|
|
|
|
|
// Must be used only internally
|
|
|
|
var childCoordinators: [Coordinator] = []
|
|
|
|
|
|
|
|
weak var delegate: DeviceVerificationCoordinatorDelegate?
|
|
|
|
|
|
|
|
// MARK: - Setup
|
2019-04-05 13:15:06 +00:00
|
|
|
|
|
|
|
/// Contrustor to start a verification of another device.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - session: the MXSession
|
|
|
|
/// - otherUserId: the device user id
|
|
|
|
/// - otherDevice: the device id
|
|
|
|
init(session: MXSession, otherUserId: String, otherDeviceId: String) {
|
2019-04-05 07:37:15 +00:00
|
|
|
self.navigationRouter = NavigationRouter(navigationController: RiotNavigationController())
|
|
|
|
self.session = session
|
2019-04-05 13:15:06 +00:00
|
|
|
self.otherUserId = otherUserId
|
|
|
|
self.otherDeviceId = otherDeviceId
|
|
|
|
}
|
|
|
|
|
2019-04-12 21:19:07 +00:00
|
|
|
/// Contrustor to manage an incoming SAS device verification transaction
|
2019-04-05 13:15:06 +00:00
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - session: the MXSession
|
|
|
|
/// - transaction: an existing device verification transaction
|
2019-04-12 21:19:07 +00:00
|
|
|
convenience init(session: MXSession, incomingTransaction: MXIncomingSASTransaction) {
|
2019-04-05 13:15:06 +00:00
|
|
|
self.init(session: session,
|
2019-04-12 21:19:07 +00:00
|
|
|
otherUserId: incomingTransaction.otherUserId,
|
|
|
|
otherDeviceId: incomingTransaction.otherDeviceId)
|
|
|
|
self.incomingTransaction = incomingTransaction
|
2019-04-05 13:15:06 +00:00
|
|
|
}
|
2019-04-05 07:37:15 +00:00
|
|
|
|
2020-01-14 20:51:05 +00:00
|
|
|
/// Contrustor to manage an incoming SAS device verification transaction
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - session: the MXSession
|
|
|
|
/// - incomingKeyVerificationRequest: An existing incoming key verification request to accept
|
|
|
|
convenience init(session: MXSession, incomingKeyVerificationRequest: MXKeyVerificationRequest) {
|
|
|
|
self.init(session: session, otherUserId: incomingKeyVerificationRequest.sender, otherDeviceId: incomingKeyVerificationRequest.fromDevice)
|
|
|
|
self.incomingKeyVerificationRequest = incomingKeyVerificationRequest
|
|
|
|
}
|
|
|
|
|
2019-04-05 07:37:15 +00:00
|
|
|
// MARK: - Public methods
|
|
|
|
|
|
|
|
func start() {
|
2020-01-14 20:51:05 +00:00
|
|
|
let rootCoordinator: Coordinator & Presentable
|
|
|
|
|
|
|
|
if let incomingKeyVerificationRequest = self.incomingKeyVerificationRequest {
|
|
|
|
rootCoordinator = self.createDataLoadingScreenCoordinator(with: incomingKeyVerificationRequest)
|
|
|
|
} else {
|
|
|
|
rootCoordinator = self.createDataLoadingScreenCoordinator()
|
|
|
|
}
|
|
|
|
|
2019-04-24 12:42:16 +00:00
|
|
|
rootCoordinator.start()
|
2020-01-14 20:51:05 +00:00
|
|
|
|
2019-04-24 12:42:16 +00:00
|
|
|
self.add(childCoordinator: rootCoordinator)
|
2019-10-23 15:09:58 +00:00
|
|
|
self.navigationRouter.setRootModule(rootCoordinator) { [weak self] in
|
|
|
|
self?.remove(childCoordinator: rootCoordinator)
|
|
|
|
}
|
2019-04-17 10:47:42 +00:00
|
|
|
}
|
2019-04-05 07:37:15 +00:00
|
|
|
|
|
|
|
func toPresentable() -> UIViewController {
|
|
|
|
return self.navigationRouter.toPresentable()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Private methods
|
|
|
|
|
2019-04-24 12:42:16 +00:00
|
|
|
private func createDataLoadingScreenCoordinator() -> DeviceVerificationDataLoadingCoordinator {
|
|
|
|
let coordinator = DeviceVerificationDataLoadingCoordinator(session: self.session, otherUserId: self.otherUserId, otherDeviceId: self.otherDeviceId)
|
|
|
|
coordinator.delegate = self
|
|
|
|
coordinator.start()
|
|
|
|
|
|
|
|
return coordinator
|
2019-04-17 10:47:42 +00:00
|
|
|
}
|
2020-01-14 20:51:05 +00:00
|
|
|
|
|
|
|
private func createDataLoadingScreenCoordinator(with keyVerificationRequest: MXKeyVerificationRequest) -> DeviceVerificationDataLoadingCoordinator {
|
|
|
|
let coordinator = DeviceVerificationDataLoadingCoordinator(incomingKeyVerificationRequest: keyVerificationRequest)
|
|
|
|
coordinator.delegate = self
|
|
|
|
coordinator.start()
|
|
|
|
|
|
|
|
return coordinator
|
|
|
|
}
|
2019-04-17 10:47:42 +00:00
|
|
|
|
|
|
|
private func showStart(otherUser: MXUser, otherDevice: MXDeviceInfo) {
|
2019-04-05 13:15:06 +00:00
|
|
|
let coordinator = DeviceVerificationStartCoordinator(session: self.session, otherUser: otherUser, otherDevice: otherDevice)
|
2019-04-05 07:37:15 +00:00
|
|
|
coordinator.delegate = self
|
2019-04-17 10:47:42 +00:00
|
|
|
coordinator.start()
|
|
|
|
|
|
|
|
self.add(childCoordinator: coordinator)
|
2019-10-23 15:09:58 +00:00
|
|
|
self.navigationRouter.setRootModule(coordinator) { [weak self] in
|
|
|
|
self?.remove(childCoordinator: coordinator)
|
|
|
|
}
|
2019-04-05 07:37:15 +00:00
|
|
|
}
|
2019-04-05 17:09:29 +00:00
|
|
|
|
2019-04-17 10:47:42 +00:00
|
|
|
private func showIncoming(otherUser: MXUser, transaction: MXIncomingSASTransaction) {
|
2019-04-12 21:19:07 +00:00
|
|
|
let coordinator = DeviceVerificationIncomingCoordinator(session: self.session, otherUser: otherUser, transaction: transaction)
|
|
|
|
coordinator.delegate = self
|
2019-04-17 10:47:42 +00:00
|
|
|
coordinator.start()
|
|
|
|
|
|
|
|
self.add(childCoordinator: coordinator)
|
2019-10-23 15:09:58 +00:00
|
|
|
self.navigationRouter.setRootModule(coordinator) { [weak self] in
|
|
|
|
self?.remove(childCoordinator: coordinator)
|
|
|
|
}
|
2019-04-12 21:19:07 +00:00
|
|
|
}
|
2019-04-05 17:09:29 +00:00
|
|
|
|
2019-04-12 21:19:07 +00:00
|
|
|
private func showVerify(transaction: MXSASTransaction, animated: Bool) {
|
2019-04-05 17:09:29 +00:00
|
|
|
let coordinator = DeviceVerificationVerifyCoordinator(session: self.session, transaction: transaction)
|
|
|
|
coordinator.delegate = self
|
|
|
|
coordinator.start()
|
|
|
|
|
|
|
|
self.add(childCoordinator: coordinator)
|
|
|
|
self.navigationRouter.push(coordinator, animated: animated) { [weak self] in
|
|
|
|
self?.remove(childCoordinator: coordinator)
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 16:40:04 +00:00
|
|
|
|
|
|
|
private func showVerified(animated: Bool) {
|
|
|
|
let viewController = DeviceVerificationVerifiedViewController.instantiate()
|
|
|
|
viewController.delegate = self
|
|
|
|
self.navigationRouter.setRootModule(viewController)
|
|
|
|
}
|
2019-04-05 07:37:15 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 12:42:16 +00:00
|
|
|
extension DeviceVerificationCoordinator: DeviceVerificationDataLoadingCoordinatorDelegate {
|
|
|
|
func deviceVerificationDataLoadingCoordinator(_ coordinator: DeviceVerificationDataLoadingCoordinatorType, didLoadUser user: MXUser, device: MXDeviceInfo) {
|
2019-04-17 10:47:42 +00:00
|
|
|
|
|
|
|
if let incomingTransaction = self.incomingTransaction {
|
|
|
|
self.showIncoming(otherUser: user, transaction: incomingTransaction)
|
|
|
|
} else {
|
|
|
|
self.showStart(otherUser: user, otherDevice: device)
|
|
|
|
}
|
|
|
|
}
|
2020-01-14 20:51:05 +00:00
|
|
|
|
|
|
|
func deviceVerificationDataLoadingCoordinator(_ coordinator: DeviceVerificationDataLoadingCoordinatorType, didAcceptKeyVerificationRequestWithTransaction transaction: MXDeviceVerificationTransaction) {
|
|
|
|
|
|
|
|
if let sasTransaction = transaction as? MXSASTransaction {
|
|
|
|
self.showVerify(transaction: sasTransaction, animated: true)
|
|
|
|
} else {
|
|
|
|
NSLog("[DeviceVerificationCoordinator] Transaction \(transaction) is not supported")
|
|
|
|
self.delegate?.deviceVerificationCoordinatorDidComplete(self, otherUserId: self.otherUserId, otherDeviceId: self.otherDeviceId)
|
|
|
|
}
|
|
|
|
}
|
2019-04-17 10:47:42 +00:00
|
|
|
|
2019-04-24 12:42:16 +00:00
|
|
|
func deviceVerificationDataLoadingCoordinatorDidCancel(_ coordinator: DeviceVerificationDataLoadingCoordinatorType) {
|
2019-04-17 10:47:42 +00:00
|
|
|
self.delegate?.deviceVerificationCoordinatorDidComplete(self, otherUserId: self.otherUserId, otherDeviceId: self.otherDeviceId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 07:37:15 +00:00
|
|
|
extension DeviceVerificationCoordinator: DeviceVerificationStartCoordinatorDelegate {
|
2019-04-05 13:15:06 +00:00
|
|
|
func deviceVerificationStartCoordinator(_ coordinator: DeviceVerificationStartCoordinatorType, didCompleteWithOutgoingTransaction transaction: MXSASTransaction) {
|
2019-04-12 21:19:07 +00:00
|
|
|
self.showVerify(transaction: transaction, animated: true)
|
2019-04-05 13:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func deviceVerificationStartCoordinator(_ coordinator: DeviceVerificationStartCoordinatorType, didTransactionCancelled transaction: MXSASTransaction) {
|
2019-04-17 06:09:43 +00:00
|
|
|
self.delegate?.deviceVerificationCoordinatorDidComplete(self, otherUserId: self.otherUserId, otherDeviceId: self.otherDeviceId)
|
2019-04-05 07:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func deviceVerificationStartCoordinatorDidCancel(_ coordinator: DeviceVerificationStartCoordinatorType) {
|
2019-04-17 06:09:43 +00:00
|
|
|
self.delegate?.deviceVerificationCoordinatorDidComplete(self, otherUserId: self.otherUserId, otherDeviceId: self.otherDeviceId)
|
2019-04-05 07:37:15 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-05 17:09:29 +00:00
|
|
|
|
2019-04-12 21:19:07 +00:00
|
|
|
extension DeviceVerificationCoordinator: DeviceVerificationIncomingCoordinatorDelegate {
|
|
|
|
func deviceVerificationIncomingCoordinator(_ coordinator: DeviceVerificationIncomingCoordinatorType, didAcceptTransaction transaction: MXSASTransaction) {
|
|
|
|
self.showVerify(transaction: transaction, animated: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deviceVerificationIncomingCoordinatorDidCancel(_ coordinator: DeviceVerificationIncomingCoordinatorType) {
|
2019-04-17 06:09:43 +00:00
|
|
|
self.delegate?.deviceVerificationCoordinatorDidComplete(self, otherUserId: self.otherUserId, otherDeviceId: self.otherDeviceId)
|
2019-04-12 21:19:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 17:09:29 +00:00
|
|
|
extension DeviceVerificationCoordinator: DeviceVerificationVerifyCoordinatorDelegate {
|
|
|
|
func deviceVerificationVerifyCoordinatorDidComplete(_ coordinator: DeviceVerificationVerifyCoordinatorType) {
|
2019-04-15 16:40:04 +00:00
|
|
|
self.showVerified(animated: true)
|
2019-04-05 17:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func deviceVerificationVerifyCoordinatorDidCancel(_ coordinator: DeviceVerificationVerifyCoordinatorType) {
|
2019-04-17 06:09:43 +00:00
|
|
|
self.delegate?.deviceVerificationCoordinatorDidComplete(self, otherUserId: self.otherUserId, otherDeviceId: self.otherDeviceId)
|
2019-04-05 17:09:29 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-15 16:40:04 +00:00
|
|
|
|
|
|
|
extension DeviceVerificationCoordinator: DeviceVerificationVerifiedViewControllerDelegate {
|
|
|
|
func deviceVerificationVerifiedViewControllerDidTapSetupAction(_ viewController: DeviceVerificationVerifiedViewController) {
|
2019-04-17 06:09:43 +00:00
|
|
|
self.delegate?.deviceVerificationCoordinatorDidComplete(self, otherUserId: self.otherUserId, otherDeviceId: self.otherDeviceId)
|
2019-04-15 16:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func deviceVerificationVerifiedViewControllerDidCancel(_ viewController: DeviceVerificationVerifiedViewController) {
|
2019-04-17 06:09:43 +00:00
|
|
|
self.delegate?.deviceVerificationCoordinatorDidComplete(self, otherUserId: self.otherUserId, otherDeviceId: self.otherDeviceId)
|
2019-04-15 16:40:04 +00:00
|
|
|
}
|
|
|
|
}
|