Skip to content

Commit

Permalink
Adopt sliding sync discovery for authentication.
Browse files Browse the repository at this point in the history
  • Loading branch information
pixlwave committed Aug 29, 2024
1 parent e54d80b commit a11adfc
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 71 deletions.
4 changes: 4 additions & 0 deletions ElementX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@
89B909AC66B96FA054EF3C14 /* InvitedRoomProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E95B3BDB80531C85CD50AE6 /* InvitedRoomProxy.swift */; };
8A0BD60CA4A6004DB06B5403 /* MediaUploadingPreprocessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 669F35C505ACE1110589F875 /* MediaUploadingPreprocessor.swift */; };
8A5064CAC8E5F3B18645621D /* CallNotificationRoomTimelineView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD6E082B0507FB28F966516A /* CallNotificationRoomTimelineView.swift */; };
8A6CB15C8FC68F557750BF54 /* AuthenticationClientBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0F569CFB77E0D40BD82203D9 /* AuthenticationClientBuilder.swift */; };
8A83D715940378B9BA9F739E /* RoomInviterLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7EB58E4E8D6D634C246AD5C2 /* RoomInviterLabel.swift */; };
8AA84EF202F2EFC8453A97BD /* SecureBackupRecoveryKeyScreenModels.swift in Sources */ = {isa = PBXBuildFile; fileRef = 645E027C112740573D27765C /* SecureBackupRecoveryKeyScreenModels.swift */; };
8AB8ED1051216546CB35FA0E /* UserSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E5E9C044BEB7C70B1378E91 /* UserSession.swift */; };
Expand Down Expand Up @@ -1248,6 +1249,7 @@
0E95B3BDB80531C85CD50AE6 /* InvitedRoomProxy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InvitedRoomProxy.swift; sourceTree = "<group>"; };
0EE9EAF0309A2A1D67D8FAF5 /* sv */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; name = sv; path = sv.lproj/Localizable.stringsdict; sourceTree = "<group>"; };
0F5567A7EF6F2AB9473236F6 /* DocumentPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DocumentPicker.swift; sourceTree = "<group>"; };
0F569CFB77E0D40BD82203D9 /* AuthenticationClientBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthenticationClientBuilder.swift; sourceTree = "<group>"; };
0F64447FF544298A6A3BEF85 /* NotificationSettingsScreenModels.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationSettingsScreenModels.swift; sourceTree = "<group>"; };
0FA60F848D1C14F873F9621A /* RoomMemberDetailsScreenCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoomMemberDetailsScreenCoordinator.swift; sourceTree = "<group>"; };
105429F29096729EDD3152CF /* ru */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ru; path = ru.lproj/SAS.strings; sourceTree = "<group>"; };
Expand Down Expand Up @@ -4586,6 +4588,7 @@
AAFDD509929A0CCF8BCE51EB /* Authentication */ = {
isa = PBXGroup;
children = (
0F569CFB77E0D40BD82203D9 /* AuthenticationClientBuilder.swift */,
F3A1AB5A84D843B6AC8D5F1E /* AuthenticationService.swift */,
5E75948AA1FE1D1A7809931F /* AuthenticationServiceProtocol.swift */,
65C2B80DD0BF6F10BB5FA922 /* MockAuthenticationServiceProxy.swift */,
Expand Down Expand Up @@ -6181,6 +6184,7 @@
88F348E2CB14FF71CBBB665D /* AudioRoomTimelineItemContent.swift in Sources */,
7BD2123144A32F082CECC108 /* AudioRoomTimelineView.swift in Sources */,
9278EC51D24E57445B290521 /* AudioSessionProtocol.swift in Sources */,
8A6CB15C8FC68F557750BF54 /* AuthenticationClientBuilder.swift in Sources */,
67E9926C4572C54F59FCA91A /* AuthenticationFlowCoordinator.swift in Sources */,
9847B056C1A216C314D21E68 /* AuthenticationService.swift in Sources */,
56DACDD379A86A1F5DEFE7BE /* AuthenticationServiceProtocol.swift in Sources */,
Expand Down
15 changes: 5 additions & 10 deletions ElementX/Sources/Application/AppSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class AppSettings {
case elementCallEncryptionEnabled

// Feature flags
case simplifiedSlidingSyncEnabled
case slidingSyncDiscovery
case publicSearchEnabled
case fuzzyRoomListSearchEnabled
case pinningEnabled
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -284,16 +280,15 @@ 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

// MARK: - Shared

@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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -241,8 +241,6 @@ class SettingsFlowCoordinator: FlowCoordinatorProtocol {
switch action {
case .clearCache:
actionsSubject.send(.clearCache)
case .forceLogout:
actionsSubject.send(.forceLogout)
}
}
.store(in: &cancellables)
Expand Down
15 changes: 8 additions & 7 deletions ElementX/Sources/Other/Extensions/ClientBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -44,8 +43,6 @@ 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,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
Expand All @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading

0 comments on commit a11adfc

Please sign in to comment.