From ddaf6300636ad5f4721bbceb0257cbb70dbe22c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 26 Mar 2020 12:24:53 +0100 Subject: [PATCH] crypto: Retire the memory store. --- src/crypto/machine.rs | 12 ++++++----- src/crypto/store/mod.rs | 46 +---------------------------------------- 2 files changed, 8 insertions(+), 50 deletions(-) diff --git a/src/crypto/machine.rs b/src/crypto/machine.rs index d4e3a2af..02edd5d0 100644 --- a/src/crypto/machine.rs +++ b/src/crypto/machine.rs @@ -24,7 +24,6 @@ use super::memory_stores::{GroupSessionStore, SessionStore}; use super::olm::{Account, InboundGroupSession, Session}; #[cfg(feature = "sqlite-cryptostore")] use super::store::sqlite::SqliteStore; -use super::store::MemoryStore; use super::CryptoStore; use crate::api; @@ -69,7 +68,7 @@ pub struct OlmMachine { /// Store for the encryption keys. /// Persists all the encrytpion keys so a client can resume the session /// without the need to create new keys. - // store: Box, + store: Option>, /// A cache of all the Olm sessions we know about. sessions: SessionStore, /// A cache of all the inbound group sessions we know about. @@ -89,7 +88,7 @@ impl OlmMachine { device_id: device_id.to_owned(), account: Arc::new(Mutex::new(Account::new())), uploaded_signed_key_count: None, - // store: Box::new(MemoryStore::new()), + store: None, sessions: SessionStore::new(), inbound_group_sessions: GroupSessionStore::new(), }) @@ -123,7 +122,7 @@ impl OlmMachine { device_id: device_id.to_owned(), account: Arc::new(Mutex::new(account)), uploaded_signed_key_count: None, - // store: Box::new(store), + store: Some(Box::new(store)), sessions: SessionStore::new(), inbound_group_sessions: GroupSessionStore::new(), }) @@ -179,7 +178,10 @@ impl OlmMachine { account.mark_keys_as_published(); drop(account); - // self.store.save_account(self.account.clone()).await?; + if let Some(store) = self.store.as_mut() { + store.save_account(self.account.clone()).await?; + } + Ok(()) } diff --git a/src/crypto/store/mod.rs b/src/crypto/store/mod.rs index 32e754f3..e7d031fc 100644 --- a/src/crypto/store/mod.rs +++ b/src/crypto/store/mod.rs @@ -53,51 +53,7 @@ pub enum CryptoStoreError { pub type Result = std::result::Result; #[async_trait] -pub trait CryptoStore: Debug + Sync + Sync { +pub trait CryptoStore: Debug + Send + Sync { async fn load_account(&mut self) -> Result>; async fn save_account(&mut self, account: Arc>) -> Result<()>; } - -pub struct MemoryStore { - pub(crate) account_info: Option<(String, bool)>, -} - -impl MemoryStore { - /// Create a new empty memory store. - pub fn new() -> Self { - MemoryStore { account_info: None } - } -} - -#[async_trait] -impl CryptoStore for MemoryStore { - async fn load_account(&mut self) -> Result> { - let result = match &self.account_info { - Some((pickle, shared)) => Some(Account::from_pickle( - pickle.to_owned(), - PicklingMode::Unencrypted, - *shared, - )?), - None => None, - }; - Ok(result) - } - - async fn save_account(&mut self, account: Arc>) -> Result<()> { - let acc = account.lock().await; - let pickle = acc.pickle(PicklingMode::Unencrypted); - self.account_info = Some((pickle, acc.shared)); - Ok(()) - } -} - -impl std::fmt::Debug for MemoryStore { - fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> StdResult<(), std::fmt::Error> { - write!( - fmt, - "MemoryStore {{ account_stored: {}, account shared: {} }}", - self.account_info.is_some(), - self.account_info.as_ref().map_or(false, |a| a.1) - ) - } -}