From 113587247eed9a20aec2835c1faed97767ccbb00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 28 Jun 2021 16:30:41 +0200 Subject: [PATCH] crypto: Notify our users that a verification timed out on our end --- matrix_sdk_crypto/src/machine.rs | 4 +-- .../src/verification/event_enums.rs | 6 ++--- matrix_sdk_crypto/src/verification/machine.rs | 26 ++++++++++++++++--- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/matrix_sdk_crypto/src/machine.rs b/matrix_sdk_crypto/src/machine.rs index 75b1fd40..68b4426b 100644 --- a/matrix_sdk_crypto/src/machine.rs +++ b/matrix_sdk_crypto/src/machine.rs @@ -789,7 +789,7 @@ impl OlmMachine { one_time_keys_counts: &BTreeMap, ) -> OlmResult { // Remove verification objects that have expired or are done. - self.verification_machine.garbage_collect(); + let mut events = self.verification_machine.garbage_collect(); // Always save the account, a new session might get created which also // touches the account. @@ -804,8 +804,6 @@ impl OlmMachine { } } - let mut events = Vec::new(); - for mut raw_event in to_device_events.events { let event = match raw_event.deserialize() { Ok(e) => e, diff --git a/matrix_sdk_crypto/src/verification/event_enums.rs b/matrix_sdk_crypto/src/verification/event_enums.rs index 39b9d195..9c63f540 100644 --- a/matrix_sdk_crypto/src/verification/event_enums.rs +++ b/matrix_sdk_crypto/src/verification/event_enums.rs @@ -666,7 +666,8 @@ impl From<(RoomId, AnyMessageEventContent)> for OutgoingContent { } #[cfg(test)] -use crate::{OutgoingRequest, OutgoingVerificationRequest, RoomMessageRequest, ToDeviceRequest}; +use crate::OutgoingVerificationRequest; +use crate::{OutgoingRequest, RoomMessageRequest, ToDeviceRequest}; #[cfg(test)] impl From for OutgoingContent { @@ -678,14 +679,12 @@ impl From for OutgoingContent { } } -#[cfg(test)] impl From for OutgoingContent { fn from(value: RoomMessageRequest) -> Self { (value.room_id, value.content).into() } } -#[cfg(test)] impl TryFrom for OutgoingContent { type Error = String; @@ -736,7 +735,6 @@ impl TryFrom for OutgoingContent { } } -#[cfg(test)] impl TryFrom for OutgoingContent { type Error = String; diff --git a/matrix_sdk_crypto/src/verification/machine.rs b/matrix_sdk_crypto/src/verification/machine.rs index a2a4abab..330f02cc 100644 --- a/matrix_sdk_crypto/src/verification/machine.rs +++ b/matrix_sdk_crypto/src/verification/machine.rs @@ -12,11 +12,18 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::{convert::TryFrom, sync::Arc}; +use std::{ + convert::{TryFrom, TryInto}, + sync::Arc, +}; use dashmap::DashMap; use matrix_sdk_common::{locks::Mutex, uuid::Uuid}; -use ruma::{DeviceId, MilliSecondsSinceUnixEpoch, UserId}; +use ruma::{ + events::{AnyToDeviceEvent, AnyToDeviceEventContent, ToDeviceEvent}, + serde::Raw, + DeviceId, MilliSecondsSinceUnixEpoch, UserId, +}; use tracing::{info, trace, warn}; use super::{ @@ -165,15 +172,28 @@ impl VerificationMachine { self.verifications.outgoing_requests() } - pub fn garbage_collect(&self) { + pub fn garbage_collect(&self) -> Vec> { + let mut events = vec![]; + for user_verification in self.requests.iter() { user_verification.retain(|_, v| !(v.is_done() || v.is_cancelled())); } self.requests.retain(|_, v| !v.is_empty()); for request in self.verifications.garbage_collect() { + if let Ok(OutgoingContent::ToDevice(AnyToDeviceEventContent::KeyVerificationCancel( + content, + ))) = request.clone().try_into() + { + let event = ToDeviceEvent { content, sender: self.account.user_id().to_owned() }; + + events.push(AnyToDeviceEvent::KeyVerificationCancel(event).into()); + } + self.verifications.add_request(request) } + + events } async fn mark_sas_as_done(