crypto: Remove the sas event enums module

This commit is contained in:
Damir Jelić 2021-06-04 15:05:35 +02:00
parent ac04b0c36e
commit bd5dda370d
7 changed files with 138 additions and 166 deletions

View file

@ -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<OwnedStartContent> 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<AnyToDeviceEventContent> 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<OutgoingVerificationRequest> 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<RoomMessageRequest> for OutgoingContent {
fn from(value: RoomMessageRequest) -> Self {
(value.room_id, value.content).into()
}
}
#[cfg(test)]
impl TryFrom<ToDeviceRequest> for OutgoingContent {
type Error = ();
fn try_from(value: ToDeviceRequest) -> Result<Self, Self::Error> {
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<OutgoingRequest> for OutgoingContent {
type Error = ();
fn try_from(value: OutgoingRequest) -> Result<Self, ()> {
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())),
}
}
}

View file

@ -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,

View file

@ -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,

View file

@ -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,

View file

@ -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<OwnedStartContent> 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<AnyToDeviceEventContent> 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<OutgoingVerificationRequest> 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<RoomMessageRequest> for OutgoingContent {
fn from(value: RoomMessageRequest) -> Self {
(value.room_id, value.content).into()
}
}
#[cfg(test)]
impl TryFrom<ToDeviceRequest> for OutgoingContent {
type Error = ();
fn try_from(value: ToDeviceRequest) -> Result<Self, Self::Error> {
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<OutgoingRequest> for OutgoingContent {
type Error = ();
fn try_from(value: OutgoingRequest) -> Result<Self, ()> {
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())),
}
}
}

View file

@ -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,

View file

@ -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,
};