From 21b0afe72cdad3c8e0208166f98e50d608980032 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 29 Jul 2020 14:19:47 +0200 Subject: [PATCH] matrix-sdk: Add a Sas wrapper. --- matrix_sdk/src/client.rs | 19 +++++ matrix_sdk/src/lib.rs | 5 ++ matrix_sdk/src/sas.rs | 69 +++++++++++++++++++ matrix_sdk_crypto/src/verification/machine.rs | 2 + matrix_sdk_crypto/src/verification/sas.rs | 2 +- 5 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 matrix_sdk/src/sas.rs diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index 9ddf3f07..97e7a00f 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -46,6 +46,9 @@ use crate::Endpoint; #[cfg(feature = "encryption")] use crate::identifiers::DeviceId; +#[cfg(feature = "encryption")] +use crate::Sas; + use crate::api; use crate::http_client::HttpClient; #[cfg(not(target_arch = "wasm32"))] @@ -1436,6 +1439,22 @@ impl Client { Ok(response) } + + #[cfg(feature = "encryption")] + #[cfg_attr(docsrs, doc(cfg(feature = "encryption")))] + #[instrument] + /// Get a `Sas` verification object with the given flow id. + pub async fn get_verification(&self, flow_id: &str) -> Option { + self.base_client + .get_verification(flow_id) + .await + .map(|sas| Sas { + inner: sas, + session: self.base_client.session().clone(), + http_client: self.http_client.clone(), + homeserver: self.homeserver.clone(), + }) + } } #[cfg(test)] diff --git a/matrix_sdk/src/lib.rs b/matrix_sdk/src/lib.rs index a5177891..1d547908 100644 --- a/matrix_sdk/src/lib.rs +++ b/matrix_sdk/src/lib.rs @@ -53,11 +53,16 @@ mod client; mod error; mod http_client; mod request_builder; +#[cfg(feature = "encryption")] +mod sas; + pub use client::{Client, ClientConfig, SyncSettings}; pub use error::{Error, Result}; pub use request_builder::{ MessagesRequestBuilder, RegistrationBuilder, RoomBuilder, RoomListFilterBuilder, }; +#[cfg(feature = "encryption")] +pub use sas::Sas; #[cfg(not(target_arch = "wasm32"))] pub(crate) const VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/matrix_sdk/src/sas.rs b/matrix_sdk/src/sas.rs new file mode 100644 index 00000000..05ee3d5b --- /dev/null +++ b/matrix_sdk/src/sas.rs @@ -0,0 +1,69 @@ +// Copyright 2020 Damir Jelić +// Copyright 2020 The Matrix.org Foundation C.I.C. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::sync::Arc; + +use url::Url; + +use matrix_sdk_base::{Sas as BaseSas, Session}; +use matrix_sdk_common::locks::RwLock; + +use crate::{error::Result, http_client::HttpClient}; + +#[allow(dead_code)] +#[derive(Debug, Clone)] +/// An object controling the interactive verification flow. +pub struct Sas { + pub(crate) inner: BaseSas, + pub(crate) homeserver: Arc, + pub(crate) http_client: HttpClient, + pub(crate) session: Arc>>, +} + +impl Sas { + /// Accept the interactive verification flow. + pub async fn accept(&self) -> Result<()> { + if let Some(request) = self.inner.accept() { + self.http_client.send(request, self.session.clone()).await?; + } + Ok(()) + } + + /// Confirm that the short auth strings match on both sides. + pub async fn confirm(&self) -> Result<()> { + if let Some(request) = self.inner.confirm() { + self.http_client.send(request, self.session.clone()).await?; + } + Ok(()) + } + + /// Cancel the interactive verification flow. + pub async fn cancel(&self) -> Result<()> { + if let Some(request) = self.inner.cancel() { + self.http_client.send(request, self.session.clone()).await?; + } + Ok(()) + } + + /// Get the emoji version of the short auth string. + pub fn emoji(&self) -> Option> { + self.inner.emoji() + } + + /// Get the decimal version of the short auth string. + pub fn decimals(&self) -> Option<(u32, u32, u32)> { + self.inner.decimals() + } +} diff --git a/matrix_sdk_crypto/src/verification/machine.rs b/matrix_sdk_crypto/src/verification/machine.rs index ef64ad41..72639203 100644 --- a/matrix_sdk_crypto/src/verification/machine.rs +++ b/matrix_sdk_crypto/src/verification/machine.rs @@ -47,6 +47,7 @@ impl VerificationMachine { } pub fn get_sas(&self, transaction_id: &str) -> Option { + #[allow(clippy::map_clone)] self.verifications.get(transaction_id).map(|s| s.clone()) } @@ -73,6 +74,7 @@ impl VerificationMachine { } pub fn outgoing_to_device_requests(&self) -> Vec { + #[allow(clippy::map_clone)] self.outgoing_to_device_messages .iter() .map(|r| r.clone()) diff --git a/matrix_sdk_crypto/src/verification/sas.rs b/matrix_sdk_crypto/src/verification/sas.rs index ad82dd3e..226fadda 100644 --- a/matrix_sdk_crypto/src/verification/sas.rs +++ b/matrix_sdk_crypto/src/verification/sas.rs @@ -1099,7 +1099,7 @@ mod test { use crate::verification::test::{get_content_from_request, wrap_any_to_device_content}; use crate::{Account, Device}; - use matrix_sdk_common::events::{AnyToDeviceEvent, EventContent, ToDeviceEvent}; + use matrix_sdk_common::events::{EventContent, ToDeviceEvent}; use matrix_sdk_common::identifiers::{DeviceId, UserId}; use super::{Accepted, Created, Sas, SasState, Started};