diff --git a/matrix_sdk/src/sas.rs b/matrix_sdk/src/sas.rs index 1adf963e..62474f8f 100644 --- a/matrix_sdk/src/sas.rs +++ b/matrix_sdk/src/sas.rs @@ -133,8 +133,8 @@ impl Sas { } /// Is the verification process canceled. - pub fn is_canceled(&self) -> bool { - self.inner.is_canceled() + pub fn is_cancelled(&self) -> bool { + self.inner.is_cancelled() } /// Get the other users device that we're veryfying. diff --git a/matrix_sdk_crypto/src/verification/machine.rs b/matrix_sdk_crypto/src/verification/machine.rs index 7e24b6de..691c8bcb 100644 --- a/matrix_sdk_crypto/src/verification/machine.rs +++ b/matrix_sdk_crypto/src/verification/machine.rs @@ -61,8 +61,8 @@ impl VerificationCache { } pub fn garbage_collect(&self) -> Vec { - self.sas_verification.retain(|_, s| !(s.is_done() || s.is_canceled())); - self.room_sas_verifications.retain(|_, s| !(s.is_done() || s.is_canceled())); + self.sas_verification.retain(|_, s| !(s.is_done() || s.is_cancelled())); + self.room_sas_verifications.retain(|_, s| !(s.is_done() || s.is_cancelled())); let mut requests: Vec = self .sas_verification diff --git a/matrix_sdk_crypto/src/verification/mod.rs b/matrix_sdk_crypto/src/verification/mod.rs index 4e014bd0..1efdb1cd 100644 --- a/matrix_sdk_crypto/src/verification/mod.rs +++ b/matrix_sdk_crypto/src/verification/mod.rs @@ -17,10 +17,70 @@ mod requests; mod sas; pub use machine::{VerificationCache, VerificationMachine}; -use matrix_sdk_common::identifiers::{EventId, RoomId}; +use matrix_sdk_common::{ + events::key::verification::{ + cancel::{CancelCode, CancelEventContent, CancelToDeviceEventContent}, + Relation, + }, + identifiers::{EventId, RoomId}, +}; pub use requests::VerificationRequest; pub use sas::{AcceptSettings, Sas, VerificationResult}; +use self::sas::CancelContent; + +#[derive(Clone, Debug)] +pub struct Cancelled { + cancel_code: CancelCode, + reason: &'static str, +} + +impl Cancelled { + fn new(code: CancelCode) -> Self { + let reason = match code { + CancelCode::Accepted => { + "A m.key.verification.request was accepted by a different device." + } + CancelCode::InvalidMessage => "The received message was invalid.", + CancelCode::KeyMismatch => "The expected key did not match the verified one", + CancelCode::Timeout => "The verification process timed out.", + CancelCode::UnexpectedMessage => "The device received an unexpected message.", + CancelCode::UnknownMethod => { + "The device does not know how to handle the requested method." + } + CancelCode::UnknownTransaction => { + "The device does not know about the given transaction ID." + } + CancelCode::User => "The user cancelled the verification.", + CancelCode::UserMismatch => "The expected user did not match the verified user", + _ => unimplemented!(), + }; + + Self { cancel_code: code, reason } + } + + pub fn as_content(&self, flow_id: &FlowId) -> CancelContent { + match flow_id { + FlowId::ToDevice(s) => CancelToDeviceEventContent::new( + s.clone(), + self.reason.to_string(), + self.cancel_code.clone(), + ) + .into(), + + FlowId::InRoom(r, e) => ( + r.clone(), + CancelEventContent::new( + self.reason.to_string(), + self.cancel_code.clone(), + Relation::new(e.clone()), + ), + ) + .into(), + } + } +} + #[derive(Clone, Debug, Hash, PartialEq, PartialOrd)] pub enum FlowId { ToDevice(String), diff --git a/matrix_sdk_crypto/src/verification/requests.rs b/matrix_sdk_crypto/src/verification/requests.rs index 3e8ebb6e..5be97bc7 100644 --- a/matrix_sdk_crypto/src/verification/requests.rs +++ b/matrix_sdk_crypto/src/verification/requests.rs @@ -848,7 +848,7 @@ mod test { let content = StartContent::try_from(&start_content).unwrap(); let alice_sas = alice_request.into_started_sas(content, bob_device, None).unwrap(); - assert!(!bob_sas.is_canceled()); - assert!(!alice_sas.is_canceled()); + assert!(!bob_sas.is_cancelled()); + assert!(!alice_sas.is_cancelled()); } } diff --git a/matrix_sdk_crypto/src/verification/sas/inner_sas.rs b/matrix_sdk_crypto/src/verification/sas/inner_sas.rs index 73662d4c..3da40092 100644 --- a/matrix_sdk_crypto/src/verification/sas/inner_sas.rs +++ b/matrix_sdk_crypto/src/verification/sas/inner_sas.rs @@ -27,13 +27,14 @@ use matrix_sdk_common::{ use super::{ event_enums::{AcceptContent, CancelContent, MacContent, OutgoingContent}, sas_state::{ - Accepted, Canceled, Confirmed, Created, Done, KeyReceived, MacReceived, SasState, Started, + Accepted, Confirmed, Created, Done, KeyReceived, MacReceived, SasState, Started, WaitingForDone, }, FlowId, StartContent, }; use crate::{ identities::{ReadOnlyDevice, UserIdentities}, + verification::Cancelled, ReadOnlyAccount, }; @@ -48,7 +49,7 @@ pub enum InnerSas { WaitingForDone(SasState), WaitingForDoneUnconfirmed(SasState), Done(SasState), - Canceled(SasState), + Cancelled(SasState), } impl InnerSas { @@ -90,7 +91,7 @@ impl InnerSas { InnerSas::WaitingForDone(_) => false, InnerSas::WaitingForDoneUnconfirmed(_) => false, InnerSas::Done(_) => false, - InnerSas::Canceled(_) => false, + InnerSas::Cancelled(_) => false, } } @@ -139,7 +140,7 @@ impl InnerSas { match self { InnerSas::Created(s) => s.set_creation_time(time), InnerSas::Started(s) => s.set_creation_time(time), - InnerSas::Canceled(s) => s.set_creation_time(time), + InnerSas::Cancelled(s) => s.set_creation_time(time), InnerSas::Accepted(s) => s.set_creation_time(time), InnerSas::KeyRecieved(s) => s.set_creation_time(time), InnerSas::Confirmed(s) => s.set_creation_time(time), @@ -162,7 +163,7 @@ impl InnerSas { let content = sas.as_content(); - (InnerSas::Canceled(sas), Some(content)) + (InnerSas::Cancelled(sas), Some(content)) } pub fn confirm(self) -> (InnerSas, Option) { @@ -201,7 +202,7 @@ impl InnerSas { Ok(s) => (InnerSas::KeyRecieved(s), None), Err(s) => { let content = s.as_content(); - (InnerSas::Canceled(s), Some(content.into())) + (InnerSas::Cancelled(s), Some(content.into())) } } } @@ -213,7 +214,7 @@ impl InnerSas { } Err(s) => { let content = s.as_content(); - (InnerSas::Canceled(s), Some(content.into())) + (InnerSas::Cancelled(s), Some(content.into())) } } } @@ -226,7 +227,7 @@ impl InnerSas { Ok(s) => (InnerSas::MacReceived(s), None), Err(s) => { let content = s.as_content(); - (InnerSas::Canceled(s), Some(content.into())) + (InnerSas::Cancelled(s), Some(content.into())) } } } @@ -239,7 +240,7 @@ impl InnerSas { } Err(s) => { let content = s.as_content(); - (InnerSas::Canceled(s), Some(content.into())) + (InnerSas::Cancelled(s), Some(content.into())) } } } @@ -251,7 +252,7 @@ impl InnerSas { Ok(s) => (InnerSas::Done(s), None), Err(s) => { let content = s.as_content(); - (InnerSas::Canceled(s), Some(content.into())) + (InnerSas::Cancelled(s), Some(content.into())) } } } @@ -263,7 +264,7 @@ impl InnerSas { } Err(s) => { let content = s.as_content(); - (InnerSas::Canceled(s), Some(content.into())) + (InnerSas::Cancelled(s), Some(content.into())) } } } @@ -285,7 +286,7 @@ impl InnerSas { } Err(s) => { let content = s.as_content(); - (InnerSas::Canceled(s), Some(content.into())) + (InnerSas::Cancelled(s), Some(content.into())) } } } else { @@ -297,7 +298,7 @@ impl InnerSas { Ok(s) => (InnerSas::KeyRecieved(s), None), Err(s) => { let content = s.as_content(); - (InnerSas::Canceled(s), Some(content.into())) + (InnerSas::Cancelled(s), Some(content.into())) } }, InnerSas::Started(s) => match s.into_key_received(&e.sender, e.content.clone()) { @@ -307,7 +308,7 @@ impl InnerSas { } Err(s) => { let content = s.as_content(); - (InnerSas::Canceled(s), Some(content.into())) + (InnerSas::Cancelled(s), Some(content.into())) } }, _ => (self, None), @@ -318,7 +319,7 @@ impl InnerSas { Ok(s) => (InnerSas::MacReceived(s), None), Err(s) => { let content = s.as_content(); - (InnerSas::Canceled(s), Some(content.into())) + (InnerSas::Cancelled(s), Some(content.into())) } } } @@ -326,7 +327,7 @@ impl InnerSas { Ok(s) => (InnerSas::Done(s), None), Err(s) => { let content = s.as_content(); - (InnerSas::Canceled(s), Some(content.into())) + (InnerSas::Cancelled(s), Some(content.into())) } }, _ => (self, None), @@ -343,15 +344,15 @@ impl InnerSas { matches!(self, InnerSas::Done(_)) } - pub fn is_canceled(&self) -> bool { - matches!(self, InnerSas::Canceled(_)) + pub fn is_cancelled(&self) -> bool { + matches!(self, InnerSas::Cancelled(_)) } pub fn timed_out(&self) -> bool { match self { InnerSas::Created(s) => s.timed_out(), InnerSas::Started(s) => s.timed_out(), - InnerSas::Canceled(s) => s.timed_out(), + InnerSas::Cancelled(s) => s.timed_out(), InnerSas::Accepted(s) => s.timed_out(), InnerSas::KeyRecieved(s) => s.timed_out(), InnerSas::Confirmed(s) => s.timed_out(), @@ -366,7 +367,7 @@ impl InnerSas { match self { InnerSas::Created(s) => s.verification_flow_id.clone(), InnerSas::Started(s) => s.verification_flow_id.clone(), - InnerSas::Canceled(s) => s.verification_flow_id.clone(), + InnerSas::Cancelled(s) => s.verification_flow_id.clone(), InnerSas::Accepted(s) => s.verification_flow_id.clone(), InnerSas::KeyRecieved(s) => s.verification_flow_id.clone(), InnerSas::Confirmed(s) => s.verification_flow_id.clone(), diff --git a/matrix_sdk_crypto/src/verification/sas/mod.rs b/matrix_sdk_crypto/src/verification/sas/mod.rs index 615ac76b..5773ef89 100644 --- a/matrix_sdk_crypto/src/verification/sas/mod.rs +++ b/matrix_sdk_crypto/src/verification/sas/mod.rs @@ -559,7 +559,7 @@ impl Sas { } pub(crate) fn cancel_if_timed_out(&self) -> Option { - if self.is_canceled() || self.is_done() { + if self.is_cancelled() || self.is_done() { None } else if self.timed_out() { let mut guard = self.inner.lock().unwrap(); @@ -598,8 +598,8 @@ impl Sas { } /// Is the SAS flow canceled. - pub fn is_canceled(&self) -> bool { - self.inner.lock().unwrap().is_canceled() + pub fn is_cancelled(&self) -> bool { + self.inner.lock().unwrap().is_cancelled() } /// Get the emoji version of the short auth string. diff --git a/matrix_sdk_crypto/src/verification/sas/sas_state.rs b/matrix_sdk_crypto/src/verification/sas/sas_state.rs index ec8ea413..80d7f6b3 100644 --- a/matrix_sdk_crypto/src/verification/sas/sas_state.rs +++ b/matrix_sdk_crypto/src/verification/sas/sas_state.rs @@ -25,7 +25,7 @@ use matrix_sdk_common::{ AcceptEventContent, AcceptMethod, AcceptToDeviceEventContent, SasV1Content as AcceptV1Content, SasV1ContentInit as AcceptV1ContentInit, }, - cancel::{CancelCode, CancelEventContent, CancelToDeviceEventContent}, + cancel::CancelCode, done::DoneEventContent, key::{KeyEventContent, KeyToDeviceEventContent}, start::{ @@ -52,7 +52,7 @@ use super::{ }; use crate::{ identities::{ReadOnlyDevice, UserIdentities}, - verification::FlowId, + verification::{Cancelled, FlowId}, ReadOnlyAccount, }; @@ -281,12 +281,6 @@ pub struct Done { verified_master_keys: Arc<[UserIdentities]>, } -#[derive(Clone, Debug)] -pub struct Canceled { - cancel_code: CancelCode, - reason: &'static str, -} - impl SasState { /// Get our own user id. #[cfg(test)] @@ -304,14 +298,14 @@ impl SasState { self.ids.other_device.clone() } - pub fn cancel(self, cancel_code: CancelCode) -> SasState { + pub fn cancel(self, cancel_code: CancelCode) -> SasState { SasState { inner: self.inner, ids: self.ids, creation_time: self.creation_time, last_event_time: self.last_event_time, verification_flow_id: self.verification_flow_id, - state: Arc::new(Canceled::new(cancel_code)), + state: Arc::new(Cancelled::new(cancel_code)), } } @@ -448,7 +442,7 @@ impl SasState { self, sender: &UserId, content: impl Into, - ) -> Result, SasState> { + ) -> Result, SasState> { let content = content.into(); self.check_event(&sender, content.flow_id().as_str()) .map_err(|c| self.clone().cancel(c))?; @@ -496,7 +490,7 @@ impl SasState { other_device: ReadOnlyDevice, other_identity: Option, content: impl Into, - ) -> Result, SasState> { + ) -> Result, SasState> { Self::from_start_helper(account, other_device, other_identity, &content.into()) } @@ -505,7 +499,7 @@ impl SasState { other_device: ReadOnlyDevice, other_identity: Option, content: &StartContent, - ) -> Result, SasState> { + ) -> Result, SasState> { let canceled = || SasState { inner: Arc::new(Mutex::new(OlmSas::new())), @@ -519,7 +513,7 @@ impl SasState { }, verification_flow_id: content.flow_id().into(), - state: Arc::new(Canceled::new(CancelCode::UnknownMethod)), + state: Arc::new(Cancelled::new(CancelCode::UnknownMethod)), }; if let StartMethod::SasV1(method_content) = content.method() { @@ -608,7 +602,7 @@ impl SasState { self, sender: &UserId, content: impl Into, - ) -> Result, SasState> { + ) -> Result, SasState> { let content = content.into(); self.check_event(&sender, &content.flow_id().as_str()) @@ -650,7 +644,7 @@ impl SasState { self, sender: &UserId, content: impl Into, - ) -> Result, SasState> { + ) -> Result, SasState> { let content = content.into(); self.check_event(&sender, content.flow_id().as_str()) @@ -783,7 +777,7 @@ impl SasState { self, sender: &UserId, content: impl Into, - ) -> Result, SasState> { + ) -> Result, SasState> { let content = content.into(); self.check_event(&sender, content.flow_id().as_str()) @@ -844,7 +838,7 @@ impl SasState { self, sender: &UserId, content: impl Into, - ) -> Result, SasState> { + ) -> Result, SasState> { let content = content.into(); self.check_event(&sender, &content.flow_id().as_str()) @@ -886,7 +880,7 @@ impl SasState { self, sender: &UserId, content: impl Into, - ) -> Result, SasState> { + ) -> Result, SasState> { let content = content.into(); self.check_event(&sender, &content.flow_id().as_str()) @@ -1036,7 +1030,7 @@ impl SasState { self, sender: &UserId, content: impl Into, - ) -> Result, SasState> { + ) -> Result, SasState> { let content = content.into(); self.check_event(&sender, &content.flow_id().as_str()) @@ -1088,51 +1082,9 @@ impl SasState { } } -impl Canceled { - fn new(code: CancelCode) -> Canceled { - let reason = match code { - CancelCode::Accepted => { - "A m.key.verification.request was accepted by a different device." - } - CancelCode::InvalidMessage => "The received message was invalid.", - CancelCode::KeyMismatch => "The expected key did not match the verified one", - CancelCode::Timeout => "The verification process timed out.", - CancelCode::UnexpectedMessage => "The device received an unexpected message.", - CancelCode::UnknownMethod => { - "The device does not know how to handle the requested method." - } - CancelCode::UnknownTransaction => { - "The device does not know about the given transaction ID." - } - CancelCode::User => "The user cancelled the verification.", - CancelCode::UserMismatch => "The expected user did not match the verified user", - _ => unimplemented!(), - }; - - Canceled { cancel_code: code, reason } - } -} - -impl SasState { +impl SasState { pub fn as_content(&self) -> CancelContent { - match self.verification_flow_id.as_ref() { - FlowId::ToDevice(s) => CancelToDeviceEventContent::new( - s.clone(), - self.state.reason.to_string(), - self.state.cancel_code.clone(), - ) - .into(), - - FlowId::InRoom(r, e) => ( - r.clone(), - CancelEventContent::new( - self.state.reason.to_string(), - self.state.cancel_code.clone(), - Relation::new(e.clone()), - ), - ) - .into(), - } + self.state.as_content(&self.verification_flow_id) } }