crypto: Retire the memory store.

master
Damir Jelić 2020-03-26 12:24:53 +01:00
parent 485296bc34
commit ddaf630063
2 changed files with 8 additions and 50 deletions

View File

@ -24,7 +24,6 @@ use super::memory_stores::{GroupSessionStore, SessionStore};
use super::olm::{Account, InboundGroupSession, Session}; use super::olm::{Account, InboundGroupSession, Session};
#[cfg(feature = "sqlite-cryptostore")] #[cfg(feature = "sqlite-cryptostore")]
use super::store::sqlite::SqliteStore; use super::store::sqlite::SqliteStore;
use super::store::MemoryStore;
use super::CryptoStore; use super::CryptoStore;
use crate::api; use crate::api;
@ -69,7 +68,7 @@ pub struct OlmMachine {
/// Store for the encryption keys. /// Store for the encryption keys.
/// Persists all the encrytpion keys so a client can resume the session /// Persists all the encrytpion keys so a client can resume the session
/// without the need to create new keys. /// without the need to create new keys.
// store: Box<dyn CryptoStore>, store: Option<Box<dyn CryptoStore>>,
/// A cache of all the Olm sessions we know about. /// A cache of all the Olm sessions we know about.
sessions: SessionStore, sessions: SessionStore,
/// A cache of all the inbound group sessions we know about. /// A cache of all the inbound group sessions we know about.
@ -89,7 +88,7 @@ impl OlmMachine {
device_id: device_id.to_owned(), device_id: device_id.to_owned(),
account: Arc::new(Mutex::new(Account::new())), account: Arc::new(Mutex::new(Account::new())),
uploaded_signed_key_count: None, uploaded_signed_key_count: None,
// store: Box::new(MemoryStore::new()), store: None,
sessions: SessionStore::new(), sessions: SessionStore::new(),
inbound_group_sessions: GroupSessionStore::new(), inbound_group_sessions: GroupSessionStore::new(),
}) })
@ -123,7 +122,7 @@ impl OlmMachine {
device_id: device_id.to_owned(), device_id: device_id.to_owned(),
account: Arc::new(Mutex::new(account)), account: Arc::new(Mutex::new(account)),
uploaded_signed_key_count: None, uploaded_signed_key_count: None,
// store: Box::new(store), store: Some(Box::new(store)),
sessions: SessionStore::new(), sessions: SessionStore::new(),
inbound_group_sessions: GroupSessionStore::new(), inbound_group_sessions: GroupSessionStore::new(),
}) })
@ -179,7 +178,10 @@ impl OlmMachine {
account.mark_keys_as_published(); account.mark_keys_as_published();
drop(account); 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(()) Ok(())
} }

View File

@ -53,51 +53,7 @@ pub enum CryptoStoreError {
pub type Result<T> = std::result::Result<T, CryptoStoreError>; pub type Result<T> = std::result::Result<T, CryptoStoreError>;
#[async_trait] #[async_trait]
pub trait CryptoStore: Debug + Sync + Sync { pub trait CryptoStore: Debug + Send + Sync {
async fn load_account(&mut self) -> Result<Option<Account>>; async fn load_account(&mut self) -> Result<Option<Account>>;
async fn save_account(&mut self, account: Arc<Mutex<Account>>) -> Result<()>; async fn save_account(&mut self, account: Arc<Mutex<Account>>) -> 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<Option<Account>> {
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<Mutex<Account>>) -> 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)
)
}
}