Skip to content

Commit

Permalink
ffi: Expose encryption_settings via FFI
Browse files Browse the repository at this point in the history
  • Loading branch information
andybalaam committed Apr 12, 2024
1 parent 7c68096 commit 0f86532
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 20 deletions.
11 changes: 4 additions & 7 deletions bindings/matrix-sdk-ffi/src/authentication_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use zeroize::Zeroize;
use super::{client::Client, client_builder::ClientBuilder};
use crate::{
client::ClientSessionDelegate,
client_builder::{CertificateBytes, ClientBuildError},
client_builder::{BackupDownloadStrategy, CertificateBytes, ClientBuildError},
error::ClientError,
};

Expand Down Expand Up @@ -621,12 +621,9 @@ 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,
})
.auto_enable_cross_signing(true)
.backup_download_strategy(BackupDownloadStrategy::AfterDecryptionFailure)
.auto_enable_backups(true)
.username(user_id.to_string());

if let Some(proxy) = &self.proxy {
Expand Down
89 changes: 76 additions & 13 deletions bindings/matrix-sdk-ffi/src/client_builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{fs, path::PathBuf, sync::Arc};

use matrix_sdk::{
encryption::{BackupDownloadStrategy, EncryptionSettings},
encryption::EncryptionSettings,
reqwest::Certificate,
ruma::{
api::{error::UnknownVersionError, MatrixVersion},
Expand All @@ -27,6 +27,44 @@ enum HomeserverConfig {
ServerNameOrUrl(String),
}

#[derive(uniffi::Enum)]
pub enum BackupDownloadStrategy {
/// Automatically download all room keys from the backup when the backup
/// recovery key has been received. The backup recovery key can be received
/// in two ways:
///
/// 1. Received as a `m.secret.send` to-device event, after a successful
/// interactive verification.
/// 2. Imported from secret storage (4S) using the
/// [`SecretStore::import_secrets()`] method.
///
/// [`SecretStore::import_secrets()`]: crate::encryption::secret_storage::SecretStore::import_secrets
OneShot,

/// Attempt to download a single room key if an event fails to be decrypted.
AfterDecryptionFailure,

/// Don't download any room keys automatically. The user can manually
/// download room keys using the [`Backups::download_room_key()`] methods.
Manual,
}

impl From<BackupDownloadStrategy> for matrix_sdk::encryption::BackupDownloadStrategy {
fn from(strat: BackupDownloadStrategy) -> Self {

Check warning on line 53 in bindings/matrix-sdk-ffi/src/client_builder.rs

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"strat" should be "start" or "strata".
match strat {

Check warning on line 54 in bindings/matrix-sdk-ffi/src/client_builder.rs

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"strat" should be "start" or "strata".
BackupDownloadStrategy::OneShot => {
matrix_sdk::encryption::BackupDownloadStrategy::OneShot
}
BackupDownloadStrategy::AfterDecryptionFailure => {
matrix_sdk::encryption::BackupDownloadStrategy::AfterDecryptionFailure
}
BackupDownloadStrategy::Manual => {
matrix_sdk::encryption::BackupDownloadStrategy::Manual
}
}
}
}

#[derive(Debug, thiserror::Error, uniffi::Error)]
#[uniffi(flat_error)]
pub enum ClientBuildError {
Expand Down Expand Up @@ -76,6 +114,7 @@ pub struct ClientBuilder {
cross_process_refresh_lock_id: Option<String>,
session_delegate: Option<Arc<dyn ClientSessionDelegate>>,
additional_root_certificates: Vec<Vec<u8>>,
encryption_settings: EncryptionSettings,
}

#[uniffi::export(async_runtime = "tokio")]
Expand All @@ -93,14 +132,16 @@ impl ClientBuilder {
proxy: None,
disable_ssl_verification: false,
disable_automatic_token_refresh: false,
inner: MatrixClient::builder().with_encryption_settings(EncryptionSettings {
auto_enable_cross_signing: false,
backup_download_strategy: BackupDownloadStrategy::AfterDecryptionFailure,
auto_enable_backups: false,
}),
inner: MatrixClient::builder(),
cross_process_refresh_lock_id: None,
session_delegate: None,
additional_root_certificates: Default::default(),
encryption_settings: EncryptionSettings {
auto_enable_cross_signing: false,
backup_download_strategy:
matrix_sdk::encryption::BackupDownloadStrategy::AfterDecryptionFailure,
auto_enable_backups: false,
},
})
}

Expand Down Expand Up @@ -203,21 +244,41 @@ impl ClientBuilder {
Arc::new(builder)
}

pub async fn build(self: Arc<Self>) -> Result<Arc<Client>, ClientBuildError> {
Ok(Arc::new(self.build_inner().await?))
pub fn auto_enable_cross_signing(
self: Arc<Self>,
auto_enable_cross_signing: bool,
) -> Arc<Self> {
let mut builder = unwrap_or_clone_arc(self);
builder.encryption_settings.auto_enable_cross_signing = auto_enable_cross_signing;
Arc::new(builder)
}
}

impl ClientBuilder {
pub(crate) fn with_encryption_settings(
/// Select a strategy to download room keys from the backup, by default room
/// keys won't be downloaded from the backup automatically.
///
/// Take a look at the [`BackupDownloadStrategy`] enum for more options.
pub fn backup_download_strategy(
self: Arc<Self>,
settings: EncryptionSettings,
backup_download_strategy: BackupDownloadStrategy,
) -> Arc<Self> {
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.with_encryption_settings(settings);
builder.encryption_settings.backup_download_strategy = backup_download_strategy.into();
Arc::new(builder)
}

/// Automatically create a backup version if no backup exists.
pub fn auto_enable_backups(self: Arc<Self>, auto_enable_backups: bool) -> Arc<Self> {
let mut builder = unwrap_or_clone_arc(self);
builder.encryption_settings.auto_enable_backups = auto_enable_backups;
Arc::new(builder)
}

pub async fn build(self: Arc<Self>) -> Result<Arc<Client>, ClientBuildError> {
Ok(Arc::new(self.build_inner().await?))
}
}

impl ClientBuilder {
pub(crate) fn enable_cross_process_refresh_lock_inner(
self: Arc<Self>,
process_id: String,
Expand Down Expand Up @@ -316,6 +377,8 @@ impl ClientBuilder {
);
}

inner_builder = inner_builder.with_encryption_settings(builder.encryption_settings);

let sdk_client = inner_builder.build().await?;

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

0 comments on commit 0f86532

Please sign in to comment.