From c20e6aeca7d93bdf41b6f890bb5d2d7d1a8f2b59 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Fri, 9 Feb 2024 17:47:27 +0100 Subject: [PATCH] ffi: configure encryption settings only when it's needed It's wrong that the first client, used only to determine how to log in and find the user id, try to run the encryption initialization tasks. In particular, it should not even try to bootstrap the account, as this may send OTKs to the server, which the client will forget about as soon as it's respawned as a database-backed client. --- .../src/authentication_service.rs | 6 ++ bindings/matrix-sdk-ffi/src/client_builder.rs | 56 +++++++++---------- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/bindings/matrix-sdk-ffi/src/authentication_service.rs b/bindings/matrix-sdk-ffi/src/authentication_service.rs index 916ccbe96..544767856 100644 --- a/bindings/matrix-sdk-ffi/src/authentication_service.rs +++ b/bindings/matrix-sdk-ffi/src/authentication_service.rs @@ -591,6 +591,12 @@ impl AuthenticationService { .passphrase(self.passphrase.clone()) .homeserver_url(homeserver_url) .sliding_sync_proxy(sliding_sync_proxy) + .with_encryption_settings(matrix_sdk::encryption::EncryptionSettings { + auto_enable_cross_signing: true, + backup_download_strategy: + matrix_sdk::encryption::BackupDownloadStrategy::AfterDecryptionFailure, + auto_enable_backups: true, + }) .username(user_id.to_string()); if let Some(id) = &self.cross_process_refresh_lock_id { diff --git a/bindings/matrix-sdk-ffi/src/client_builder.rs b/bindings/matrix-sdk-ffi/src/client_builder.rs index 1326736d1..0dbe2d6e7 100644 --- a/bindings/matrix-sdk-ffi/src/client_builder.rs +++ b/bindings/matrix-sdk-ffi/src/client_builder.rs @@ -1,7 +1,7 @@ use std::{fs, path::PathBuf, sync::Arc}; use matrix_sdk::{ - encryption::{BackupDownloadStrategy, EncryptionSettings}, + encryption::EncryptionSettings, ruma::{ api::{error::UnknownVersionError, MatrixVersion}, ServerName, UserId, @@ -43,7 +43,22 @@ pub struct ClientBuilder { impl ClientBuilder { #[uniffi::constructor] pub fn new() -> Arc { - Arc::new(Self::default()) + Arc::new(Self { + base_path: None, + username: None, + server_name: None, + homeserver_url: None, + server_versions: None, + passphrase: Zeroizing::new(None), + user_agent: None, + sliding_sync_proxy: None, + proxy: None, + disable_ssl_verification: false, + disable_automatic_token_refresh: false, + inner: MatrixClient::builder(), + cross_process_refresh_lock_id: None, + session_delegate: None, + }) } pub fn enable_cross_process_refresh_lock( @@ -136,6 +151,15 @@ impl ClientBuilder { } impl ClientBuilder { + pub(crate) fn with_encryption_settings( + self: Arc, + settings: EncryptionSettings, + ) -> Arc { + let mut builder = unwrap_or_clone_arc(self); + builder.inner = builder.inner.with_encryption_settings(settings); + Arc::new(builder) + } + pub(crate) fn enable_cross_process_refresh_lock_inner( self: Arc, process_id: String, @@ -247,31 +271,3 @@ impl ClientBuilder { )?) } } - -impl Default for ClientBuilder { - fn default() -> Self { - let encryption_settings = EncryptionSettings { - auto_enable_cross_signing: true, - auto_enable_backups: true, - backup_download_strategy: BackupDownloadStrategy::AfterDecryptionFailure, - }; - let inner = MatrixClient::builder().with_encryption_settings(encryption_settings); - - Self { - base_path: None, - username: None, - server_name: None, - homeserver_url: None, - server_versions: None, - passphrase: Zeroizing::new(None), - user_agent: None, - sliding_sync_proxy: None, - proxy: None, - disable_ssl_verification: false, - disable_automatic_token_refresh: false, - inner, - cross_process_refresh_lock_id: None, - session_delegate: None, - } - } -}