Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a different green spinner during onboarding #5623

Merged
merged 1 commit into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Riot/Modules/Application/LegacyAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -2410,6 +2410,12 @@ - (void)handleAppState

MXLogDebug(@"[AppDelegate] handleAppState: isLaunching: %@", isLaunching ? @"YES" : @"NO");

if (self.masterTabBarController.isOnboardingInProgress)
{
MXLogDebug(@"[AppDelegate] handleAppState: Skipping LaunchLoadingView due to Onboarding.");
return;
}

if (isLaunching)
{
MXLogDebug(@"[AppDelegate] handleAppState: LaunchLoadingView");
Expand Down
165 changes: 160 additions & 5 deletions Riot/Modules/Authentication/AuthenticationCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
limitations under the License.
*/

import Foundation
import UIKit

/// A coordinator that handles authentication, verification and setting a PIN.
Expand All @@ -26,17 +25,27 @@ final class AuthenticationCoordinator: NSObject, AuthenticationCoordinatorProtoc

// MARK: Private

private let navigationRouter: NavigationRouterType

private let authenticationViewController: AuthenticationViewController
private let crossSigningService = CrossSigningService()

/// The password entered, for use when setting up cross-signing.
private var password: String?
/// The session created when successfully authenticated.
private var session: MXSession?

// MARK: Public

// Must be used only internally
var childCoordinators: [Coordinator] = []
var completion: ((MXKAuthenticationType) -> Void)?
var completion: ((AuthenticationCoordinatorResult) -> Void)?

// MARK: - Setup

override init() {
init(parameters: AuthenticationCoordinatorParameters) {
self.navigationRouter = parameters.navigationRouter

let authenticationViewController = AuthenticationViewController()
self.authenticationViewController = authenticationViewController

Expand Down Expand Up @@ -81,11 +90,157 @@ final class AuthenticationCoordinator: NSObject, AuthenticationCoordinatorProtoc
func continueSSOLogin(withToken loginToken: String, transactionID: String) -> Bool {
authenticationViewController.continueSSOLogin(withToken: loginToken, txnId: transactionID)
}

// MARK: - Private

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)
}

private func presentCompleteSecurity(with session: MXSession) {
let isNewSignIn = true
let keyVerificationCoordinator = KeyVerificationCoordinator(session: session, flow: .completeSecurity(isNewSignIn))

keyVerificationCoordinator.delegate = self
let presentable = keyVerificationCoordinator.toPresentable()
presentable.presentationController?.delegate = self
navigationRouter.present(presentable, animated: true)
keyVerificationCoordinator.start()
add(childCoordinator: keyVerificationCoordinator)
}

private func authenticationDidComplete() {
completion?(.didComplete(authenticationViewController.authType))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this is called from a lot of places, even on failures. Is that expected?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes in as much as this is how AuthenticationViewController previously worked (calling dismiss).

The calls from sessionStateDidChange are all once the session is running and the failures are only crypto/cross-signing related so should all be handled from the home tab once the onboarding has completed. There is probably scope for improvement once the auth flow is re-written though.

}

private func registerSessionStateChangeNotification(for session: MXSession) {
NotificationCenter.default.addObserver(self, selector: #selector(sessionStateDidChange), name: .mxSessionStateDidChange, object: session)
}

private func unregisterSessionStateChangeNotification() {
NotificationCenter.default.removeObserver(self, name: .mxSessionStateDidChange, object: nil)
}

@objc private func sessionStateDidChange(_ notification: Notification) {
guard let session = notification.object as? MXSession else {
MXLog.error("[AuthenticationCoordinator] sessionStateDidChange: Missing session in the notification")
return
}

if session.state == .storeDataReady {
if let crypto = session.crypto, crypto.crossSigning != nil {
// Do not make key share requests while the "Complete security" is not complete.
// If the device is self-verified, the SDK will restore the existing key backup.
// Then, it will re-enable outgoing key share requests
crypto.setOutgoingKeyRequestsEnabled(false, onComplete: nil)
}
} else if session.state == .running {
unregisterSessionStateChangeNotification()

if let crypto = session.crypto, let crossSigning = crypto.crossSigning {
crossSigning.refreshState { [weak self] stateUpdated in
guard let self = self else { return }

MXLog.debug("[AuthenticationCoordinator] sessionStateDidChange: crossSigning.state: \(crossSigning.state)")

switch crossSigning.state {
case .notBootstrapped:
// TODO: This is still not sure we want to disable the automatic cross-signing bootstrap
// if the admin disabled e2e by default.
// Do like riot-web for the moment
if session.vc_homeserverConfiguration().isE2EEByDefaultEnabled {
// Bootstrap cross-signing on user's account
// We do it for both registration and new login as long as cross-signing does not exist yet
if let password = self.password, !password.isEmpty {
MXLog.debug("[AuthenticationCoordinator] sessionStateDidChange: Bootstrap with password")

crossSigning.setup(withPassword: password) {
MXLog.debug("[AuthenticationCoordinator] sessionStateDidChange: Bootstrap succeeded")
self.authenticationDidComplete()
} failure: { error in
MXLog.error("[AuthenticationCoordinator] sessionStateDidChange: Bootstrap failed. Error: \(error)")
crypto.setOutgoingKeyRequestsEnabled(true, onComplete: nil)
self.authenticationDidComplete()
}
} else {
// Try to setup cross-signing without authentication parameters in case if a grace period is enabled
self.crossSigningService.setupCrossSigningWithoutAuthentication(for: session) {
MXLog.debug("[AuthenticationCoordinator] sessionStateDidChange: Bootstrap succeeded without credentials")
self.authenticationDidComplete()
} failure: { error in
MXLog.error("[AuthenticationCoordinator] sessionStateDidChange: Do not know how to bootstrap cross-signing. Skip it.")
crypto.setOutgoingKeyRequestsEnabled(true, onComplete: nil)
self.authenticationDidComplete()
}
}
} else {
crypto.setOutgoingKeyRequestsEnabled(true, onComplete: nil)
self.authenticationDidComplete()
}
case .crossSigningExists:
MXLog.debug("[AuthenticationCoordinator] sessionStateDidChange: Complete security")
self.presentCompleteSecurity(with: session)
default:
MXLog.debug("[AuthenticationCoordinator] sessionStateDidChange: Nothing to do")

crypto.setOutgoingKeyRequestsEnabled(true, onComplete: nil)
self.authenticationDidComplete()
}
} failure: { [weak self] error in
MXLog.error("[AuthenticationCoordinator] sessionStateDidChange: Fail to refresh crypto state with error: \(error)")
crypto.setOutgoingKeyRequestsEnabled(true, onComplete: nil)
self?.authenticationDidComplete()
}
} else {
authenticationDidComplete()
}
}
}
}

// MARK: - AuthenticationViewControllerDelegate
extension AuthenticationCoordinator: AuthenticationViewControllerDelegate {
func authenticationViewControllerDidDismiss(_ authenticationViewController: AuthenticationViewController!) {
completion?(authenticationViewController.authType)
func authenticationViewController(_ authenticationViewController: AuthenticationViewController!, didLoginWith session: MXSession!, andPassword password: String!) {
registerSessionStateChangeNotification(for: session)

self.session = session
self.password = password

self.showLoadingAnimation()
completion?(.didLogin(session))
}
}

// MARK: - KeyVerificationCoordinatorDelegate
extension AuthenticationCoordinator: KeyVerificationCoordinatorDelegate {
func keyVerificationCoordinatorDidComplete(_ coordinator: KeyVerificationCoordinatorType, otherUserId: String, otherDeviceId: String) {
if let crypto = session?.crypto,
!crypto.backup.hasPrivateKeyInCryptoStore || !crypto.backup.enabled {
MXLog.debug("[AuthenticationCoordinator][MXKeyVerification] requestAllPrivateKeys: Request key backup private keys")
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
extension AuthenticationCoordinator: UIAdaptivePresentationControllerDelegate {
func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool {
// Prevent Key Verification from using swipe to dismiss
return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,21 @@

import Foundation

struct AuthenticationCoordinatorParameters {
let navigationRouter: NavigationRouterType
}

enum AuthenticationCoordinatorResult {
/// The user has authenticated but key verification is yet to happen. The session value is
/// for a fresh session that still needs to load, sync etc before being ready.
case didLogin(MXSession)
/// All of the required authentication steps including key verification is complete.
case didComplete(MXKAuthenticationType)
}

/// `AuthenticationCoordinatorProtocol` is a protocol describing a Coordinator that handle's the authentication navigation flow.
protocol AuthenticationCoordinatorProtocol: Coordinator, Presentable {
var completion: ((MXKAuthenticationType) -> Void)? { get set }
var completion: ((AuthenticationCoordinatorResult) -> Void)? { get set }

/// Update the screen to display registration or login.
func update(authenticationType: MXKAuthenticationType)
Expand Down
4 changes: 3 additions & 1 deletion Riot/Modules/Authentication/AuthenticationViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@

@protocol AuthenticationViewControllerDelegate <NSObject>

- (void)authenticationViewControllerDidDismiss:(AuthenticationViewController *)authenticationViewController;
- (void)authenticationViewController:(AuthenticationViewController *)authenticationViewController
didLoginWithSession:(MXSession *)session
andPassword:(NSString *)password;
Comment on lines +65 to +67
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like I should indicate the parameter nullability for Swift.


@end;
Loading