From 71aba433da76f2bab5b491dde18ae8f8a3ef6432 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 9 Jun 2021 17:40:31 +0200 Subject: [PATCH] crypto: Add some more accessors to the sas structs --- matrix_sdk/src/sas.rs | 23 ++++++++++++++++++- matrix_sdk_crypto/src/verification/mod.rs | 6 +++-- .../src/verification/sas/inner_sas.rs | 14 +++++++++++ matrix_sdk_crypto/src/verification/sas/mod.rs | 12 +++++++++- 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/matrix_sdk/src/sas.rs b/matrix_sdk/src/sas.rs index 42949feb..17db6a2f 100644 --- a/matrix_sdk/src/sas.rs +++ b/matrix_sdk/src/sas.rs @@ -15,6 +15,7 @@ use matrix_sdk_base::crypto::{ AcceptSettings, OutgoingVerificationRequest, ReadOnlyDevice, Sas as BaseSas, }; +use ruma::UserId; use crate::{error::Result, Client}; @@ -43,14 +44,19 @@ impl Sas { /// # use matrix_sdk::Client; /// # use futures::executor::block_on; /// # use url::Url; + /// # use ruma::identifiers::user_id; /// use matrix_sdk::Sas; /// use matrix_sdk_base::crypto::AcceptSettings; /// use matrix_sdk::events::key::verification::ShortAuthenticationString; /// # let homeserver = Url::parse("http://example.com").unwrap(); /// # let client = Client::new(homeserver).unwrap(); /// # let flow_id = "someID"; + /// # let user_id = user_id!("@alice:example"); /// # block_on(async { - /// let sas = client.get_verification(flow_id).await.unwrap(); + /// let sas = client + /// .get_verification(&user_id, flow_id) + /// .await + /// .unwrap(); /// /// let only_decimal = AcceptSettings::with_allowed_methods( /// vec![ShortAuthenticationString::Decimal] @@ -141,4 +147,19 @@ impl Sas { pub fn other_device(&self) -> &ReadOnlyDevice { self.inner.other_device() } + + /// Did this verification flow start from a verification request. + pub fn started_from_request(&self) -> bool { + self.inner.started_from_request() + } + + /// Is this a verification that is veryfying one of our own devices. + pub fn is_self_verification(&self) -> bool { + self.inner.is_self_verification() + } + + /// Get our own user id. + pub fn own_user_id(&self) -> &UserId { + self.inner.user_id() + } } diff --git a/matrix_sdk_crypto/src/verification/mod.rs b/matrix_sdk_crypto/src/verification/mod.rs index 95040dff..f0c5776e 100644 --- a/matrix_sdk_crypto/src/verification/mod.rs +++ b/matrix_sdk_crypto/src/verification/mod.rs @@ -240,6 +240,10 @@ impl IdentitiesBeingVerified { self.private_identity.user_id() } + fn is_self_verification(&self) -> bool { + self.user_id() == self.other_user_id() + } + fn other_user_id(&self) -> &UserId { self.device_being_verified.user_id() } @@ -370,8 +374,6 @@ impl IdentitiesBeingVerified { return Ok(None); } - // TODO signal an error, e.g. when the identity got deleted so we don't - // verify/save the device either. let identity = self.store.get_user_identity(self.other_user_id()).await?; if let Some(identity) = identity { diff --git a/matrix_sdk_crypto/src/verification/sas/inner_sas.rs b/matrix_sdk_crypto/src/verification/sas/inner_sas.rs index b9b37475..67b5302e 100644 --- a/matrix_sdk_crypto/src/verification/sas/inner_sas.rs +++ b/matrix_sdk_crypto/src/verification/sas/inner_sas.rs @@ -61,6 +61,20 @@ impl InnerSas { (InnerSas::Created(sas), content.into()) } + pub fn started_from_request(&self) -> bool { + match self { + InnerSas::Created(s) => s.started_from_request, + InnerSas::Started(s) => s.started_from_request, + InnerSas::Accepted(s) => s.started_from_request, + InnerSas::KeyReceived(s) => s.started_from_request, + InnerSas::Confirmed(s) => s.started_from_request, + InnerSas::MacReceived(s) => s.started_from_request, + InnerSas::WaitingForDone(s) => s.started_from_request, + InnerSas::Done(s) => s.started_from_request, + InnerSas::Cancelled(s) => s.started_from_request, + } + } + pub fn supports_emoji(&self) -> bool { match self { InnerSas::Created(_) => false, diff --git a/matrix_sdk_crypto/src/verification/sas/mod.rs b/matrix_sdk_crypto/src/verification/sas/mod.rs index d8b0330f..46487ef1 100644 --- a/matrix_sdk_crypto/src/verification/sas/mod.rs +++ b/matrix_sdk_crypto/src/verification/sas/mod.rs @@ -49,8 +49,8 @@ use crate::{ ReadOnlyAccount, ToDeviceRequest, }; -#[derive(Clone, Debug)] /// Short authentication string object. +#[derive(Clone, Debug)] pub struct Sas { inner: Arc>, account: ReadOnlyAccount, @@ -95,6 +95,16 @@ impl Sas { self.inner.lock().unwrap().supports_emoji() } + /// Did this verification flow start from a verification request. + pub fn started_from_request(&self) -> bool { + self.inner.lock().unwrap().started_from_request() + } + + /// Is this a verification that is veryfying one of our own devices. + pub fn is_self_verification(&self) -> bool { + self.identities_being_verified.is_self_verification() + } + #[cfg(test)] #[allow(dead_code)] pub(crate) fn set_creation_time(&self, time: Instant) {