From b4cedb81644e04d73f83e297598cc684ae475e22 Mon Sep 17 00:00:00 2001 From: Doug Date: Fri, 19 Jul 2024 16:55:17 +0100 Subject: [PATCH] Automatically sign out when toggling the SSS feature flag. --- ElementX.xcodeproj/project.pbxproj | 2 +- .../xcshareddata/swiftpm/Package.resolved | 4 ++-- ElementX/Sources/Application/AppSettings.swift | 1 - .../SettingsFlowCoordinator.swift | 2 ++ .../Other/Extensions/ClientBuilder.swift | 18 ++++++++++++++++-- .../DeveloperOptionsScreenCoordinator.swift | 4 ++++ .../DeveloperOptionsScreenModels.swift | 2 ++ .../DeveloperOptionsScreenViewModel.swift | 10 ++++++++++ .../View/DeveloperOptionsScreen.swift | 2 +- .../Authentication/AuthenticationService.swift | 3 +-- .../Services/QRCode/QRCodeLoginService.swift | 3 +-- .../UserSession/UserSessionStore.swift | 2 +- NSE/Sources/Other/NSEUserSession.swift | 2 +- project.yml | 2 +- 14 files changed, 43 insertions(+), 14 deletions(-) diff --git a/ElementX.xcodeproj/project.pbxproj b/ElementX.xcodeproj/project.pbxproj index 1a170677a9..ec50a914c3 100644 --- a/ElementX.xcodeproj/project.pbxproj +++ b/ElementX.xcodeproj/project.pbxproj @@ -7501,7 +7501,7 @@ repositoryURL = "https://github.com/element-hq/matrix-rust-components-swift"; requirement = { kind = exactVersion; - version = 1.0.27; + version = 1.0.28; }; }; 701C7BEF8F70F7A83E852DCC /* XCRemoteSwiftPackageReference "GZIP" */ = { diff --git a/ElementX.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/ElementX.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index c506ff5bf3..cbd4b97795 100644 --- a/ElementX.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/ElementX.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -149,8 +149,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/element-hq/matrix-rust-components-swift", "state" : { - "revision" : "3a1f56a8dc2b14c93e562ece82fbf780d2f79704", - "version" : "1.0.27" + "revision" : "1a1cbc9d9d43a188d9b07fe00a141d02f7c43c7c", + "version" : "1.0.28" } }, { diff --git a/ElementX/Sources/Application/AppSettings.swift b/ElementX/Sources/Application/AppSettings.swift index 62d3495afc..2ac15cdd76 100644 --- a/ElementX/Sources/Application/AppSettings.swift +++ b/ElementX/Sources/Application/AppSettings.swift @@ -64,7 +64,6 @@ final class AppSettings { static func resetSessionSpecificSettings() { MXLog.warning("Resetting the user session specific AppSettings.") store.removeObject(forKey: UserDefaultsKeys.hasRunIdentityConfirmationOnboarding.rawValue) - store.removeObject(forKey: UserDefaultsKeys.simplifiedSlidingSyncEnabled.rawValue) } static func configureWithSuiteName(_ name: String) { diff --git a/ElementX/Sources/FlowCoordinators/SettingsFlowCoordinator.swift b/ElementX/Sources/FlowCoordinators/SettingsFlowCoordinator.swift index 3caa84fa82..1d8c0a2954 100644 --- a/ElementX/Sources/FlowCoordinators/SettingsFlowCoordinator.swift +++ b/ElementX/Sources/FlowCoordinators/SettingsFlowCoordinator.swift @@ -233,6 +233,8 @@ class SettingsFlowCoordinator: FlowCoordinatorProtocol { switch action { case .clearCache: actionsSubject.send(.clearCache) + case .forceLogout: + actionsSubject.send(.forceLogout) } } .store(in: &cancellables) diff --git a/ElementX/Sources/Other/Extensions/ClientBuilder.swift b/ElementX/Sources/Other/Extensions/ClientBuilder.swift index 4106061919..635f9acb8e 100644 --- a/ElementX/Sources/Other/Extensions/ClientBuilder.swift +++ b/ElementX/Sources/Other/Extensions/ClientBuilder.swift @@ -21,15 +21,20 @@ extension ClientBuilder { /// A helper method that applies the common builder modifiers needed for the app. static func baseBuilder(setupEncryption: Bool = true, httpProxy: String? = nil, + slidingSync: ClientBuilderSlidingSync, slidingSyncProxy: URL? = nil, sessionDelegate: ClientSessionDelegate, - simplifiedSlidingSyncEnabled: Bool, appHooks: AppHooks) -> ClientBuilder { var builder = ClientBuilder() .slidingSyncProxy(slidingSyncProxy: slidingSyncProxy?.absoluteString) .enableCrossProcessRefreshLock(processId: InfoPlistReader.main.bundleIdentifier, sessionDelegate: sessionDelegate) .userAgent(userAgent: UserAgentBuilder.makeASCIIUserAgent()) - .simplifiedSlidingSync(enable: simplifiedSlidingSyncEnabled) + + builder = switch slidingSync { + case .restored: builder + case .discovered: builder.requiresSlidingSync() + case .simplified: builder.simplifiedSlidingSync(enable: true) + } if setupEncryption { builder = builder @@ -45,3 +50,12 @@ extension ClientBuilder { return appHooks.clientBuilderHook.configure(builder) } } + +enum ClientBuilderSlidingSync { + /// The proxy will be supplied when restoring the Session. + case restored + /// A proxy must be discovered whilst building the session. + case discovered + /// Use Simplified Sliding Sync (discovery isn't a thing yet). + case simplified +} diff --git a/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/DeveloperOptionsScreenCoordinator.swift b/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/DeveloperOptionsScreenCoordinator.swift index 1b4d9061c8..57df849963 100644 --- a/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/DeveloperOptionsScreenCoordinator.swift +++ b/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/DeveloperOptionsScreenCoordinator.swift @@ -19,6 +19,8 @@ import SwiftUI enum DeveloperOptionsScreenCoordinatorAction { case clearCache + /// Logout without a confirmation to avoid losing keys when trying SSS. + case forceLogout } final class DeveloperOptionsScreenCoordinator: CoordinatorProtocol { @@ -42,6 +44,8 @@ final class DeveloperOptionsScreenCoordinator: CoordinatorProtocol { switch action { case .clearCache: actionsSubject.send(.clearCache) + case .forceLogout: + actionsSubject.send(.forceLogout) } } .store(in: &cancellables) diff --git a/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/DeveloperOptionsScreenModels.swift b/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/DeveloperOptionsScreenModels.swift index 8a881b30b4..ba618ad297 100644 --- a/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/DeveloperOptionsScreenModels.swift +++ b/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/DeveloperOptionsScreenModels.swift @@ -18,6 +18,8 @@ import Foundation enum DeveloperOptionsScreenViewModelAction { case clearCache + /// Logout without a confirmation to avoid losing keys when trying SSS. + case forceLogout } struct DeveloperOptionsScreenViewState: BindableState { diff --git a/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/DeveloperOptionsScreenViewModel.swift b/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/DeveloperOptionsScreenViewModel.swift index 7d218a5308..e6450adc3c 100644 --- a/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/DeveloperOptionsScreenViewModel.swift +++ b/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/DeveloperOptionsScreenViewModel.swift @@ -31,6 +31,16 @@ class DeveloperOptionsScreenViewModel: DeveloperOptionsScreenViewModelType, Deve let state = DeveloperOptionsScreenViewState(elementCallBaseURL: elementCallBaseURL, bindings: bindings) super.init(initialViewState: state) + + context.$viewState + .map(\.bindings.simplifiedSlidingSyncEnabled) + .removeDuplicates() + .dropFirst() // Ignore the initial value received when opening the screen. + .sink { [weak self] isEnabled in + MXLog.error("Toggled simplifiedSlidingSyncEnabled: \(isEnabled). Signing out.") + self?.actionsSubject.send(.forceLogout) + } + .store(in: &cancellables) } override func process(viewAction: DeveloperOptionsScreenViewAction) { diff --git a/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/View/DeveloperOptionsScreen.swift b/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/View/DeveloperOptionsScreen.swift index 0147a8559c..6462998924 100644 --- a/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/View/DeveloperOptionsScreen.swift +++ b/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/View/DeveloperOptionsScreen.swift @@ -30,7 +30,7 @@ struct DeveloperOptionsScreen: View { Section("Sliding Sync") { Toggle(isOn: $context.simplifiedSlidingSyncEnabled) { Text("Simplified Sliding Sync") - Text("Requires app reboot") + Text("When toggled you'll be logged out of the app and will need to log in again.") } } diff --git a/ElementX/Sources/Services/Authentication/AuthenticationService.swift b/ElementX/Sources/Services/Authentication/AuthenticationService.swift index 2f08ac2fbb..21efe55ac8 100644 --- a/ElementX/Sources/Services/Authentication/AuthenticationService.swift +++ b/ElementX/Sources/Services/Authentication/AuthenticationService.swift @@ -141,13 +141,12 @@ class AuthenticationService: AuthenticationServiceProtocol { private func makeClientBuilder() -> ClientBuilder { ClientBuilder .baseBuilder(httpProxy: appSettings.websiteURL.globalProxy, + slidingSync: appSettings.simplifiedSlidingSyncEnabled ? .simplified : .discovered, slidingSyncProxy: appSettings.slidingSyncProxyURL, sessionDelegate: userSessionStore.clientSessionDelegate, - simplifiedSlidingSyncEnabled: appSettings.simplifiedSlidingSyncEnabled, appHooks: appHooks) .sessionPath(path: sessionDirectory.path(percentEncoded: false)) .passphrase(passphrase: passphrase) - .requiresSlidingSync() } private func userSession(for client: Client) async -> Result { diff --git a/ElementX/Sources/Services/QRCode/QRCodeLoginService.swift b/ElementX/Sources/Services/QRCode/QRCodeLoginService.swift index 1c8fa629f8..20dd900286 100644 --- a/ElementX/Sources/Services/QRCode/QRCodeLoginService.swift +++ b/ElementX/Sources/Services/QRCode/QRCodeLoginService.swift @@ -59,13 +59,12 @@ final class QRCodeLoginService: QRCodeLoginServiceProtocol { do { let client = try await ClientBuilder .baseBuilder(httpProxy: appSettings.websiteURL.globalProxy, + slidingSync: appSettings.simplifiedSlidingSyncEnabled ? .simplified : .discovered, slidingSyncProxy: appSettings.slidingSyncProxyURL, sessionDelegate: userSessionStore.clientSessionDelegate, - simplifiedSlidingSyncEnabled: appSettings.simplifiedSlidingSyncEnabled, appHooks: appHooks) .sessionPath(path: sessionDirectory.path(percentEncoded: false)) .passphrase(passphrase: passphrase) - .requiresSlidingSync() .buildWithQrCode(qrCodeData: qrData, oidcConfiguration: appSettings.oidcConfiguration.rustValue, progressListener: listener) return await login(client: client) } catch let error as HumanQrLoginError { diff --git a/ElementX/Sources/Services/UserSession/UserSessionStore.swift b/ElementX/Sources/Services/UserSession/UserSessionStore.swift index 11cecf73df..bdfa4ef575 100644 --- a/ElementX/Sources/Services/UserSession/UserSessionStore.swift +++ b/ElementX/Sources/Services/UserSession/UserSessionStore.swift @@ -124,8 +124,8 @@ class UserSessionStore: UserSessionStoreProtocol { let builder = ClientBuilder .baseBuilder(httpProxy: URL(string: homeserverURL)?.globalProxy, + slidingSync: appSettings.simplifiedSlidingSyncEnabled ? .simplified : .restored, sessionDelegate: keychainController, - simplifiedSlidingSyncEnabled: appSettings.simplifiedSlidingSyncEnabled, appHooks: appHooks) .sessionPath(path: credentials.restorationToken.sessionDirectory.path(percentEncoded: false)) .username(username: credentials.userID) diff --git a/NSE/Sources/Other/NSEUserSession.swift b/NSE/Sources/Other/NSEUserSession.swift index 6f4ffdd2ba..a23f15a6c6 100644 --- a/NSE/Sources/Other/NSEUserSession.swift +++ b/NSE/Sources/Other/NSEUserSession.swift @@ -35,8 +35,8 @@ final class NSEUserSession { let clientBuilder = ClientBuilder .baseBuilder(setupEncryption: false, httpProxy: URL(string: homeserverURL)?.globalProxy, + slidingSync: simplifiedSlidingSyncEnabled ? .simplified : .restored, sessionDelegate: clientSessionDelegate, - simplifiedSlidingSyncEnabled: simplifiedSlidingSyncEnabled, appHooks: appHooks) .sessionPath(path: credentials.restorationToken.sessionDirectory.path(percentEncoded: false)) .username(username: credentials.userID) diff --git a/project.yml b/project.yml index f4a59d5664..c321817705 100644 --- a/project.yml +++ b/project.yml @@ -60,7 +60,7 @@ packages: # Element/Matrix dependencies MatrixRustSDK: url: https://github.com/element-hq/matrix-rust-components-swift - exactVersion: 1.0.27 + exactVersion: 1.0.28 # path: ../matrix-rust-sdk Compound: url: https://github.com/element-hq/compound-ios