mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 06:55:59 +00:00
Enable database encryption for new logins on Nightly/PR builds. (#2328)
- Slightly reworks where the pusher client ID is generated.
This commit is contained in:
@@ -14,24 +14,13 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import CryptoKit
|
||||
import Foundation
|
||||
|
||||
import MatrixRustSDK
|
||||
|
||||
struct RestorationToken: Codable, Equatable {
|
||||
let session: MatrixRustSDK.Session
|
||||
let passphrase: String?
|
||||
let pusherNotificationClientIdentifier: String?
|
||||
|
||||
init(session: MatrixRustSDK.Session) {
|
||||
self.session = session
|
||||
if let data = session.userId.data(using: .utf8) {
|
||||
let digest = SHA256.hash(data: data)
|
||||
pusherNotificationClientIdentifier = digest.compactMap { String(format: "%02x", $0) }.joined()
|
||||
} else {
|
||||
pusherNotificationClientIdentifier = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension MatrixRustSDK.Session: Codable {
|
||||
|
||||
@@ -68,13 +68,21 @@ class UserSessionStore: UserSessionStoreProtocol {
|
||||
}
|
||||
}
|
||||
|
||||
func userSession(for client: Client) async -> Result<UserSessionProtocol, UserSessionStoreError> {
|
||||
switch await setupProxyForClient(client) {
|
||||
case .success(let clientProxy):
|
||||
func userSession(for client: Client, passphrase: String?) async -> Result<UserSessionProtocol, UserSessionStoreError> {
|
||||
do {
|
||||
let session = try client.session()
|
||||
let userID = try client.userId()
|
||||
let clientProxy = await setupProxyForClient(client)
|
||||
|
||||
keychainController.setRestorationToken(RestorationToken(session: session,
|
||||
passphrase: passphrase,
|
||||
pusherNotificationClientIdentifier: clientProxy.pusherNotificationClientIdentifier),
|
||||
forUsername: userID)
|
||||
|
||||
return .success(buildUserSessionWithClient(clientProxy))
|
||||
case .failure(let error):
|
||||
} catch {
|
||||
MXLog.error("Failed creating user session with error: \(error)")
|
||||
return .failure(error)
|
||||
return .failure(.failedSettingUpSession)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,10 +112,14 @@ class UserSessionStore: UserSessionStoreProtocol {
|
||||
}
|
||||
|
||||
private func restorePreviousLogin(_ credentials: KeychainCredentials) async -> Result<ClientProxyProtocol, UserSessionStoreError> {
|
||||
if credentials.restorationToken.passphrase != nil {
|
||||
MXLog.info("Restoring client with encrypted store.")
|
||||
}
|
||||
let builder = ClientBuilder()
|
||||
.basePath(path: baseDirectory.path)
|
||||
.username(username: credentials.userID)
|
||||
.homeserverUrl(url: credentials.restorationToken.session.homeserverUrl)
|
||||
.passphrase(passphrase: credentials.restorationToken.passphrase)
|
||||
.userAgent(userAgent: UserAgentBuilder.makeASCIIUserAgent())
|
||||
.enableCrossProcessRefreshLock(processId: InfoPlistReader.main.bundleIdentifier,
|
||||
sessionDelegate: keychainController)
|
||||
@@ -119,30 +131,18 @@ class UserSessionStore: UserSessionStoreProtocol {
|
||||
try client.restoreSession(session: credentials.restorationToken.session)
|
||||
return client
|
||||
}
|
||||
return await setupProxyForClient(client)
|
||||
return await .success(setupProxyForClient(client))
|
||||
} catch {
|
||||
MXLog.error("Failed restoring login with error: \(error)")
|
||||
return .failure(.failedRestoringLogin)
|
||||
}
|
||||
}
|
||||
|
||||
private func setupProxyForClient(_ client: Client) async -> Result<ClientProxyProtocol, UserSessionStoreError> {
|
||||
do {
|
||||
let session = try client.session()
|
||||
let userID = try client.userId()
|
||||
|
||||
keychainController.setRestorationToken(RestorationToken(session: session), forUsername: userID)
|
||||
} catch {
|
||||
MXLog.error("Failed setting up user session with error: \(error)")
|
||||
return .failure(.failedSettingUpSession)
|
||||
}
|
||||
|
||||
let clientProxy = await ClientProxy(client: client,
|
||||
backgroundTaskService: backgroundTaskService,
|
||||
appSettings: ServiceLocator.shared.settings,
|
||||
networkMonitor: ServiceLocator.shared.networkMonitor)
|
||||
|
||||
return .success(clientProxy)
|
||||
private func setupProxyForClient(_ client: Client) async -> ClientProxyProtocol {
|
||||
await ClientProxy(client: client,
|
||||
backgroundTaskService: backgroundTaskService,
|
||||
appSettings: ServiceLocator.shared.settings,
|
||||
networkMonitor: ServiceLocator.shared.networkMonitor)
|
||||
}
|
||||
|
||||
private func deleteSessionDirectory(for userID: String) {
|
||||
|
||||
@@ -42,8 +42,8 @@ protocol UserSessionStoreProtocol {
|
||||
/// Restores an existing user session.
|
||||
func restoreUserSession() async -> Result<UserSessionProtocol, UserSessionStoreError>
|
||||
|
||||
/// Creates a user session for a new client from the SDK.
|
||||
func userSession(for client: Client) async -> Result<UserSessionProtocol, UserSessionStoreError>
|
||||
/// Creates a user session for a new client from the SDK along with the passphrase used for the data stores.
|
||||
func userSession(for client: Client, passphrase: String?) async -> Result<UserSessionProtocol, UserSessionStoreError>
|
||||
|
||||
/// Logs out of the specified session.
|
||||
func logout(userSession: UserSessionProtocol)
|
||||
|
||||
Reference in New Issue
Block a user