crypto: Create a enum for the verification types

master
Damir Jelić 2021-06-04 14:21:53 +02:00
parent a04afac963
commit ac04b0c36e
2 changed files with 49 additions and 50 deletions

View File

@ -26,7 +26,7 @@ use super::{
event_enums::{AnyEvent, AnyVerificationContent}, event_enums::{AnyEvent, AnyVerificationContent},
requests::VerificationRequest, requests::VerificationRequest,
sas::{content_to_request, OutgoingContent, Sas}, sas::{content_to_request, OutgoingContent, Sas},
FlowId, VerificationResult, FlowId, Verification, VerificationResult,
}; };
use crate::{ use crate::{
olm::PrivateCrossSigningIdentity, olm::PrivateCrossSigningIdentity,
@ -37,74 +37,50 @@ use crate::{
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct VerificationCache { pub struct VerificationCache {
sas_verification: Arc<DashMap<String, Sas>>, verification: Arc<DashMap<String, Verification>>,
room_sas_verifications: Arc<DashMap<EventId, Sas>>,
outgoing_requests: Arc<DashMap<Uuid, OutgoingRequest>>, outgoing_requests: Arc<DashMap<Uuid, OutgoingRequest>>,
} }
impl VerificationCache { impl VerificationCache {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self { verification: DashMap::new().into(), outgoing_requests: DashMap::new().into() }
sas_verification: DashMap::new().into(),
room_sas_verifications: DashMap::new().into(),
outgoing_requests: DashMap::new().into(),
}
} }
#[cfg(test)] #[cfg(test)]
fn is_empty(&self) -> bool { fn is_empty(&self) -> bool {
self.room_sas_verifications.is_empty() && self.sas_verification.is_empty() self.verification.is_empty()
} }
pub fn insert_sas(&self, sas: Sas) { pub fn insert_sas(&self, sas: Sas) {
match sas.flow_id() { self.verification.insert(sas.flow_id().as_str().to_string(), sas.into());
super::FlowId::ToDevice(t) => self.sas_verification.insert(t.to_owned(), sas),
super::FlowId::InRoom(_, e) => self.room_sas_verifications.insert(e.to_owned(), sas),
};
} }
pub fn garbage_collect(&self) -> Vec<OutgoingRequest> { pub fn garbage_collect(&self) -> Vec<OutgoingRequest> {
self.sas_verification.retain(|_, s| !(s.is_done() || s.is_cancelled())); self.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<OutgoingRequest> = self self.verification
.sas_verification
.iter() .iter()
.filter_map(|s| { .filter_map(|s| {
if let Verification::SasV1(s) = s.value() {
s.cancel_if_timed_out().map(|r| OutgoingRequest { s.cancel_if_timed_out().map(|r| OutgoingRequest {
request_id: r.request_id(), request_id: r.request_id(),
request: Arc::new(r.into()), request: Arc::new(r.into()),
}) })
} else {
None
}
}) })
.collect(); .collect()
let room_requests: Vec<OutgoingRequest> = self
.room_sas_verifications
.iter()
.filter_map(|s| {
s.cancel_if_timed_out().map(|r| OutgoingRequest {
request_id: r.request_id(),
request: Arc::new(r.into()),
})
})
.collect();
requests.extend(room_requests);
requests
} }
pub fn get_sas(&self, transaction_id: &str) -> Option<Sas> { pub fn get_sas(&self, transaction_id: &str) -> Option<Sas> {
let sas = if let Ok(e) = EventId::try_from(transaction_id) { self.verification.get(transaction_id).and_then(|v| {
self.room_sas_verifications.get(&e).map(|s| s.clone()) if let Verification::SasV1(sas) = v.value() {
Some(sas.clone())
} else { } else {
None None
};
if sas.is_some() {
sas
} else {
self.sas_verification.get(transaction_id).map(|s| s.clone())
} }
})
} }
pub fn add_request(&self, request: OutgoingRequest) { pub fn add_request(&self, request: OutgoingRequest) {
@ -195,9 +171,7 @@ impl VerificationMachine {
let request = let request =
content_to_request(device.user_id(), device.device_id().to_owned(), c); content_to_request(device.user_id(), device.device_id().to_owned(), c);
self.verifications self.verifications.insert_sas(sas.clone());
.sas_verification
.insert(sas.flow_id().as_str().to_owned(), sas.clone());
request.into() request.into()
} }

View File

@ -44,6 +44,31 @@ use crate::{
CryptoStoreError, LocalTrust, ReadOnlyDevice, UserIdentities, CryptoStoreError, LocalTrust, ReadOnlyDevice, UserIdentities,
}; };
#[derive(Clone, Debug)]
pub enum Verification {
SasV1(Sas),
}
impl Verification {
pub fn is_done(&self) -> bool {
match self {
Verification::SasV1(s) => s.is_done(),
}
}
pub fn is_cancelled(&self) -> bool {
match self {
Verification::SasV1(s) => s.is_cancelled(),
}
}
}
impl From<Sas> for Verification {
fn from(sas: Sas) -> Self {
Self::SasV1(sas)
}
}
/// The verification state indicating that the verification finished /// The verification state indicating that the verification finished
/// successfully. /// successfully.
/// ///