Skip to content

Commit 2c7afc2

Browse files
authored
Merge pull request #3326 from matrix-org/andybalaam/allow-setting-encryption-settings
ffi: Expose encryption settings via FFI
2 parents 8f5e1f3 + ebcf1c4 commit 2c7afc2

File tree

3 files changed

+42
-18
lines changed

3 files changed

+42
-18
lines changed

bindings/matrix-sdk-ffi/src/authentication_service.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::{
44
};
55

66
use matrix_sdk::{
7+
encryption::BackupDownloadStrategy,
78
oidc::{
89
registrations::{ClientId, OidcRegistrations, OidcRegistrationsError},
910
types::{
@@ -621,12 +622,9 @@ impl AuthenticationService {
621622
.passphrase(self.passphrase.clone())
622623
.homeserver_url(homeserver_url)
623624
.sliding_sync_proxy(sliding_sync_proxy)
624-
.with_encryption_settings(matrix_sdk::encryption::EncryptionSettings {
625-
auto_enable_cross_signing: true,
626-
backup_download_strategy:
627-
matrix_sdk::encryption::BackupDownloadStrategy::AfterDecryptionFailure,
628-
auto_enable_backups: true,
629-
})
625+
.auto_enable_cross_signing(true)
626+
.backup_download_strategy(BackupDownloadStrategy::AfterDecryptionFailure)
627+
.auto_enable_backups(true)
630628
.username(user_id.to_string());
631629

632630
if let Some(proxy) = &self.proxy {

bindings/matrix-sdk-ffi/src/client_builder.rs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub struct ClientBuilder {
7676
cross_process_refresh_lock_id: Option<String>,
7777
session_delegate: Option<Arc<dyn ClientSessionDelegate>>,
7878
additional_root_certificates: Vec<Vec<u8>>,
79+
encryption_settings: EncryptionSettings,
7980
}
8081

8182
#[uniffi::export(async_runtime = "tokio")]
@@ -93,14 +94,16 @@ impl ClientBuilder {
9394
proxy: None,
9495
disable_ssl_verification: false,
9596
disable_automatic_token_refresh: false,
96-
inner: MatrixClient::builder().with_encryption_settings(EncryptionSettings {
97-
auto_enable_cross_signing: false,
98-
backup_download_strategy: BackupDownloadStrategy::AfterDecryptionFailure,
99-
auto_enable_backups: false,
100-
}),
97+
inner: MatrixClient::builder(),
10198
cross_process_refresh_lock_id: None,
10299
session_delegate: None,
103100
additional_root_certificates: Default::default(),
101+
encryption_settings: EncryptionSettings {
102+
auto_enable_cross_signing: false,
103+
backup_download_strategy:
104+
matrix_sdk::encryption::BackupDownloadStrategy::AfterDecryptionFailure,
105+
auto_enable_backups: false,
106+
},
104107
})
105108
}
106109

@@ -203,21 +206,41 @@ impl ClientBuilder {
203206
Arc::new(builder)
204207
}
205208

206-
pub async fn build(self: Arc<Self>) -> Result<Arc<Client>, ClientBuildError> {
207-
Ok(Arc::new(self.build_inner().await?))
209+
pub fn auto_enable_cross_signing(
210+
self: Arc<Self>,
211+
auto_enable_cross_signing: bool,
212+
) -> Arc<Self> {
213+
let mut builder = unwrap_or_clone_arc(self);
214+
builder.encryption_settings.auto_enable_cross_signing = auto_enable_cross_signing;
215+
Arc::new(builder)
208216
}
209-
}
210217

211-
impl ClientBuilder {
212-
pub(crate) fn with_encryption_settings(
218+
/// Select a strategy to download room keys from the backup. By default
219+
/// we download after a decryption failure.
220+
///
221+
/// Take a look at the [`BackupDownloadStrategy`] enum for more options.
222+
pub fn backup_download_strategy(
213223
self: Arc<Self>,
214-
settings: EncryptionSettings,
224+
backup_download_strategy: BackupDownloadStrategy,
215225
) -> Arc<Self> {
216226
let mut builder = unwrap_or_clone_arc(self);
217-
builder.inner = builder.inner.with_encryption_settings(settings);
227+
builder.encryption_settings.backup_download_strategy = backup_download_strategy;
218228
Arc::new(builder)
219229
}
220230

231+
/// Automatically create a backup version if no backup exists.
232+
pub fn auto_enable_backups(self: Arc<Self>, auto_enable_backups: bool) -> Arc<Self> {
233+
let mut builder = unwrap_or_clone_arc(self);
234+
builder.encryption_settings.auto_enable_backups = auto_enable_backups;
235+
Arc::new(builder)
236+
}
237+
238+
pub async fn build(self: Arc<Self>) -> Result<Arc<Client>, ClientBuildError> {
239+
Ok(Arc::new(self.build_inner().await?))
240+
}
241+
}
242+
243+
impl ClientBuilder {
221244
pub(crate) fn enable_cross_process_refresh_lock_inner(
222245
self: Arc<Self>,
223246
process_id: String,
@@ -316,6 +339,8 @@ impl ClientBuilder {
316339
);
317340
}
318341

342+
inner_builder = inner_builder.with_encryption_settings(builder.encryption_settings);
343+
319344
let sdk_client = inner_builder.build().await?;
320345

321346
// At this point, `sdk_client` might contain a `sliding_sync_proxy` that has

crates/matrix-sdk/src/encryption/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ pub struct EncryptionSettings {
161161

162162
/// Settings for end-to-end encryption features.
163163
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
164+
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
164165
pub enum BackupDownloadStrategy {
165166
/// Automatically download all room keys from the backup when the backup
166167
/// recovery key has been received. The backup recovery key can be received

0 commit comments

Comments
 (0)