From 806ee13aa08da50342c4ae99c0aaa4e4951f2531 Mon Sep 17 00:00:00 2001 From: Valere Date: Mon, 30 Sep 2024 11:19:10 +0200 Subject: [PATCH] ffi: Expose `room_decryption_trust_requirement` for ClientBuilder --- bindings/matrix-sdk-crypto-ffi/src/machine.rs | 5 ++--- bindings/matrix-sdk-ffi/src/client_builder.rs | 17 +++++++++++++++-- crates/matrix-sdk-crypto/src/lib.rs | 2 ++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/bindings/matrix-sdk-crypto-ffi/src/machine.rs b/bindings/matrix-sdk-crypto-ffi/src/machine.rs index 3cfabeaf4..9e978fbb4 100644 --- a/bindings/matrix-sdk-crypto-ffi/src/machine.rs +++ b/bindings/matrix-sdk-crypto-ffi/src/machine.rs @@ -17,7 +17,7 @@ use matrix_sdk_crypto::{ decrypt_room_key_export, encrypt_room_key_export, olm::ExportedRoomKey, store::{BackupDecryptionKey, Changes}, - DecryptionSettings, LocalTrust, OlmMachine as InnerMachine, ToDeviceRequest, TrustRequirement, + DecryptionSettings, LocalTrust, OlmMachine as InnerMachine, ToDeviceRequest, UserIdentity as SdkUserIdentity, }; use ruma::{ @@ -867,6 +867,7 @@ impl OlmMachine { room_id: String, handle_verification_events: bool, strict_shields: bool, + decryption_settings: DecryptionSettings, ) -> Result { // Element Android wants only the content and the type and will create a // decrypted event with those two itself, this struct makes sure we @@ -882,8 +883,6 @@ impl OlmMachine { let event: Raw<_> = serde_json::from_str(&event)?; let room_id = RoomId::parse(room_id)?; - let decryption_settings = - DecryptionSettings { sender_device_trust_requirement: TrustRequirement::Untrusted }; let decrypted = self.runtime.block_on(self.inner.decrypt_room_event( &event, &room_id, diff --git a/bindings/matrix-sdk-ffi/src/client_builder.rs b/bindings/matrix-sdk-ffi/src/client_builder.rs index f619daedf..ec0522faf 100644 --- a/bindings/matrix-sdk-ffi/src/client_builder.rs +++ b/bindings/matrix-sdk-ffi/src/client_builder.rs @@ -5,7 +5,7 @@ use matrix_sdk::{ authentication::qrcode::{self, DeviceCodeErrorResponseType, LoginFailureReason}, crypto::{ types::qr_login::{LoginQrCodeDecodeError, QrCodeModeData}, - CollectStrategy, + CollectStrategy, TrustRequirement, }, encryption::{BackupDownloadStrategy, EncryptionSettings}, reqwest::Certificate, @@ -266,6 +266,7 @@ pub struct ClientBuilder { disable_built_in_root_certificates: bool, encryption_settings: EncryptionSettings, room_key_recipient_strategy: CollectStrategy, + decryption_trust_requirement: TrustRequirement, request_config: Option, } @@ -294,6 +295,7 @@ impl ClientBuilder { auto_enable_backups: false, }, room_key_recipient_strategy: Default::default(), + decryption_trust_requirement: TrustRequirement::Untrusted, request_config: Default::default(), }) } @@ -449,6 +451,16 @@ impl ClientBuilder { Arc::new(builder) } + /// Set the trust requirement to be used when decrypting events. + pub fn room_decryption_trust_requirement( + self: Arc, + trust_requirement: TrustRequirement, + ) -> Arc { + let mut builder = unwrap_or_clone_arc(self); + builder.decryption_trust_requirement = trust_requirement; + Arc::new(builder) + } + /// Add a default request config to this client. pub fn request_config(self: Arc, config: RequestConfig) -> Arc { let mut builder = unwrap_or_clone_arc(self); @@ -548,7 +560,8 @@ impl ClientBuilder { inner_builder = inner_builder .with_encryption_settings(builder.encryption_settings) - .with_room_key_recipient_strategy(builder.room_key_recipient_strategy); + .with_room_key_recipient_strategy(builder.room_key_recipient_strategy) + .with_decryption_trust_requirement(builder.decryption_trust_requirement); match builder.sliding_sync_version_builder { SlidingSyncVersionBuilder::None => { diff --git a/crates/matrix-sdk-crypto/src/lib.rs b/crates/matrix-sdk-crypto/src/lib.rs index d46fbbbe5..afe19c12e 100644 --- a/crates/matrix-sdk-crypto/src/lib.rs +++ b/crates/matrix-sdk-crypto/src/lib.rs @@ -117,6 +117,7 @@ uniffi::setup_scaffolding!(); /// The trust level in the sender's device that is required to decrypt an /// event. #[derive(Clone, Copy, Debug, Deserialize, Serialize)] +#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))] pub enum TrustRequirement { /// Decrypt events from everyone regardless of trust. Untrusted, @@ -129,6 +130,7 @@ pub enum TrustRequirement { /// Settings for decrypting messages #[derive(Clone, Debug, Deserialize, Serialize)] +#[cfg_attr(feature = "uniffi", derive(uniffi::Record))] pub struct DecryptionSettings { /// The trust level in the sender's device that is required to decrypt the /// event. If the sender's device is not sufficiently trusted,