From 6db7eb06940b19971c8e5c172a518c22db10720c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 17 Aug 2020 17:36:07 +0200 Subject: [PATCH] crypto: Add a method to directly verify a device. --- matrix_sdk/src/device.rs | 19 +++++++++++++++++-- matrix_sdk_crypto/src/device.rs | 16 +++++++++++++++- matrix_sdk_crypto/src/verification/machine.rs | 2 +- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/matrix_sdk/src/device.rs b/matrix_sdk/src/device.rs index 008579a0..37315c48 100644 --- a/matrix_sdk/src/device.rs +++ b/matrix_sdk/src/device.rs @@ -12,9 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::ops::Deref; +use std::{ops::Deref, result::Result as StdResult}; -use matrix_sdk_base::{Device as BaseDevice, ReadOnlyDevice, UserDevices as BaseUserDevices}; +use matrix_sdk_base::{ + CryptoStoreError, Device as BaseDevice, ReadOnlyDevice, TrustState, + UserDevices as BaseUserDevices, +}; use matrix_sdk_common::{ api::r0::to_device::send_event_to_device::Request as ToDeviceRequest, identifiers::DeviceId, }; @@ -72,6 +75,18 @@ impl Device { http_client: self.http_client.clone(), }) } + + /// Set the trust state of the device to the given state. + /// + /// # Arguments + /// + /// * `trust_state` - The new trust state that should be set for the device. + pub async fn set_trust_state( + &self, + trust_state: TrustState, + ) -> StdResult<(), CryptoStoreError> { + self.inner.set_trust_state(trust_state).await.into() + } } /// A read only view over all devices belonging to a user. diff --git a/matrix_sdk_crypto/src/device.rs b/matrix_sdk_crypto/src/device.rs index 5f20c0a9..f8eb6e49 100644 --- a/matrix_sdk_crypto/src/device.rs +++ b/matrix_sdk_crypto/src/device.rs @@ -36,7 +36,8 @@ use serde_json::{json, Value}; use super::{Account, OlmMachine}; use crate::{ - error::SignatureError, verification::VerificationMachine, verify_json, ReadOnlyUserDevices, Sas, + error::SignatureError, store::Result as StoreResult, verification::VerificationMachine, + verify_json, ReadOnlyUserDevices, Sas, }; /// A read-only version of a `Device`. @@ -74,6 +75,19 @@ impl Device { pub fn start_verification(&self) -> (Sas, OwnedToDeviceRequest) { self.verification_machine.start_sas(self.inner.clone()) } + + /// Set the trust state of the device to the given state. + /// + /// # Arguments + /// + /// * `trust_state` - The new trust state that should be set for the device. + pub async fn set_trust_state(&self, trust_state: TrustState) -> StoreResult<()> { + self.inner.set_trust_state(trust_state); + self.verification_machine + .store + .save_devices(&[self.inner.clone()]) + .await + } } /// A read only view over all devices belonging to a user. diff --git a/matrix_sdk_crypto/src/verification/machine.rs b/matrix_sdk_crypto/src/verification/machine.rs index e4e7575d..d6382b7a 100644 --- a/matrix_sdk_crypto/src/verification/machine.rs +++ b/matrix_sdk_crypto/src/verification/machine.rs @@ -30,7 +30,7 @@ use crate::{Account, CryptoStore, CryptoStoreError, ReadOnlyDevice}; #[derive(Clone, Debug)] pub struct VerificationMachine { account: Account, - store: Arc>, + pub(crate) store: Arc>, verifications: Arc>, outgoing_to_device_messages: Arc>, }