From 5566886f208e9dfd06bc2b4c0236b8ce86b1dffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 19 Jul 2021 10:26:39 +0200 Subject: [PATCH] crypto: Add public methods to request verifications with devices --- matrix_sdk_crypto/src/identities/device.rs | 49 ++++++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/matrix_sdk_crypto/src/identities/device.rs b/matrix_sdk_crypto/src/identities/device.rs index 9b432e77..5f782a60 100644 --- a/matrix_sdk_crypto/src/identities/device.rs +++ b/matrix_sdk_crypto/src/identities/device.rs @@ -28,7 +28,8 @@ use ruma::{ encryption::{DeviceKeys, SignedKey}, events::{ forwarded_room_key::ForwardedRoomKeyToDeviceEventContent, - room::encrypted::EncryptedEventContent, AnyToDeviceEventContent, + key::verification::VerificationMethod, room::encrypted::EncryptedEventContent, + AnyToDeviceEventContent, }, DeviceId, DeviceIdBox, DeviceKeyAlgorithm, DeviceKeyId, EventEncryptionAlgorithm, UserId, }; @@ -43,7 +44,7 @@ use crate::{ olm::{InboundGroupSession, PrivateCrossSigningIdentity, Session, Utility}, store::{Changes, CryptoStore, DeviceChanges, Result as StoreResult}, verification::VerificationMachine, - OutgoingVerificationRequest, Sas, ToDeviceRequest, + OutgoingVerificationRequest, Sas, ToDeviceRequest, VerificationRequest, }; #[cfg(test)] use crate::{OlmMachine, ReadOnlyAccount}; @@ -125,7 +126,13 @@ impl Deref for Device { impl Device { /// Start a interactive verification with this `Device` /// - /// Returns a `Sas` object and to-device request that needs to be sent out. + /// Returns a `Sas` object and a to-device request that needs to be sent + /// out. + /// + /// This method has been deprecated in the spec and the + /// [`request_verification()`] method should be used instead. + /// + /// [`request_verification()`]: #method.request_verification pub async fn start_verification(&self) -> StoreResult<(Sas, ToDeviceRequest)> { let (sas, request) = self.verification_machine.start_sas(self.inner.clone()).await?; @@ -136,6 +143,42 @@ impl Device { } } + /// Request an interacitve verification with this `Device` + /// + /// Returns a `VerificationRequest` object and a to-device request that + /// needs to be sent out. + pub async fn request_verification(&self) -> (VerificationRequest, OutgoingVerificationRequest) { + self.request_verification_helper(None).await + } + + /// Request an interacitve verification with this `Device` + /// + /// Returns a `VerificationRequest` object and a to-device request that + /// needs to be sent out. + /// + /// # Arguments + /// + /// * `methods` - The verification methods that we want to support. + pub async fn request_verification_with_methods( + &self, + methods: Vec, + ) -> (VerificationRequest, OutgoingVerificationRequest) { + self.request_verification_helper(Some(methods)).await + } + + async fn request_verification_helper( + &self, + methods: Option>, + ) -> (VerificationRequest, OutgoingVerificationRequest) { + self.verification_machine + .request_to_device_verification( + self.user_id(), + vec![self.device_id().to_owned()], + methods, + ) + .await + } + /// Get the Olm sessions that belong to this device. pub(crate) async fn get_sessions(&self) -> StoreResult>>>> { if let Some(k) = self.get_key(DeviceKeyAlgorithm::Curve25519) {