2020-07-29 12:19:47 +00:00
|
|
|
// 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.
|
|
|
|
|
2021-03-11 20:10:26 +00:00
|
|
|
use matrix_sdk_base::crypto::{
|
|
|
|
AcceptSettings, OutgoingVerificationRequest, ReadOnlyDevice, Sas as BaseSas,
|
|
|
|
};
|
2020-07-29 12:19:47 +00:00
|
|
|
|
2020-12-15 15:35:54 +00:00
|
|
|
use crate::{error::Result, Client};
|
2020-07-29 12:19:47 +00:00
|
|
|
|
2021-05-31 07:35:19 +00:00
|
|
|
/// An object controlling the interactive verification flow.
|
2020-08-10 13:21:32 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2020-07-29 12:19:47 +00:00
|
|
|
pub struct Sas {
|
|
|
|
pub(crate) inner: BaseSas,
|
2020-12-15 15:35:54 +00:00
|
|
|
pub(crate) client: Client,
|
2020-07-29 12:19:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Sas {
|
|
|
|
/// Accept the interactive verification flow.
|
|
|
|
pub async fn accept(&self) -> Result<()> {
|
2021-03-11 20:10:26 +00:00
|
|
|
self.accept_with_settings(Default::default()).await
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Accept the interactive verification flow with specific settings.
|
|
|
|
///
|
2021-03-12 14:45:58 +00:00
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `settings` - specific customizations to the verification flow.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2021-03-23 13:30:31 +00:00
|
|
|
/// ```no_run
|
|
|
|
/// # use matrix_sdk::Client;
|
|
|
|
/// # use futures::executor::block_on;
|
|
|
|
/// # use url::Url;
|
2021-03-12 14:45:58 +00:00
|
|
|
/// use matrix_sdk::Sas;
|
|
|
|
/// use matrix_sdk_base::crypto::AcceptSettings;
|
|
|
|
/// use matrix_sdk::events::key::verification::ShortAuthenticationString;
|
2021-03-23 13:30:31 +00:00
|
|
|
/// # let homeserver = Url::parse("http://example.com").unwrap();
|
|
|
|
/// # let client = Client::new(homeserver).unwrap();
|
|
|
|
/// # let flow_id = "someID";
|
|
|
|
/// # block_on(async {
|
|
|
|
/// let sas = client.get_verification(flow_id).await.unwrap();
|
2021-03-12 14:45:58 +00:00
|
|
|
///
|
|
|
|
/// let only_decimal = AcceptSettings::with_allowed_methods(
|
|
|
|
/// vec![ShortAuthenticationString::Decimal]
|
|
|
|
/// );
|
2021-03-23 13:30:31 +00:00
|
|
|
/// sas.accept_with_settings(only_decimal).await.unwrap();
|
|
|
|
/// # });
|
2021-03-12 14:45:58 +00:00
|
|
|
/// ```
|
2021-03-11 20:10:26 +00:00
|
|
|
pub async fn accept_with_settings(&self, settings: AcceptSettings) -> Result<()> {
|
|
|
|
if let Some(req) = self.inner.accept_with_settings(settings) {
|
2020-12-15 15:35:54 +00:00
|
|
|
match req {
|
|
|
|
OutgoingVerificationRequest::ToDevice(r) => {
|
|
|
|
self.client.send_to_device(&r).await?;
|
|
|
|
}
|
2021-05-07 14:57:52 +00:00
|
|
|
OutgoingVerificationRequest::InRoom(r) => {
|
|
|
|
self.client.room_send_helper(&r).await?;
|
|
|
|
}
|
2020-12-15 15:35:54 +00:00
|
|
|
}
|
2020-07-29 12:19:47 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Confirm that the short auth strings match on both sides.
|
|
|
|
pub async fn confirm(&self) -> Result<()> {
|
2020-12-17 11:15:11 +00:00
|
|
|
let (request, signature) = self.inner.confirm().await?;
|
2020-10-27 15:39:23 +00:00
|
|
|
|
2020-12-17 11:15:11 +00:00
|
|
|
match request {
|
|
|
|
Some(OutgoingVerificationRequest::InRoom(r)) => {
|
|
|
|
self.client.room_send_helper(&r).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(OutgoingVerificationRequest::ToDevice(r)) => {
|
|
|
|
self.client.send_to_device(&r).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
None => (),
|
2020-07-29 12:19:47 +00:00
|
|
|
}
|
2020-08-03 10:38:43 +00:00
|
|
|
|
2020-10-27 15:39:23 +00:00
|
|
|
if let Some(s) = signature {
|
2021-02-01 16:15:29 +00:00
|
|
|
self.client.send(s, None).await?;
|
2020-10-27 15:39:23 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 12:19:47 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Cancel the interactive verification flow.
|
|
|
|
pub async fn cancel(&self) -> Result<()> {
|
2020-12-15 15:35:54 +00:00
|
|
|
if let Some(request) = self.inner.cancel() {
|
2020-12-17 11:15:11 +00:00
|
|
|
match request {
|
|
|
|
OutgoingVerificationRequest::ToDevice(r) => {
|
|
|
|
self.client.send_to_device(&r).await?;
|
|
|
|
}
|
|
|
|
OutgoingVerificationRequest::InRoom(r) => {
|
|
|
|
self.client.room_send_helper(&r).await?;
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 12:19:47 +00:00
|
|
|
}
|
2020-12-15 15:35:54 +00:00
|
|
|
|
2020-07-29 12:19:47 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the emoji version of the short auth string.
|
2021-05-07 15:01:53 +00:00
|
|
|
pub fn emoji(&self) -> Option<[(&'static str, &'static str); 7]> {
|
2020-07-29 12:19:47 +00:00
|
|
|
self.inner.emoji()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the decimal version of the short auth string.
|
2020-08-03 15:22:44 +00:00
|
|
|
pub fn decimals(&self) -> Option<(u16, u16, u16)> {
|
2020-07-29 12:19:47 +00:00
|
|
|
self.inner.decimals()
|
|
|
|
}
|
2020-08-03 12:33:15 +00:00
|
|
|
|
2021-05-12 12:30:36 +00:00
|
|
|
/// Does this verification flow support emoji for the short authentication
|
|
|
|
/// string.
|
|
|
|
pub fn supports_emoji(&self) -> bool {
|
|
|
|
self.inner.supports_emoji()
|
|
|
|
}
|
|
|
|
|
2020-08-03 12:33:15 +00:00
|
|
|
/// Is the verification process done.
|
|
|
|
pub fn is_done(&self) -> bool {
|
|
|
|
self.inner.is_done()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Is the verification process canceled.
|
2021-05-26 09:54:22 +00:00
|
|
|
pub fn is_cancelled(&self) -> bool {
|
|
|
|
self.inner.is_cancelled()
|
2020-08-03 12:33:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the other users device that we're veryfying.
|
2021-05-31 14:31:35 +00:00
|
|
|
pub fn other_device(&self) -> &ReadOnlyDevice {
|
2020-08-03 12:33:15 +00:00
|
|
|
self.inner.other_device()
|
|
|
|
}
|
2020-07-29 12:19:47 +00:00
|
|
|
}
|