mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-27 19:56:57 +02:00
Refactor DeviceVerification prefix classes to KeyVerification where needed. And move key verification related classes into the same module KeyVerification.
This commit is contained in:
@@ -0,0 +1,325 @@
|
||||
// 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 KeyVerificationCoordinator: KeyVerificationCoordinatorType {
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private let navigationRouter: NavigationRouterType
|
||||
private let session: MXSession
|
||||
private let otherUserId: String
|
||||
private let otherDeviceId: String
|
||||
|
||||
private var incomingTransaction: MXIncomingSASTransaction?
|
||||
private var incomingKeyVerificationRequest: MXKeyVerificationRequest?
|
||||
|
||||
private var verificationKind: KeyVerificationKind = .device
|
||||
private var roomMember: MXRoomMember?
|
||||
|
||||
// MARK: Public
|
||||
|
||||
// Must be used only internally
|
||||
var childCoordinators: [Coordinator] = []
|
||||
|
||||
weak var delegate: KeyVerificationCoordinatorDelegate?
|
||||
|
||||
// MARK: - Setup
|
||||
|
||||
/// 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) {
|
||||
self.navigationRouter = NavigationRouter(navigationController: RiotNavigationController())
|
||||
self.session = session
|
||||
self.otherUserId = otherUserId
|
||||
self.otherDeviceId = otherDeviceId
|
||||
}
|
||||
|
||||
init(navigationRouter: NavigationRouterType, session: MXSession, userId: String, otherDeviceId: String) {
|
||||
self.navigationRouter = navigationRouter
|
||||
self.session = session
|
||||
self.otherUserId = userId
|
||||
self.otherDeviceId = otherDeviceId
|
||||
}
|
||||
|
||||
/// Contrustor to manage an incoming SAS device verification transaction
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - session: the MXSession
|
||||
/// - transaction: an existing device verification transaction
|
||||
convenience init(session: MXSession, incomingTransaction: MXIncomingSASTransaction) {
|
||||
self.init(session: session,
|
||||
otherUserId: incomingTransaction.otherUserId,
|
||||
otherDeviceId: incomingTransaction.otherDeviceId)
|
||||
self.incomingTransaction = incomingTransaction
|
||||
}
|
||||
|
||||
/// 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) {
|
||||
let otherDeviceId = incomingKeyVerificationRequest.otherDevice ?? incomingKeyVerificationRequest.fromDevice
|
||||
|
||||
self.init(session: session, otherUserId: incomingKeyVerificationRequest.otherUser, otherDeviceId: otherDeviceId)
|
||||
self.incomingKeyVerificationRequest = incomingKeyVerificationRequest
|
||||
}
|
||||
|
||||
/// Constructor to start a user verification.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - session: the MXSession
|
||||
/// - roomMember: an other room member
|
||||
init(session: MXSession, roomMember: MXRoomMember) {
|
||||
self.navigationRouter = NavigationRouter(navigationController: RiotNavigationController())
|
||||
self.session = session
|
||||
self.otherUserId = roomMember.userId
|
||||
self.otherDeviceId = ""
|
||||
self.roomMember = roomMember
|
||||
self.verificationKind = .user
|
||||
}
|
||||
|
||||
// MARK: - Public methods
|
||||
|
||||
func start() {
|
||||
let rootCoordinator: Coordinator & Presentable
|
||||
|
||||
if let incomingKeyVerificationRequest = self.incomingKeyVerificationRequest {
|
||||
rootCoordinator = self.createDataLoadingScreenCoordinator(with: incomingKeyVerificationRequest)
|
||||
} else if let roomMember = self.roomMember {
|
||||
rootCoordinator = self.createUserVerificationStartCoordinator(with: roomMember)
|
||||
} else {
|
||||
rootCoordinator = self.createDataLoadingScreenCoordinator()
|
||||
}
|
||||
|
||||
rootCoordinator.start()
|
||||
|
||||
self.add(childCoordinator: rootCoordinator)
|
||||
|
||||
if self.navigationRouter.modules.isEmpty == false {
|
||||
self.navigationRouter.push(rootCoordinator, animated: true, popCompletion: { [weak self] in
|
||||
self?.remove(childCoordinator: rootCoordinator)
|
||||
})
|
||||
} else {
|
||||
self.navigationRouter.setRootModule(rootCoordinator) { [weak self] in
|
||||
self?.remove(childCoordinator: rootCoordinator)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func toPresentable() -> UIViewController {
|
||||
return self.navigationRouter.toPresentable()
|
||||
}
|
||||
|
||||
// MARK: - Private methods
|
||||
|
||||
private func createDataLoadingScreenCoordinator() -> KeyVerificationDataLoadingCoordinator {
|
||||
let coordinator = KeyVerificationDataLoadingCoordinator(session: self.session, otherUserId: self.otherUserId, otherDeviceId: self.otherDeviceId)
|
||||
coordinator.delegate = self
|
||||
coordinator.start()
|
||||
|
||||
return coordinator
|
||||
}
|
||||
|
||||
private func createDataLoadingScreenCoordinator(with keyVerificationRequest: MXKeyVerificationRequest) -> KeyVerificationDataLoadingCoordinator {
|
||||
let coordinator = KeyVerificationDataLoadingCoordinator(session: self.session, incomingKeyVerificationRequest: keyVerificationRequest)
|
||||
coordinator.delegate = self
|
||||
coordinator.start()
|
||||
|
||||
return coordinator
|
||||
}
|
||||
|
||||
private func createUserVerificationStartCoordinator(with roomMember: MXRoomMember) -> UserVerificationStartCoordinator {
|
||||
let coordinator = UserVerificationStartCoordinator(session: self.session, roomMember: roomMember)
|
||||
coordinator.delegate = self
|
||||
coordinator.start()
|
||||
|
||||
return coordinator
|
||||
}
|
||||
|
||||
private func showStart(otherUser: MXUser, otherDevice: MXDeviceInfo) {
|
||||
let coordinator = DeviceVerificationStartCoordinator(session: self.session, otherUser: otherUser, otherDevice: otherDevice)
|
||||
coordinator.delegate = self
|
||||
coordinator.start()
|
||||
|
||||
self.add(childCoordinator: coordinator)
|
||||
self.navigationRouter.setRootModule(coordinator) { [weak self] in
|
||||
self?.remove(childCoordinator: coordinator)
|
||||
}
|
||||
}
|
||||
|
||||
private func showIncoming(otherUser: MXUser, transaction: MXIncomingSASTransaction) {
|
||||
let coordinator = DeviceVerificationIncomingCoordinator(session: self.session, otherUser: otherUser, transaction: transaction)
|
||||
coordinator.delegate = self
|
||||
coordinator.start()
|
||||
|
||||
self.add(childCoordinator: coordinator)
|
||||
self.navigationRouter.setRootModule(coordinator) { [weak self] in
|
||||
self?.remove(childCoordinator: coordinator)
|
||||
}
|
||||
}
|
||||
|
||||
private func showVerifyBySAS(transaction: MXSASTransaction, animated: Bool) {
|
||||
let coordinator = KeyVerificationVerifyBySASCoordinator(session: self.session, transaction: transaction, verificationKind: self.verificationKind)
|
||||
coordinator.delegate = self
|
||||
coordinator.start()
|
||||
|
||||
self.add(childCoordinator: coordinator)
|
||||
self.navigationRouter.push(coordinator, animated: animated) { [weak self] in
|
||||
self?.remove(childCoordinator: coordinator)
|
||||
}
|
||||
}
|
||||
|
||||
private func showVerifyByScanning(keyVerificationRequest: MXKeyVerificationRequest, animated: Bool) {
|
||||
let coordinator = KeyVerificationVerifyByScanningCoordinator(session: self.session, keyVerificationRequest: keyVerificationRequest)
|
||||
coordinator.delegate = self
|
||||
coordinator.start()
|
||||
|
||||
self.add(childCoordinator: coordinator)
|
||||
self.navigationRouter.push(coordinator, animated: animated) { [weak self] in
|
||||
self?.remove(childCoordinator: coordinator)
|
||||
}
|
||||
}
|
||||
|
||||
private func showVerified(animated: Bool) {
|
||||
let viewController = KeyVerificationVerifiedViewController.instantiate(with: self.verificationKind)
|
||||
viewController.delegate = self
|
||||
self.navigationRouter.setRootModule(viewController)
|
||||
}
|
||||
}
|
||||
|
||||
extension KeyVerificationCoordinator: KeyVerificationDataLoadingCoordinatorDelegate {
|
||||
|
||||
func keyVerificationDataLoadingCoordinator(_ coordinator: KeyVerificationDataLoadingCoordinatorType, didAcceptKeyVerificationRequest keyVerificationRequest: MXKeyVerificationRequest) {
|
||||
self.showVerifyByScanning(keyVerificationRequest: keyVerificationRequest, animated: true)
|
||||
}
|
||||
|
||||
func keyVerificationDataLoadingCoordinator(_ coordinator: KeyVerificationDataLoadingCoordinatorType, didLoadUser user: MXUser, device: MXDeviceInfo) {
|
||||
|
||||
if let incomingTransaction = self.incomingTransaction {
|
||||
self.showIncoming(otherUser: user, transaction: incomingTransaction)
|
||||
} else {
|
||||
self.showStart(otherUser: user, otherDevice: device)
|
||||
}
|
||||
}
|
||||
|
||||
func keyVerificationDataLoadingCoordinator(_ coordinator: KeyVerificationDataLoadingCoordinatorType, didAcceptKeyVerificationRequestWithTransaction transaction: MXKeyVerificationTransaction) {
|
||||
|
||||
if let sasTransaction = transaction as? MXSASTransaction {
|
||||
self.showVerifyBySAS(transaction: sasTransaction, animated: true)
|
||||
} else {
|
||||
NSLog("[KeyVerificationCoordinator] Transaction \(transaction) is not supported")
|
||||
self.delegate?.keyVerificationCoordinatorDidComplete(self, otherUserId: self.otherUserId, otherDeviceId: self.otherDeviceId)
|
||||
}
|
||||
}
|
||||
|
||||
func keyVerificationDataLoadingCoordinatorDidCancel(_ coordinator: KeyVerificationDataLoadingCoordinatorType) {
|
||||
self.delegate?.keyVerificationCoordinatorDidComplete(self, otherUserId: self.otherUserId, otherDeviceId: self.otherDeviceId)
|
||||
}
|
||||
}
|
||||
|
||||
extension KeyVerificationCoordinator: DeviceVerificationStartCoordinatorDelegate {
|
||||
func deviceVerificationStartCoordinator(_ coordinator: DeviceVerificationStartCoordinatorType, didCompleteWithOutgoingTransaction transaction: MXSASTransaction) {
|
||||
self.showVerifyBySAS(transaction: transaction, animated: true)
|
||||
}
|
||||
|
||||
func deviceVerificationStartCoordinator(_ coordinator: DeviceVerificationStartCoordinatorType, didTransactionCancelled transaction: MXSASTransaction) {
|
||||
self.delegate?.keyVerificationCoordinatorDidComplete(self, otherUserId: self.otherUserId, otherDeviceId: self.otherDeviceId)
|
||||
}
|
||||
|
||||
func deviceVerificationStartCoordinatorDidCancel(_ coordinator: DeviceVerificationStartCoordinatorType) {
|
||||
self.delegate?.keyVerificationCoordinatorDidComplete(self, otherUserId: self.otherUserId, otherDeviceId: self.otherDeviceId)
|
||||
}
|
||||
}
|
||||
|
||||
extension KeyVerificationCoordinator: DeviceVerificationIncomingCoordinatorDelegate {
|
||||
func deviceVerificationIncomingCoordinator(_ coordinator: DeviceVerificationIncomingCoordinatorType, didAcceptTransaction transaction: MXSASTransaction) {
|
||||
self.showVerifyBySAS(transaction: transaction, animated: true)
|
||||
}
|
||||
|
||||
func deviceVerificationIncomingCoordinatorDidCancel(_ coordinator: DeviceVerificationIncomingCoordinatorType) {
|
||||
self.delegate?.keyVerificationCoordinatorDidComplete(self, otherUserId: self.otherUserId, otherDeviceId: self.otherDeviceId)
|
||||
}
|
||||
}
|
||||
|
||||
extension KeyVerificationCoordinator: KeyVerificationVerifyBySASCoordinatorDelegate {
|
||||
func keyVerificationVerifyBySASCoordinatorDidComplete(_ coordinator: KeyVerificationVerifyBySASCoordinatorType) {
|
||||
self.showVerified(animated: true)
|
||||
}
|
||||
|
||||
func keyVerificationVerifyBySASCoordinatorDidCancel(_ coordinator: KeyVerificationVerifyBySASCoordinatorType) {
|
||||
self.delegate?.keyVerificationCoordinatorDidComplete(self, otherUserId: self.otherUserId, otherDeviceId: self.otherDeviceId)
|
||||
}
|
||||
}
|
||||
|
||||
extension KeyVerificationCoordinator: KeyVerificationVerifiedViewControllerDelegate {
|
||||
func keyVerificationVerifiedViewControllerDidTapSetupAction(_ viewController: KeyVerificationVerifiedViewController) {
|
||||
self.delegate?.keyVerificationCoordinatorDidComplete(self, otherUserId: self.otherUserId, otherDeviceId: self.otherDeviceId)
|
||||
}
|
||||
|
||||
func keyVerificationVerifiedViewControllerDidCancel(_ viewController: KeyVerificationVerifiedViewController) {
|
||||
self.delegate?.keyVerificationCoordinatorDidComplete(self, otherUserId: self.otherUserId, otherDeviceId: self.otherDeviceId)
|
||||
}
|
||||
}
|
||||
|
||||
extension KeyVerificationCoordinator: UserVerificationStartCoordinatorDelegate {
|
||||
|
||||
func userVerificationStartCoordinator(_ coordinator: UserVerificationStartCoordinatorType, otherDidAcceptRequest request: MXKeyVerificationRequest) {
|
||||
self.showVerifyByScanning(keyVerificationRequest: request, animated: true)
|
||||
}
|
||||
|
||||
func userVerificationStartCoordinator(_ coordinator: UserVerificationStartCoordinatorType, didCompleteWithOutgoingTransaction transaction: MXSASTransaction) {
|
||||
self.showVerifyBySAS(transaction: transaction, animated: true)
|
||||
}
|
||||
|
||||
func userVerificationStartCoordinator(_ coordinator: UserVerificationStartCoordinatorType, didTransactionCancelled transaction: MXSASTransaction) {
|
||||
self.delegate?.keyVerificationCoordinatorDidComplete(self, otherUserId: self.otherUserId, otherDeviceId: self.otherDeviceId)
|
||||
}
|
||||
|
||||
func userVerificationStartCoordinatorDidCancel(_ coordinator: UserVerificationStartCoordinatorType) {
|
||||
self.delegate?.keyVerificationCoordinatorDidComplete(self, otherUserId: self.otherUserId, otherDeviceId: self.otherDeviceId)
|
||||
}
|
||||
}
|
||||
|
||||
extension KeyVerificationCoordinator: KeyVerificationVerifyByScanningCoordinatorDelegate {
|
||||
|
||||
func keyVerificationVerifyByScanningCoordinatorDidCancel(_ coordinator: KeyVerificationVerifyByScanningCoordinatorType) {
|
||||
self.delegate?.keyVerificationCoordinatorDidComplete(self, otherUserId: self.otherUserId, otherDeviceId: self.otherDeviceId)
|
||||
}
|
||||
|
||||
func keyVerificationVerifyByScanningCoordinatorCannotScan(_ coordinator: KeyVerificationVerifyByScanningCoordinatorType) {
|
||||
self.showVerified(animated: true)
|
||||
}
|
||||
|
||||
func keyVerificationVerifyByScanningCoordinatorDidCompleteQRCodeVerification(_ coordinator: KeyVerificationVerifyByScanningCoordinatorType) {
|
||||
self.showVerified(animated: true)
|
||||
}
|
||||
|
||||
func keyVerificationVerifyByScanningCoordinator(_ coordinator: KeyVerificationVerifyByScanningCoordinatorType, didCompleteWithSASTransaction transaction: MXSASTransaction) {
|
||||
self.showVerifyBySAS(transaction: transaction, animated: true)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user