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.
|
|
|
|
|
2020-08-17 14:17:28 +00:00
|
|
|
use matrix_sdk_base::{ReadOnlyDevice, Sas as BaseSas};
|
2020-08-17 13:29:07 +00:00
|
|
|
use matrix_sdk_common::api::r0::to_device::send_event_to_device::Request as ToDeviceRequest;
|
2020-07-29 12:19:47 +00:00
|
|
|
|
|
|
|
use crate::{error::Result, http_client::HttpClient};
|
|
|
|
|
|
|
|
/// An object controling 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,
|
|
|
|
pub(crate) http_client: HttpClient,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sas {
|
|
|
|
/// Accept the interactive verification flow.
|
|
|
|
pub async fn accept(&self) -> Result<()> {
|
2020-08-10 10:39:00 +00:00
|
|
|
if let Some(req) = self.inner.accept() {
|
|
|
|
let request = ToDeviceRequest {
|
|
|
|
event_type: req.event_type,
|
|
|
|
txn_id: &req.txn_id,
|
|
|
|
messages: req.messages,
|
|
|
|
};
|
|
|
|
|
2020-08-17 13:29:07 +00:00
|
|
|
self.http_client.send(request).await?;
|
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-08-10 10:39:00 +00:00
|
|
|
if let Some(req) = self.inner.confirm().await? {
|
|
|
|
let request = ToDeviceRequest {
|
|
|
|
event_type: req.event_type,
|
|
|
|
txn_id: &req.txn_id,
|
|
|
|
messages: req.messages,
|
|
|
|
};
|
|
|
|
|
2020-08-17 13:29:07 +00:00
|
|
|
self.http_client.send(request).await?;
|
2020-07-29 12:19:47 +00:00
|
|
|
}
|
2020-08-03 10:38:43 +00:00
|
|
|
|
2020-07-29 12:19:47 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Cancel the interactive verification flow.
|
|
|
|
pub async fn cancel(&self) -> Result<()> {
|
2020-08-21 14:26:34 +00:00
|
|
|
if let Some((_, req)) = self.inner.cancel() {
|
2020-08-10 10:39:00 +00:00
|
|
|
let request = ToDeviceRequest {
|
|
|
|
event_type: req.event_type,
|
|
|
|
txn_id: &req.txn_id,
|
|
|
|
messages: req.messages,
|
|
|
|
};
|
|
|
|
|
2020-08-17 13:29:07 +00:00
|
|
|
self.http_client.send(request).await?;
|
2020-07-29 12:19:47 +00:00
|
|
|
}
|
|
|
|
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.
|
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
|
|
|
|
|
|
|
/// Is the verification process done.
|
|
|
|
pub fn is_done(&self) -> bool {
|
|
|
|
self.inner.is_done()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Is the verification process canceled.
|
|
|
|
pub fn is_canceled(&self) -> bool {
|
|
|
|
self.inner.is_canceled()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the other users device that we're veryfying.
|
2020-08-17 14:17:28 +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
|
|
|
}
|