2022-06-14 17:36:28 +01:00
|
|
|
//
|
2024-09-06 16:34:30 +03:00
|
|
|
// Copyright 2022-2024 New Vector Ltd.
|
2022-06-14 17:36:28 +01:00
|
|
|
//
|
2025-01-06 11:27:37 +01:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
|
|
// Please see LICENSE files in the repository root for full details.
|
2022-06-14 17:36:28 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Kingfisher
|
2022-07-06 14:49:05 +01:00
|
|
|
import MatrixRustSDK
|
2022-06-14 17:36:28 +01:00
|
|
|
|
2022-06-15 10:10:22 +01:00
|
|
|
class UserSessionStore: UserSessionStoreProtocol {
|
2022-06-14 17:36:28 +01:00
|
|
|
private let keychainController: KeychainControllerProtocol
|
2024-07-18 10:16:51 +01:00
|
|
|
private let appSettings: AppSettings
|
2024-08-08 18:29:39 +02:00
|
|
|
private let networkMonitor: NetworkMonitorProtocol
|
2024-07-18 09:47:37 +01:00
|
|
|
private let appHooks: AppHooks
|
2022-06-14 17:36:28 +01:00
|
|
|
|
|
|
|
/// Whether or not there are sessions in the store.
|
2022-11-04 13:24:53 +02:00
|
|
|
var hasSessions: Bool { !keychainController.restorationTokens().isEmpty }
|
2023-06-30 13:27:49 +01:00
|
|
|
/// All the user IDs managed by the store.
|
|
|
|
var userIDs: [String] { keychainController.restorationTokens().map(\.userID) }
|
2022-07-27 10:57:16 +01:00
|
|
|
|
2023-09-15 10:01:09 +01:00
|
|
|
var clientSessionDelegate: ClientSessionDelegate { keychainController }
|
|
|
|
|
2024-08-08 18:29:39 +02:00
|
|
|
init(keychainController: KeychainControllerProtocol,
|
|
|
|
appSettings: AppSettings,
|
|
|
|
appHooks: AppHooks,
|
|
|
|
networkMonitor: NetworkMonitorProtocol) {
|
2023-10-11 13:59:47 +01:00
|
|
|
self.keychainController = keychainController
|
2024-07-18 10:16:51 +01:00
|
|
|
self.appSettings = appSettings
|
2024-07-18 09:47:37 +01:00
|
|
|
self.appHooks = appHooks
|
2024-08-08 18:29:39 +02:00
|
|
|
self.networkMonitor = networkMonitor
|
2022-06-14 17:36:28 +01:00
|
|
|
}
|
|
|
|
|
2022-12-16 10:02:22 +02:00
|
|
|
/// Deletes all data stored in the shared container and keychain
|
|
|
|
func reset() {
|
2023-03-31 14:42:05 +03:00
|
|
|
MXLog.warning("Resetting the UserSessionStore. All accounts will be affected.")
|
2024-06-06 18:35:57 +01:00
|
|
|
try? FileManager.default.removeItem(at: .sessionsBaseDirectory)
|
2022-12-16 10:02:22 +02:00
|
|
|
keychainController.removeAllRestorationTokens()
|
|
|
|
}
|
|
|
|
|
2023-01-18 16:29:44 +02:00
|
|
|
func restoreUserSession() async -> Result<UserSessionProtocol, UserSessionStoreError> {
|
2022-11-04 13:24:53 +02:00
|
|
|
let availableCredentials = keychainController.restorationTokens()
|
2022-06-14 17:36:28 +01:00
|
|
|
|
2022-07-27 10:57:16 +01:00
|
|
|
guard let credentials = availableCredentials.first else {
|
2022-06-14 17:36:28 +01:00
|
|
|
return .failure(.missingCredentials)
|
|
|
|
}
|
|
|
|
|
2022-07-27 10:57:16 +01:00
|
|
|
switch await restorePreviousLogin(credentials) {
|
2022-06-14 17:36:28 +01:00
|
|
|
case .success(let clientProxy):
|
2023-01-18 16:29:44 +02:00
|
|
|
return .success(buildUserSessionWithClient(clientProxy))
|
2022-06-14 17:36:28 +01:00
|
|
|
case .failure(let error):
|
|
|
|
MXLog.error("Failed restoring login with error: \(error)")
|
|
|
|
|
|
|
|
// On any restoration failure reset the token and restart
|
2024-06-06 18:35:57 +01:00
|
|
|
keychainController.removeRestorationTokenForUsername(credentials.userID)
|
2024-09-11 14:32:03 +01:00
|
|
|
credentials.restorationToken.sessionDirectories.delete()
|
2022-06-14 17:36:28 +01:00
|
|
|
|
|
|
|
return .failure(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-25 14:40:18 +01:00
|
|
|
func userSession(for client: ClientProtocol, sessionDirectories: SessionDirectories, passphrase: String?) async -> Result<UserSessionProtocol, UserSessionStoreError> {
|
2024-01-12 16:45:59 +00:00
|
|
|
do {
|
2024-05-28 10:17:13 +03:00
|
|
|
let session = try client.session()
|
2024-01-12 16:45:59 +00:00
|
|
|
let userID = try client.userId()
|
2025-03-06 12:16:28 +01:00
|
|
|
let clientProxy = try await setupProxyForClient(client, needsSlidingSyncMigration: false)
|
2024-01-12 16:45:59 +00:00
|
|
|
|
|
|
|
keychainController.setRestorationToken(RestorationToken(session: session,
|
2024-09-11 14:32:03 +01:00
|
|
|
sessionDirectories: sessionDirectories,
|
2024-01-12 16:45:59 +00:00
|
|
|
passphrase: passphrase,
|
2025-02-18 10:34:32 +00:00
|
|
|
pusherNotificationClientIdentifier: clientProxy.pusherNotificationClientIdentifier,
|
|
|
|
slidingSyncProxyURLString: nil),
|
2024-01-12 16:45:59 +00:00
|
|
|
forUsername: userID)
|
|
|
|
|
2024-09-13 14:46:30 +03:00
|
|
|
MXLog.info("Set up session for user \(userID) at: \(sessionDirectories)")
|
|
|
|
|
2023-01-18 16:29:44 +02:00
|
|
|
return .success(buildUserSessionWithClient(clientProxy))
|
2024-01-12 16:45:59 +00:00
|
|
|
} catch {
|
2022-06-14 17:36:28 +01:00
|
|
|
MXLog.error("Failed creating user session with error: \(error)")
|
2024-01-12 16:45:59 +00:00
|
|
|
return .failure(.failedSettingUpSession)
|
2022-06-14 17:36:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func logout(userSession: UserSessionProtocol) {
|
2023-01-20 13:37:39 +00:00
|
|
|
let userID = userSession.clientProxy.userID
|
2024-06-06 18:35:57 +01:00
|
|
|
let credentials = keychainController.restorationTokens().first { $0.userID == userID }
|
2022-11-04 13:24:53 +02:00
|
|
|
keychainController.removeRestorationTokenForUsername(userID)
|
2024-06-06 18:35:57 +01:00
|
|
|
|
|
|
|
if let credentials {
|
2024-09-11 14:32:03 +01:00
|
|
|
credentials.restorationToken.sessionDirectories.delete()
|
2024-06-06 18:35:57 +01:00
|
|
|
}
|
2022-06-14 17:36:28 +01:00
|
|
|
}
|
2025-03-05 13:30:18 +02:00
|
|
|
|
2022-09-22 13:10:23 +03:00
|
|
|
// MARK: - Private
|
|
|
|
|
2023-01-18 16:29:44 +02:00
|
|
|
private func buildUserSessionWithClient(_ clientProxy: ClientProxyProtocol) -> UserSessionProtocol {
|
2023-10-13 11:48:11 +02:00
|
|
|
let mediaProvider = MediaProvider(mediaLoader: clientProxy,
|
2024-08-15 18:20:19 +03:00
|
|
|
imageCache: .onlyInMemory,
|
|
|
|
networkMonitor: networkMonitor)
|
2023-10-13 11:48:11 +02:00
|
|
|
|
2024-04-22 18:10:24 +03:00
|
|
|
let voiceMessageMediaManager = VoiceMessageMediaManager(mediaProvider: mediaProvider)
|
2023-10-13 11:48:11 +02:00
|
|
|
|
2023-01-18 16:29:44 +02:00
|
|
|
return UserSession(clientProxy: clientProxy,
|
2023-10-13 11:48:11 +02:00
|
|
|
mediaProvider: mediaProvider,
|
|
|
|
voiceMessageMediaManager: voiceMessageMediaManager)
|
2023-01-18 16:29:44 +02:00
|
|
|
}
|
|
|
|
|
2022-07-27 10:57:16 +01:00
|
|
|
private func restorePreviousLogin(_ credentials: KeychainCredentials) async -> Result<ClientProxyProtocol, UserSessionStoreError> {
|
2024-01-12 16:45:59 +00:00
|
|
|
if credentials.restorationToken.passphrase != nil {
|
|
|
|
MXLog.info("Restoring client with encrypted store.")
|
|
|
|
}
|
2024-02-21 15:17:23 +01:00
|
|
|
|
2024-11-12 14:13:28 +02:00
|
|
|
guard credentials.restorationToken.sessionDirectories.isNonTransientUserDataValid() else {
|
|
|
|
MXLog.error("Failed restoring login, missing non-transient user data")
|
|
|
|
return .failure(.failedRestoringLogin)
|
|
|
|
}
|
|
|
|
|
2024-02-21 15:17:23 +01:00
|
|
|
let homeserverURL = credentials.restorationToken.session.homeserverUrl
|
|
|
|
|
2024-06-24 15:05:00 +01:00
|
|
|
let builder = ClientBuilder
|
|
|
|
.baseBuilder(httpProxy: URL(string: homeserverURL)?.globalProxy,
|
2024-08-29 11:50:06 +01:00
|
|
|
slidingSync: .restored,
|
2024-07-18 09:47:37 +01:00
|
|
|
sessionDelegate: keychainController,
|
2024-09-18 17:30:45 +02:00
|
|
|
appHooks: appHooks,
|
2024-12-10 17:43:16 +02:00
|
|
|
enableOnlySignedDeviceIsolationMode: appSettings.enableOnlySignedDeviceIsolationMode,
|
|
|
|
eventCacheEnabled: appSettings.eventCacheEnabled)
|
2024-09-11 14:32:03 +01:00
|
|
|
.sessionPaths(dataPath: credentials.restorationToken.sessionDirectories.dataPath,
|
|
|
|
cachePath: credentials.restorationToken.sessionDirectories.cachePath)
|
2022-07-27 10:57:16 +01:00
|
|
|
.username(username: credentials.userID)
|
2024-02-21 15:17:23 +01:00
|
|
|
.homeserverUrl(url: homeserverURL)
|
2024-01-12 16:45:59 +00:00
|
|
|
.passphrase(passphrase: credentials.restorationToken.passphrase)
|
2024-02-21 15:17:23 +01:00
|
|
|
|
2022-09-21 17:50:15 +01:00
|
|
|
do {
|
2024-06-24 15:05:00 +01:00
|
|
|
let client = try await builder.build()
|
2024-04-08 10:26:33 +03:00
|
|
|
|
2024-05-03 12:00:05 +01:00
|
|
|
try await client.restoreSession(session: credentials.restorationToken.session)
|
2024-04-08 10:26:33 +03:00
|
|
|
|
2024-09-13 14:46:30 +03:00
|
|
|
MXLog.info("Set up session for user \(credentials.userID) at: \(credentials.restorationToken.sessionDirectories)")
|
|
|
|
|
2025-03-06 12:16:28 +01:00
|
|
|
return try await .success(setupProxyForClient(client, needsSlidingSyncMigration: credentials.restorationToken.needsSlidingSyncMigration))
|
|
|
|
} catch UserSessionStoreError.failedSettingUpClientProxy(let error) {
|
|
|
|
// If this has failed, there is likely something wrong with the creation of the sync service
|
|
|
|
// There is nothing we can do, but at the same time we don't want the user to the get logged out
|
|
|
|
// So it's better to crash here and let the app restart
|
|
|
|
fatalError("Failed setting up the client proxy with error: \(error)")
|
2022-09-21 17:50:15 +01:00
|
|
|
} catch {
|
2022-06-14 17:36:28 +01:00
|
|
|
MXLog.error("Failed restoring login with error: \(error)")
|
|
|
|
return .failure(.failedRestoringLogin)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-06 12:16:28 +01:00
|
|
|
private func setupProxyForClient(_ client: ClientProtocol, needsSlidingSyncMigration: Bool) async throws -> ClientProxyProtocol {
|
|
|
|
do {
|
|
|
|
return try await ClientProxy(client: client,
|
|
|
|
needsSlidingSyncMigration: needsSlidingSyncMigration,
|
|
|
|
networkMonitor: networkMonitor,
|
|
|
|
appSettings: appSettings)
|
|
|
|
} catch {
|
|
|
|
throw UserSessionStoreError.failedSettingUpClientProxy(error)
|
|
|
|
}
|
2022-06-14 17:36:28 +01:00
|
|
|
}
|
|
|
|
}
|