matrix-sdk: Add a Sas wrapper.

master
Damir Jelić 2020-07-29 14:19:47 +02:00
parent a726ebab39
commit 21b0afe72c
5 changed files with 96 additions and 1 deletions

View File

@ -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<Sas> {
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)]

View File

@ -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");

69
matrix_sdk/src/sas.rs Normal file
View File

@ -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<Url>,
pub(crate) http_client: HttpClient,
pub(crate) session: Arc<RwLock<Option<Session>>>,
}
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<Vec<(&'static str, &'static str)>> {
self.inner.emoji()
}
/// Get the decimal version of the short auth string.
pub fn decimals(&self) -> Option<(u32, u32, u32)> {
self.inner.decimals()
}
}

View File

@ -47,6 +47,7 @@ impl VerificationMachine {
}
pub fn get_sas(&self, transaction_id: &str) -> Option<Sas> {
#[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<ToDeviceRequest> {
#[allow(clippy::map_clone)]
self.outgoing_to_device_messages
.iter()
.map(|r| r.clone())

View File

@ -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};