diff --git a/matrix_sdk_crypto/src/verification/event_enums.rs b/matrix_sdk_crypto/src/verification/event_enums.rs index f784df3a..e5fe856d 100644 --- a/matrix_sdk_crypto/src/verification/event_enums.rs +++ b/matrix_sdk_crypto/src/verification/event_enums.rs @@ -39,7 +39,7 @@ use matrix_sdk_common::{ CanonicalJsonValue, }; -use super::{sas::OutgoingContent, FlowId}; +use super::FlowId; #[derive(Debug)] pub enum AnyEvent<'a> { @@ -709,3 +709,127 @@ impl OwnedAcceptContent { } } } + +#[derive(Clone, Debug)] +pub enum OutgoingContent { + Room(RoomId, AnyMessageEventContent), + ToDevice(AnyToDeviceEventContent), +} + +impl From for OutgoingContent { + fn from(content: OwnedStartContent) -> Self { + match content { + OwnedStartContent::Room(r, c) => { + (r, AnyMessageEventContent::KeyVerificationStart(c)).into() + } + OwnedStartContent::ToDevice(c) => { + AnyToDeviceEventContent::KeyVerificationStart(c).into() + } + } + } +} + +impl From for OutgoingContent { + fn from(content: AnyToDeviceEventContent) -> Self { + OutgoingContent::ToDevice(content) + } +} + +impl From<(RoomId, AnyMessageEventContent)> for OutgoingContent { + fn from(content: (RoomId, AnyMessageEventContent)) -> Self { + OutgoingContent::Room(content.0, content.1) + } +} + +#[cfg(test)] +use crate::{OutgoingRequest, OutgoingVerificationRequest, RoomMessageRequest, ToDeviceRequest}; + +#[cfg(test)] +impl From for OutgoingContent { + fn from(request: OutgoingVerificationRequest) -> Self { + match request { + OutgoingVerificationRequest::ToDevice(r) => Self::try_from(r).unwrap(), + OutgoingVerificationRequest::InRoom(r) => Self::from(r), + } + } +} + +#[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 = (); + + fn try_from(value: ToDeviceRequest) -> Result { + use matrix_sdk_common::events::EventType; + use serde_json::Value; + + let json: Value = serde_json::from_str( + value + .messages + .values() + .next() + .and_then(|m| m.values().next().map(|j| j.get())) + .ok_or(())?, + ) + .map_err(|_| ())?; + + match value.event_type { + EventType::KeyVerificationRequest => { + Ok(AnyToDeviceEventContent::KeyVerificationRequest( + serde_json::from_value(json).map_err(|_| ())?, + ) + .into()) + } + EventType::KeyVerificationReady => Ok(AnyToDeviceEventContent::KeyVerificationReady( + serde_json::from_value(json).map_err(|_| ())?, + ) + .into()), + EventType::KeyVerificationDone => Ok(AnyToDeviceEventContent::KeyVerificationDone( + serde_json::from_value(json).map_err(|_| ())?, + ) + .into()), + EventType::KeyVerificationStart => Ok(AnyToDeviceEventContent::KeyVerificationStart( + serde_json::from_value(json).map_err(|_| ())?, + ) + .into()), + EventType::KeyVerificationKey => Ok(AnyToDeviceEventContent::KeyVerificationKey( + serde_json::from_value(json).map_err(|_| ())?, + ) + .into()), + EventType::KeyVerificationAccept => Ok(AnyToDeviceEventContent::KeyVerificationAccept( + serde_json::from_value(json).map_err(|_| ())?, + ) + .into()), + EventType::KeyVerificationMac => Ok(AnyToDeviceEventContent::KeyVerificationMac( + serde_json::from_value(json).map_err(|_| ())?, + ) + .into()), + EventType::KeyVerificationCancel => Ok(AnyToDeviceEventContent::KeyVerificationCancel( + serde_json::from_value(json).map_err(|_| ())?, + ) + .into()), + _ => Err(()), + } + } +} + +#[cfg(test)] +impl TryFrom for OutgoingContent { + type Error = (); + + fn try_from(value: OutgoingRequest) -> Result { + match value.request() { + crate::OutgoingRequests::KeysUpload(_) => Err(()), + crate::OutgoingRequests::KeysQuery(_) => Err(()), + crate::OutgoingRequests::ToDeviceRequest(r) => Self::try_from(r.clone()), + crate::OutgoingRequests::SignatureUpload(_) => Err(()), + crate::OutgoingRequests::RoomMessage(r) => Ok(Self::from(r.clone())), + } + } +} diff --git a/matrix_sdk_crypto/src/verification/machine.rs b/matrix_sdk_crypto/src/verification/machine.rs index 5979ad50..24563150 100644 --- a/matrix_sdk_crypto/src/verification/machine.rs +++ b/matrix_sdk_crypto/src/verification/machine.rs @@ -16,16 +16,16 @@ use std::{convert::TryFrom, sync::Arc}; use dashmap::DashMap; use matrix_sdk_common::{ - identifiers::{DeviceId, EventId, UserId}, + identifiers::{DeviceId, UserId}, locks::Mutex, uuid::Uuid, }; use tracing::{info, warn}; use super::{ - event_enums::{AnyEvent, AnyVerificationContent}, + event_enums::{AnyEvent, AnyVerificationContent, OutgoingContent}, requests::VerificationRequest, - sas::{content_to_request, OutgoingContent, Sas}, + sas::{content_to_request, Sas}, FlowId, Verification, VerificationResult, }; use crate::{ @@ -398,8 +398,7 @@ mod test { olm::PrivateCrossSigningIdentity, store::{CryptoStore, MemoryStore}, verification::{ - event_enums::{AcceptContent, KeyContent, MacContent}, - sas::OutgoingContent, + event_enums::{AcceptContent, KeyContent, MacContent, OutgoingContent}, test::wrap_any_to_device_content, }, ReadOnlyAccount, ReadOnlyDevice, diff --git a/matrix_sdk_crypto/src/verification/mod.rs b/matrix_sdk_crypto/src/verification/mod.rs index a94207f0..2d9bd37f 100644 --- a/matrix_sdk_crypto/src/verification/mod.rs +++ b/matrix_sdk_crypto/src/verification/mod.rs @@ -19,6 +19,7 @@ mod sas; use std::sync::Arc; +use event_enums::OutgoingContent; pub use machine::{VerificationCache, VerificationMachine}; use matrix_sdk_common::{ api::r0::keys::upload_signatures::Request as SignatureUploadRequest, @@ -36,7 +37,6 @@ pub use requests::VerificationRequest; pub use sas::{AcceptSettings, Sas}; use tracing::{error, info, trace, warn}; -use self::sas::OutgoingContent; use crate::{ error::SignatureError, olm::PrivateCrossSigningIdentity, @@ -450,7 +450,7 @@ pub(crate) mod test { }; use serde_json::Value; - use super::sas::OutgoingContent; + use super::event_enums::OutgoingContent; use crate::{ requests::{OutgoingRequest, OutgoingRequests}, OutgoingVerificationRequest, diff --git a/matrix_sdk_crypto/src/verification/requests.rs b/matrix_sdk_crypto/src/verification/requests.rs index 88a30b70..d8e723cf 100644 --- a/matrix_sdk_crypto/src/verification/requests.rs +++ b/matrix_sdk_crypto/src/verification/requests.rs @@ -35,8 +35,8 @@ use matrix_sdk_common::{ use tracing::{info, warn}; use super::{ - event_enums::{ReadyContent, RequestContent, StartContent}, - sas::{content_to_request, OutgoingContent}, + event_enums::{OutgoingContent, ReadyContent, RequestContent, StartContent}, + sas::content_to_request, FlowId, VerificationCache, }; use crate::{ @@ -599,8 +599,7 @@ mod test { olm::{PrivateCrossSigningIdentity, ReadOnlyAccount}, store::{Changes, CryptoStore, MemoryStore}, verification::{ - event_enums::{ReadyContent, StartContent}, - sas::OutgoingContent, + event_enums::{OutgoingContent, ReadyContent, StartContent}, FlowId, VerificationCache, }, ReadOnlyDevice, diff --git a/matrix_sdk_crypto/src/verification/sas/event_enums.rs b/matrix_sdk_crypto/src/verification/sas/event_enums.rs deleted file mode 100644 index 125d1045..00000000 --- a/matrix_sdk_crypto/src/verification/sas/event_enums.rs +++ /dev/null @@ -1,146 +0,0 @@ -// 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 matrix_sdk_common::{ - events::{AnyMessageEventContent, AnyToDeviceEventContent}, - identifiers::RoomId, -}; - -#[derive(Clone, Debug)] -pub enum OutgoingContent { - Room(RoomId, AnyMessageEventContent), - ToDevice(AnyToDeviceEventContent), -} - -impl From for OutgoingContent { - fn from(content: OwnedStartContent) -> Self { - match content { - OwnedStartContent::Room(r, c) => { - (r, AnyMessageEventContent::KeyVerificationStart(c)).into() - } - OwnedStartContent::ToDevice(c) => { - AnyToDeviceEventContent::KeyVerificationStart(c).into() - } - } - } -} - -impl From for OutgoingContent { - fn from(content: AnyToDeviceEventContent) -> Self { - OutgoingContent::ToDevice(content) - } -} - -impl From<(RoomId, AnyMessageEventContent)> for OutgoingContent { - fn from(content: (RoomId, AnyMessageEventContent)) -> Self { - OutgoingContent::Room(content.0, content.1) - } -} - -#[cfg(test)] -use std::convert::TryFrom; - -use crate::verification::event_enums::OwnedStartContent; -#[cfg(test)] -use crate::{OutgoingRequest, OutgoingVerificationRequest, RoomMessageRequest, ToDeviceRequest}; - -#[cfg(test)] -impl From for OutgoingContent { - fn from(request: OutgoingVerificationRequest) -> Self { - match request { - OutgoingVerificationRequest::ToDevice(r) => Self::try_from(r).unwrap(), - OutgoingVerificationRequest::InRoom(r) => Self::from(r), - } - } -} - -#[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 = (); - - fn try_from(value: ToDeviceRequest) -> Result { - use matrix_sdk_common::events::EventType; - use serde_json::Value; - - let json: Value = serde_json::from_str( - value - .messages - .values() - .next() - .and_then(|m| m.values().next().map(|j| j.get())) - .ok_or(())?, - ) - .map_err(|_| ())?; - - match value.event_type { - EventType::KeyVerificationRequest => { - Ok(AnyToDeviceEventContent::KeyVerificationRequest( - serde_json::from_value(json).map_err(|_| ())?, - ) - .into()) - } - EventType::KeyVerificationReady => Ok(AnyToDeviceEventContent::KeyVerificationReady( - serde_json::from_value(json).map_err(|_| ())?, - ) - .into()), - EventType::KeyVerificationDone => Ok(AnyToDeviceEventContent::KeyVerificationDone( - serde_json::from_value(json).map_err(|_| ())?, - ) - .into()), - EventType::KeyVerificationStart => Ok(AnyToDeviceEventContent::KeyVerificationStart( - serde_json::from_value(json).map_err(|_| ())?, - ) - .into()), - EventType::KeyVerificationKey => Ok(AnyToDeviceEventContent::KeyVerificationKey( - serde_json::from_value(json).map_err(|_| ())?, - ) - .into()), - EventType::KeyVerificationAccept => Ok(AnyToDeviceEventContent::KeyVerificationAccept( - serde_json::from_value(json).map_err(|_| ())?, - ) - .into()), - EventType::KeyVerificationMac => Ok(AnyToDeviceEventContent::KeyVerificationMac( - serde_json::from_value(json).map_err(|_| ())?, - ) - .into()), - EventType::KeyVerificationCancel => Ok(AnyToDeviceEventContent::KeyVerificationCancel( - serde_json::from_value(json).map_err(|_| ())?, - ) - .into()), - _ => Err(()), - } - } -} - -#[cfg(test)] -impl TryFrom for OutgoingContent { - type Error = (); - - fn try_from(value: OutgoingRequest) -> Result { - match value.request() { - crate::OutgoingRequests::KeysUpload(_) => Err(()), - crate::OutgoingRequests::KeysQuery(_) => Err(()), - crate::OutgoingRequests::ToDeviceRequest(r) => Self::try_from(r.clone()), - crate::OutgoingRequests::SignatureUpload(_) => Err(()), - crate::OutgoingRequests::RoomMessage(r) => Ok(Self::from(r.clone())), - } - } -} diff --git a/matrix_sdk_crypto/src/verification/sas/inner_sas.rs b/matrix_sdk_crypto/src/verification/sas/inner_sas.rs index 2dc9dfa5..c509cca9 100644 --- a/matrix_sdk_crypto/src/verification/sas/inner_sas.rs +++ b/matrix_sdk_crypto/src/verification/sas/inner_sas.rs @@ -22,7 +22,6 @@ use matrix_sdk_common::{ }; use super::{ - event_enums::OutgoingContent, sas_state::{ Accepted, Confirmed, Created, KeyReceived, MacReceived, SasState, Started, WaitingForDone, }, @@ -31,7 +30,7 @@ use super::{ use crate::{ identities::{ReadOnlyDevice, UserIdentities}, verification::{ - event_enums::{AnyVerificationContent, OwnedAcceptContent, StartContent}, + event_enums::{AnyVerificationContent, OutgoingContent, OwnedAcceptContent, StartContent}, Cancelled, Done, }, ReadOnlyAccount, diff --git a/matrix_sdk_crypto/src/verification/sas/mod.rs b/matrix_sdk_crypto/src/verification/sas/mod.rs index c3e260f8..889c9896 100644 --- a/matrix_sdk_crypto/src/verification/sas/mod.rs +++ b/matrix_sdk_crypto/src/verification/sas/mod.rs @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -mod event_enums; mod helpers; mod inner_sas; mod sas_state; @@ -21,7 +20,6 @@ use std::sync::{Arc, Mutex}; #[cfg(test)] use std::time::Instant; -pub use event_enums::OutgoingContent; pub use helpers::content_to_request; use inner_sas::InnerSas; use matrix_sdk_common::{ @@ -40,7 +38,7 @@ use matrix_sdk_common::{ use tracing::trace; use super::{ - event_enums::{AnyVerificationContent, OwnedAcceptContent, StartContent}, + event_enums::{AnyVerificationContent, OutgoingContent, OwnedAcceptContent, StartContent}, FlowId, IdentitiesBeingVerified, VerificationResult, }; use crate::{ @@ -494,9 +492,8 @@ mod test { use crate::{ olm::PrivateCrossSigningIdentity, store::{CryptoStore, MemoryStore}, - verification::{ - event_enums::{AcceptContent, KeyContent, MacContent, StartContent}, - sas::OutgoingContent, + verification::event_enums::{ + AcceptContent, KeyContent, MacContent, OutgoingContent, StartContent, }, ReadOnlyAccount, ReadOnlyDevice, };