Skip to content

Commit

Permalink
Automatically sign out when toggling the SSS feature flag. (#3071)
Browse files Browse the repository at this point in the history
  • Loading branch information
pixlwave authored Jul 23, 2024
1 parent 9879552 commit bf22250
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 14 deletions.
2 changes: 1 addition & 1 deletion ElementX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
{
Expand Down
1 change: 0 additions & 1 deletion ElementX/Sources/Application/AppSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ class SettingsFlowCoordinator: FlowCoordinatorProtocol {
switch action {
case .clearCache:
actionsSubject.send(.clearCache)
case .forceLogout:
actionsSubject.send(.forceLogout)
}
}
.store(in: &cancellables)
Expand Down
18 changes: 16 additions & 2 deletions ElementX/Sources/Other/Extensions/ClientBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -42,6 +44,8 @@ final class DeveloperOptionsScreenCoordinator: CoordinatorProtocol {
switch action {
case .clearCache:
actionsSubject.send(.clearCache)
case .forceLogout:
actionsSubject.send(.forceLogout)
}
}
.store(in: &cancellables)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserSessionProtocol, AuthenticationServiceError> {
Expand Down
3 changes: 1 addition & 2 deletions ElementX/Sources/Services/QRCode/QRCodeLoginService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion NSE/Sources/Other/NSEUserSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bf22250

Please sign in to comment.