mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 21:33:26 +00:00
Adopt sliding sync discovery for authentication.
This commit is contained in:
@@ -43,7 +43,7 @@ final class AppSettings {
|
||||
case elementCallEncryptionEnabled
|
||||
|
||||
// Feature flags
|
||||
case simplifiedSlidingSyncEnabled
|
||||
case slidingSyncDiscovery
|
||||
case publicSearchEnabled
|
||||
case fuzzyRoomListSearchEnabled
|
||||
case pinningEnabled
|
||||
@@ -106,10 +106,6 @@ final class AppSettings {
|
||||
/// so that it can be passed to Rust as a ServerName for well-known discovery.
|
||||
private(set) var defaultHomeserverAddress = "matrix.org"
|
||||
|
||||
/// An override of the homeserver's Sliding Sync proxy URL. This allows development against servers
|
||||
/// that don't yet have an officially trusted proxy configured in their well-known.
|
||||
let slidingSyncProxyURL: URL? = nil
|
||||
|
||||
/// The task identifier used for background app refresh. Also used in main target's the Info.plist
|
||||
let backgroundAppRefreshTaskIdentifier = "io.element.elementx.background.refresh"
|
||||
|
||||
@@ -284,6 +280,10 @@ final class AppSettings {
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.pinningEnabled, defaultValue: false, storageType: .userDefaults(store))
|
||||
var pinningEnabled
|
||||
|
||||
enum SlidingSyncDiscovery: Codable { case proxy, native, forceNative }
|
||||
@UserPreference(key: UserDefaultsKeys.slidingSyncDiscovery, defaultValue: .proxy, storageType: .userDefaults(store))
|
||||
var slidingSyncDiscovery: SlidingSyncDiscovery
|
||||
|
||||
#endif
|
||||
|
||||
@@ -291,9 +291,4 @@ final class AppSettings {
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.logLevel, defaultValue: TracingConfiguration.LogLevel.info, storageType: .userDefaults(store))
|
||||
var logLevel
|
||||
|
||||
// MARK: Shared Feature Flags
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.simplifiedSlidingSyncEnabled, defaultValue: false, storageType: .userDefaults(store))
|
||||
var simplifiedSlidingSyncEnabled
|
||||
}
|
||||
|
||||
@@ -232,7 +232,7 @@ class SettingsFlowCoordinator: FlowCoordinatorProtocol {
|
||||
}
|
||||
|
||||
private func presentDeveloperOptions() {
|
||||
let coordinator = DeveloperOptionsScreenCoordinator()
|
||||
let coordinator = DeveloperOptionsScreenCoordinator(isUsingNativeSlidingSync: parameters.userSession.clientProxy.slidingSyncVersion == .native)
|
||||
|
||||
coordinator.actions
|
||||
.sink { [weak self] action in
|
||||
@@ -241,8 +241,6 @@ class SettingsFlowCoordinator: FlowCoordinatorProtocol {
|
||||
switch action {
|
||||
case .clearCache:
|
||||
actionsSubject.send(.clearCache)
|
||||
case .forceLogout:
|
||||
actionsSubject.send(.forceLogout)
|
||||
}
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
@@ -22,19 +22,18 @@ extension ClientBuilder {
|
||||
static func baseBuilder(setupEncryption: Bool = true,
|
||||
httpProxy: String? = nil,
|
||||
slidingSync: ClientBuilderSlidingSync,
|
||||
slidingSyncProxy: URL? = nil,
|
||||
sessionDelegate: ClientSessionDelegate,
|
||||
appHooks: AppHooks) -> ClientBuilder {
|
||||
var builder = ClientBuilder()
|
||||
.slidingSyncProxy(slidingSyncProxy: slidingSyncProxy?.absoluteString)
|
||||
.enableCrossProcessRefreshLock(processId: InfoPlistReader.main.bundleIdentifier, sessionDelegate: sessionDelegate)
|
||||
.userAgent(userAgent: UserAgentBuilder.makeASCIIUserAgent())
|
||||
.requestConfig(config: .init(retryLimit: 0, timeout: 30000, maxConcurrentRequests: nil, retryTimeout: nil))
|
||||
|
||||
builder = switch slidingSync {
|
||||
case .restored: builder
|
||||
case .discovered: builder.requiresSlidingSync()
|
||||
case .simplified: builder.simplifiedSlidingSync(enable: true)
|
||||
case .discoverProxy: builder.slidingSyncVersionBuilder(versionBuilder: .discoverProxy)
|
||||
case .discoverNative: builder.slidingSyncVersionBuilder(versionBuilder: .discoverNative)
|
||||
case .forceNative: builder.slidingSyncVersionBuilder(versionBuilder: .native)
|
||||
}
|
||||
|
||||
if setupEncryption {
|
||||
@@ -56,7 +55,9 @@ 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
|
||||
case discoverProxy
|
||||
/// Native sliding sync must be discovered whilst building the session.
|
||||
case discoverNative
|
||||
/// Forces native sliding sync without discovering it.
|
||||
case forceNative
|
||||
}
|
||||
|
||||
+3
-6
@@ -19,8 +19,6 @@ import SwiftUI
|
||||
|
||||
enum DeveloperOptionsScreenCoordinatorAction {
|
||||
case clearCache
|
||||
/// Logout without a confirmation to avoid losing keys when trying SSS.
|
||||
case forceLogout
|
||||
}
|
||||
|
||||
final class DeveloperOptionsScreenCoordinator: CoordinatorProtocol {
|
||||
@@ -33,9 +31,10 @@ final class DeveloperOptionsScreenCoordinator: CoordinatorProtocol {
|
||||
actionsSubject.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
init() {
|
||||
init(isUsingNativeSlidingSync: Bool) {
|
||||
viewModel = DeveloperOptionsScreenViewModel(developerOptions: ServiceLocator.shared.settings,
|
||||
elementCallBaseURL: ServiceLocator.shared.settings.elementCallBaseURL)
|
||||
elementCallBaseURL: ServiceLocator.shared.settings.elementCallBaseURL,
|
||||
isUsingNativeSlidingSync: isUsingNativeSlidingSync)
|
||||
|
||||
viewModel.actions
|
||||
.sink { [weak self] action in
|
||||
@@ -44,8 +43,6 @@ final class DeveloperOptionsScreenCoordinator: CoordinatorProtocol {
|
||||
switch action {
|
||||
case .clearCache:
|
||||
actionsSubject.send(.clearCache)
|
||||
case .forceLogout:
|
||||
actionsSubject.send(.forceLogout)
|
||||
}
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
+6
-3
@@ -18,13 +18,16 @@ import Foundation
|
||||
|
||||
enum DeveloperOptionsScreenViewModelAction {
|
||||
case clearCache
|
||||
/// Logout without a confirmation to avoid losing keys when trying SSS.
|
||||
case forceLogout
|
||||
}
|
||||
|
||||
struct DeveloperOptionsScreenViewState: BindableState {
|
||||
let elementCallBaseURL: URL
|
||||
let isUsingNativeSlidingSync: Bool
|
||||
var bindings: DeveloperOptionsScreenViewStateBindings
|
||||
|
||||
var slidingSyncFooter: String {
|
||||
"The method used to configure sliding sync when signing in. Changing this setting has no effect until you sign out.\n\nYour current session is using \(isUsingNativeSlidingSync ? "native sliding sync." : "a sliding sync proxy.")"
|
||||
}
|
||||
}
|
||||
|
||||
// periphery: ignore - subscripts are seen as false positive
|
||||
@@ -48,7 +51,7 @@ enum DeveloperOptionsScreenViewAction {
|
||||
|
||||
protocol DeveloperOptionsProtocol: AnyObject {
|
||||
var logLevel: TracingConfiguration.LogLevel { get set }
|
||||
var simplifiedSlidingSyncEnabled: Bool { get set }
|
||||
var slidingSyncDiscovery: AppSettings.SlidingSyncDiscovery { get set }
|
||||
var hideUnreadMessagesBadge: Bool { get set }
|
||||
var elementCallBaseURLOverride: URL? { get set }
|
||||
var fuzzyRoomListSearchEnabled: Bool { get set }
|
||||
|
||||
+2
-12
@@ -26,21 +26,11 @@ class DeveloperOptionsScreenViewModel: DeveloperOptionsScreenViewModelType, Deve
|
||||
actionsSubject.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
init(developerOptions: DeveloperOptionsProtocol, elementCallBaseURL: URL) {
|
||||
init(developerOptions: DeveloperOptionsProtocol, elementCallBaseURL: URL, isUsingNativeSlidingSync: Bool) {
|
||||
let bindings = DeveloperOptionsScreenViewStateBindings(developerOptions: developerOptions)
|
||||
let state = DeveloperOptionsScreenViewState(elementCallBaseURL: elementCallBaseURL, bindings: bindings)
|
||||
let state = DeveloperOptionsScreenViewState(elementCallBaseURL: elementCallBaseURL, isUsingNativeSlidingSync: isUsingNativeSlidingSync, 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) {
|
||||
|
||||
+11
-5
@@ -32,11 +32,16 @@ struct DeveloperOptionsScreen: View {
|
||||
LogLevelConfigurationView(logLevel: $context.logLevel)
|
||||
}
|
||||
|
||||
Section("Sliding Sync") {
|
||||
Toggle(isOn: $context.simplifiedSlidingSyncEnabled) {
|
||||
Text("Simplified Sliding Sync")
|
||||
Text("When toggled you'll be logged out of the app and will need to log in again.")
|
||||
Section {
|
||||
Picker("Discovery", selection: $context.slidingSyncDiscovery) {
|
||||
Text("Proxy only").tag(AppSettings.SlidingSyncDiscovery.proxy)
|
||||
Text("Automatic").tag(AppSettings.SlidingSyncDiscovery.native)
|
||||
Text("Force Native ⚠️").tag(AppSettings.SlidingSyncDiscovery.forceNative)
|
||||
}
|
||||
} header: {
|
||||
Text("Sliding Sync")
|
||||
} footer: {
|
||||
Text(context.viewState.slidingSyncFooter)
|
||||
}
|
||||
|
||||
Section("Message Pinning") {
|
||||
@@ -174,7 +179,8 @@ private struct LogLevelConfigurationView: View {
|
||||
|
||||
struct DeveloperOptionsScreen_Previews: PreviewProvider {
|
||||
static let viewModel = DeveloperOptionsScreenViewModel(developerOptions: ServiceLocator.shared.settings,
|
||||
elementCallBaseURL: ServiceLocator.shared.settings.elementCallBaseURL)
|
||||
elementCallBaseURL: ServiceLocator.shared.settings.elementCallBaseURL,
|
||||
isUsingNativeSlidingSync: true)
|
||||
static var previews: some View {
|
||||
NavigationStack {
|
||||
DeveloperOptionsScreen(context: viewModel.context)
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
//
|
||||
// Copyright 2024 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 Foundation
|
||||
import MatrixRustSDK
|
||||
|
||||
/// A wrapper around `ClientBuilder` to share reusable code between Normal and QR logins.
|
||||
struct AuthenticationClientBuilder {
|
||||
let sessionDirectories: SessionDirectories
|
||||
let passphrase: String
|
||||
let clientSessionDelegate: ClientSessionDelegate
|
||||
|
||||
let appSettings: AppSettings
|
||||
let appHooks: AppHooks
|
||||
|
||||
/// Builds a Client for login using OIDC or password authentication.
|
||||
func build(homeserverAddress: String) async throws -> Client {
|
||||
if appSettings.slidingSyncDiscovery == .forceNative {
|
||||
return try await makeClientBuilder(slidingSync: .forceNative).serverNameOrHomeserverUrl(serverNameOrUrl: homeserverAddress).build()
|
||||
}
|
||||
|
||||
if appSettings.slidingSyncDiscovery == .native {
|
||||
do {
|
||||
return try await makeClientBuilder(slidingSync: .discoverNative).serverNameOrHomeserverUrl(serverNameOrUrl: homeserverAddress).build()
|
||||
} catch {
|
||||
MXLog.warning("Native sliding sync not available: \(error)")
|
||||
MXLog.info("Falling back to a sliding sync proxy.")
|
||||
}
|
||||
}
|
||||
|
||||
return try await makeClientBuilder(slidingSync: .discoverProxy).serverNameOrHomeserverUrl(serverNameOrUrl: homeserverAddress).build()
|
||||
}
|
||||
|
||||
/// Builds a Client, authenticating with the given QR code data.
|
||||
func buildWithQRCode(qrCodeData: QrCodeData,
|
||||
oidcConfiguration: OIDCConfigurationProxy,
|
||||
progressListener: QrLoginProgressListenerProxy) async throws -> Client {
|
||||
if appSettings.slidingSyncDiscovery == .forceNative {
|
||||
return try await makeClientBuilder(slidingSync: .forceNative).buildWithQrCode(qrCodeData: qrCodeData,
|
||||
oidcConfiguration: oidcConfiguration.rustValue,
|
||||
progressListener: progressListener)
|
||||
}
|
||||
|
||||
if appSettings.slidingSyncDiscovery == .native {
|
||||
do {
|
||||
return try await makeClientBuilder(slidingSync: .discoverNative).buildWithQrCode(qrCodeData: qrCodeData,
|
||||
oidcConfiguration: oidcConfiguration.rustValue,
|
||||
progressListener: progressListener)
|
||||
} catch HumanQrLoginError.SlidingSyncNotAvailable {
|
||||
MXLog.warning("Native sliding sync not available")
|
||||
MXLog.info("Falling back to a sliding sync proxy.")
|
||||
} catch {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
return try await makeClientBuilder(slidingSync: .discoverProxy).buildWithQrCode(qrCodeData: qrCodeData,
|
||||
oidcConfiguration: oidcConfiguration.rustValue,
|
||||
progressListener: progressListener)
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
/// The base builder configuration used for authentication within the app.
|
||||
private func makeClientBuilder(slidingSync: ClientBuilderSlidingSync) -> ClientBuilder {
|
||||
ClientBuilder
|
||||
.baseBuilder(httpProxy: appSettings.websiteURL.globalProxy,
|
||||
slidingSync: slidingSync,
|
||||
sessionDelegate: clientSessionDelegate,
|
||||
appHooks: appHooks)
|
||||
.sessionPaths(dataPath: sessionDirectories.dataPath,
|
||||
cachePath: sessionDirectories.cachePath)
|
||||
.passphrase(passphrase: passphrase)
|
||||
}
|
||||
}
|
||||
@@ -47,9 +47,11 @@ class AuthenticationService: AuthenticationServiceProtocol {
|
||||
do {
|
||||
var homeserver = LoginHomeserver(address: homeserverAddress, loginMode: .unknown)
|
||||
|
||||
let client = try await makeClientBuilder().serverNameOrHomeserverUrl(serverNameOrUrl: homeserverAddress).build()
|
||||
let client = try await makeClientBuilder().build(homeserverAddress: homeserverAddress)
|
||||
let loginDetails = await client.homeserverLoginDetails()
|
||||
|
||||
MXLog.info("Sliding sync: \(client.slidingSyncVersion())")
|
||||
|
||||
if loginDetails.supportsOidcLogin() {
|
||||
homeserver.loginMode = .oidc
|
||||
} else if loginDetails.supportsPasswordLogin() {
|
||||
@@ -64,8 +66,8 @@ class AuthenticationService: AuthenticationServiceProtocol {
|
||||
} catch ClientBuildError.WellKnownDeserializationError(let error) {
|
||||
MXLog.error("The user entered a server with an invalid well-known file: \(error)")
|
||||
return .failure(.invalidWellKnown(error))
|
||||
} catch ClientBuildError.SlidingSyncNotAvailable {
|
||||
MXLog.info("User entered a homeserver that isn't configured for sliding sync.")
|
||||
} catch ClientBuildError.SlidingSyncVersion(let error) {
|
||||
MXLog.info("User entered a homeserver that isn't configured for sliding sync: \(error)")
|
||||
return .failure(.slidingSyncNotAvailable)
|
||||
} catch {
|
||||
MXLog.error("Failed configuring a server: \(error)")
|
||||
@@ -138,20 +140,16 @@ class AuthenticationService: AuthenticationServiceProtocol {
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private func makeClientBuilder() -> ClientBuilder {
|
||||
private func makeClientBuilder() -> AuthenticationClientBuilder {
|
||||
// Use a fresh session directory each time the user enters a different server
|
||||
// so that caches (e.g. server versions) are always fresh for the new server.
|
||||
rotateSessionDirectory()
|
||||
|
||||
return ClientBuilder
|
||||
.baseBuilder(httpProxy: appSettings.websiteURL.globalProxy,
|
||||
slidingSync: appSettings.simplifiedSlidingSyncEnabled ? .simplified : .discovered,
|
||||
slidingSyncProxy: appSettings.slidingSyncProxyURL,
|
||||
sessionDelegate: userSessionStore.clientSessionDelegate,
|
||||
appHooks: appHooks)
|
||||
.sessionPaths(dataPath: sessionDirectories.dataPath,
|
||||
cachePath: sessionDirectories.cachePath)
|
||||
.passphrase(passphrase: passphrase)
|
||||
return AuthenticationClientBuilder(sessionDirectories: sessionDirectories,
|
||||
passphrase: passphrase,
|
||||
clientSessionDelegate: userSessionStore.clientSessionDelegate,
|
||||
appSettings: appSettings,
|
||||
appHooks: appHooks)
|
||||
}
|
||||
|
||||
private func rotateSessionDirectory() {
|
||||
|
||||
@@ -57,9 +57,10 @@ final class QRCodeLoginService: QRCodeLoginServiceProtocol {
|
||||
}
|
||||
|
||||
do {
|
||||
let client = try await makeClientBuilder().buildWithQrCode(qrCodeData: qrData,
|
||||
oidcConfiguration: appSettings.oidcConfiguration.rustValue,
|
||||
let client = try await makeClientBuilder().buildWithQRCode(qrCodeData: qrData,
|
||||
oidcConfiguration: appSettings.oidcConfiguration,
|
||||
progressListener: listener)
|
||||
MXLog.info("Sliding sync: \(client.slidingSyncVersion())")
|
||||
return await userSession(for: client)
|
||||
} catch let error as HumanQrLoginError {
|
||||
MXLog.error("QRCode login error: \(error)")
|
||||
@@ -72,20 +73,16 @@ final class QRCodeLoginService: QRCodeLoginServiceProtocol {
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private func makeClientBuilder() -> ClientBuilder {
|
||||
private func makeClientBuilder() -> AuthenticationClientBuilder {
|
||||
// Use a fresh session directory each time the user scans a QR code to ensure caches
|
||||
// (e.g. server versions) are always fresh in case a different server is used.
|
||||
rotateSessionDirectory()
|
||||
|
||||
return ClientBuilder
|
||||
.baseBuilder(httpProxy: appSettings.websiteURL.globalProxy,
|
||||
slidingSync: appSettings.simplifiedSlidingSyncEnabled ? .simplified : .discovered,
|
||||
slidingSyncProxy: appSettings.slidingSyncProxyURL,
|
||||
sessionDelegate: userSessionStore.clientSessionDelegate,
|
||||
appHooks: appHooks)
|
||||
.sessionPaths(dataPath: sessionDirectories.dataPath,
|
||||
cachePath: sessionDirectories.cachePath)
|
||||
.passphrase(passphrase: passphrase)
|
||||
return AuthenticationClientBuilder(sessionDirectories: sessionDirectories,
|
||||
passphrase: passphrase,
|
||||
clientSessionDelegate: userSessionStore.clientSessionDelegate,
|
||||
appSettings: appSettings,
|
||||
appHooks: appHooks)
|
||||
}
|
||||
|
||||
private func rotateSessionDirectory() {
|
||||
|
||||
Reference in New Issue
Block a user