crypto: indicate when a device was updated

update `ReadOnlyDevice::update_device` to return a bool indicating whether
anything is changing.
This commit is contained in:
Richard van der Hoff
2024-05-22 13:28:12 +01:00
parent d7a887766c
commit eee0dc4e87
2 changed files with 17 additions and 7 deletions

View File

@@ -848,7 +848,12 @@ impl ReadOnlyDevice {
}
/// Update a device with a new device keys struct.
pub(crate) fn update_device(&mut self, device_keys: &DeviceKeys) -> Result<(), SignatureError> {
///
/// Returns `true` if any changes were made to the data.
pub(crate) fn update_device(
&mut self,
device_keys: &DeviceKeys,
) -> Result<bool, SignatureError> {
self.verify_device_keys(device_keys)?;
if self.user_id() != device_keys.user_id || self.device_id() != device_keys.device_id {
@@ -858,10 +863,13 @@ impl ReadOnlyDevice {
self.ed25519_key().map(Box::new),
device_keys.ed25519_key().map(Box::new),
))
} else {
} else if self.inner.as_ref() != device_keys {
self.inner = device_keys.clone().into();
Ok(())
Ok(true)
} else {
// no changes needed
Ok(false)
}
}
@@ -1066,9 +1074,11 @@ pub(crate) mod tests {
let mut device_keys = device_keys();
device_keys.unsigned.device_display_name = Some(display_name.clone());
device.update_device(&device_keys).unwrap();
assert!(device.update_device(&device_keys).unwrap());
assert_eq!(&display_name, device.display_name().as_ref().unwrap());
// A second call to `update_device` with the same data should return `false`.
assert!(!device.update_device(&device_keys).unwrap());
}
#[test]

View File

@@ -39,7 +39,7 @@ use super::{EventEncryptionAlgorithm, Signatures};
/// identity keys.
///
/// [device_keys_spec]: https://spec.matrix.org/v1.10/client-server-api/#_matrixclientv3keysupload_devicekeys
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(try_from = "DeviceKeyHelper", into = "DeviceKeyHelper")]
pub struct DeviceKeys {
/// The ID of the user the device belongs to.
@@ -130,7 +130,7 @@ impl DeviceKeys {
}
/// Additional data added to device key information by intermediate servers.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
pub struct UnsignedDeviceInfo {
/// The display name which the user set on the device.
#[serde(skip_serializing_if = "Option::is_none")]