Skip to content

Commit bef186d

Browse files
committed
feat(ffi): Include the user ID in the secrets bundle
This allows us to double check if the right user got logged in.
1 parent 44a92c3 commit bef186d

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

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

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use std::{
1616
collections::HashMap,
1717
fmt::Debug,
1818
path::PathBuf,
19+
str::FromStr,
1920
sync::{Arc, OnceLock},
2021
time::Duration,
2122
};
@@ -105,7 +106,7 @@ use ruma::{
105106
},
106107
push::{HttpPusherData as RumaHttpPusherData, PushFormat as RumaPushFormat},
107108
room::RoomType,
108-
OwnedDeviceId, OwnedServerName, RoomAliasId, RoomOrAliasId, ServerName,
109+
OwnedDeviceId, OwnedServerName, OwnedUserId, RoomAliasId, RoomOrAliasId, ServerName,
109110
};
110111
use serde::{Deserialize, Serialize};
111112
use serde_json::{json, Value};
@@ -205,16 +206,18 @@ impl From<PushFormat> for RumaPushFormat {
205206

206207
#[derive(uniffi::Object)]
207208
pub struct SecretsBundle {
209+
user_id: OwnedUserId,
208210
inner: matrix_sdk_base::crypto::types::SecretsBundle,
209211
}
210212

211213
#[matrix_sdk_ffi_macros::export]
212214
impl SecretsBundle {
213215
#[uniffi::constructor]
214-
pub fn from_str(bundle: &str) -> Result<Arc<Self>, ClientError> {
216+
pub fn from_str(user_id: &str, bundle: &str) -> Result<Arc<Self>, ClientError> {
217+
let user_id = OwnedUserId::from_str(user_id)?;
215218
let bundle = serde_json::from_str(bundle)?;
216219

217-
Ok(Self { inner: bundle }.into())
220+
Ok(Self { user_id, inner: bundle }.into())
218221
}
219222

220223
#[uniffi::constructor]
@@ -223,6 +226,11 @@ impl SecretsBundle {
223226
}
224227
}
225228

229+
#[matrix_sdk_ffi_macros::export]
230+
pub fn database_contains_secrets_bundle(database_path: &str) -> Result<bool, ClientError> {
231+
todo!()
232+
}
233+
226234
#[matrix_sdk_ffi_macros::export(callback_interface)]
227235
pub trait ClientDelegate: SyncOutsideWasm + SendOutsideWasm {
228236
/// A callback invoked whenever the SDK runs into an unknown token error.
@@ -443,18 +451,29 @@ impl Client {
443451
&self,
444452
secrets_bundle: &SecretsBundle,
445453
) -> Result<(), ClientError> {
446-
self.inner
447-
.encryption()
448-
.import_secrets_bundle(&secrets_bundle.inner)
449-
.await
450-
.map_err(|e| ClientError::from_err(e))?;
454+
let user_id = self.inner.user_id().expect(
455+
"We should have a user ID available now, this is only called once we're logged in",
456+
);
451457

452-
// Upload the device keys, this will ensure that other devices see us as a fully
453-
// verified device as soon as this method returns.
454-
self.inner.encryption().ensure_device_keys_upload().await?;
455-
self.inner.encryption().wait_for_e2ee_initialization_tasks().await;
458+
if user_id == secrets_bundle.user_id {
459+
self.inner
460+
.encryption()
461+
.import_secrets_bundle(&secrets_bundle.inner)
462+
.await
463+
.map_err(|e| ClientError::from_err(e))?;
456464

457-
Ok(())
465+
// Upload the device keys, this will ensure that other devices see us as a fully
466+
// verified device as soon as this method returns.
467+
self.inner.encryption().ensure_device_keys_upload().await?;
468+
self.inner.encryption().wait_for_e2ee_initialization_tasks().await;
469+
470+
Ok(())
471+
} else {
472+
Err(ClientError::Generic {
473+
msg: "Secrets bundle does not belong to the user which was logged in".to_owned(),
474+
details: None,
475+
})
476+
}
458477
}
459478
}
460479

0 commit comments

Comments
 (0)