crypto: Rename the memory stores into caches and reorder the store module.

master
Damir Jelić 2020-09-04 12:42:40 +02:00
parent 7b3dfe2f27
commit adf8905d9f
12 changed files with 92 additions and 49 deletions

View File

@ -46,8 +46,8 @@ use matrix_sdk_common::{
};
#[cfg(feature = "encryption")]
use matrix_sdk_crypto::{
CryptoStore, CryptoStoreError, Device, IncomingResponse, OlmError, OlmMachine, OutgoingRequest,
Sas, UserDevices,
store::{CryptoStore, CryptoStoreError},
Device, IncomingResponse, OlmError, OlmMachine, OutgoingRequest, Sas, UserDevices,
};
use zeroize::Zeroizing;

View File

@ -57,8 +57,8 @@ pub use state::{AllRooms, ClientState};
#[cfg(feature = "encryption")]
#[cfg_attr(feature = "docs", doc(cfg(encryption)))]
pub use matrix_sdk_crypto::{
CryptoStoreError, Device, IncomingResponse, LocalTrust, OutgoingRequest, OutgoingRequests,
ReadOnlyDevice, Sas, UserDevices,
store::CryptoStoreError, Device, IncomingResponse, LocalTrust, OutgoingRequest,
OutgoingRequests, ReadOnlyDevice, Sas, UserDevices,
};
#[cfg(feature = "messages")]

View File

@ -41,9 +41,9 @@ use crate::{Account, OlmMachine};
use crate::{
error::{EventError, OlmError, OlmResult, SignatureError},
identities::{OwnUserIdentity, UserIdentities},
store::Result as StoreResult,
store::{caches::ReadOnlyUserDevices, Result as StoreResult},
verification::VerificationMachine,
verify_json, ReadOnlyUserDevices, Sas,
verify_json, Sas,
};
/// A read-only version of a `Device`.

View File

@ -513,7 +513,7 @@ mod test {
identities::{Device, ReadOnlyDevice},
machine::test::response_from_file,
olm::Account,
store::memorystore::MemoryStore,
store::MemoryStore,
verification::VerificationMachine,
};

View File

@ -30,10 +30,9 @@
mod error;
mod identities;
mod machine;
pub mod memory_stores;
pub mod olm;
mod requests;
mod store;
pub mod store;
mod verification;
pub use error::{MegolmError, OlmError};
@ -41,13 +40,9 @@ pub use identities::{
Device, LocalTrust, OwnUserIdentity, ReadOnlyDevice, UserDevices, UserIdentities, UserIdentity,
};
pub use machine::OlmMachine;
pub use memory_stores::ReadOnlyUserDevices;
pub(crate) use olm::Account;
pub use olm::EncryptionSettings;
pub use requests::{IncomingResponse, OutgoingRequest, OutgoingRequests};
#[cfg(feature = "sqlite_cryptostore")]
pub use store::sqlite::SqliteStore;
pub use store::{CryptoStore, CryptoStoreError};
pub use verification::Sas;
use error::SignatureError;

View File

@ -63,9 +63,8 @@ use super::{
OlmMessage, OutboundGroupSession,
},
requests::{IncomingResponse, OutgoingRequest},
store::{memorystore::MemoryStore, Result as StoreResult},
store::{CryptoStore, MemoryStore, Result as StoreResult},
verification::{Sas, VerificationMachine},
CryptoStore,
};
/// State machine implementation of the Olm/Megolm encryption protocol used for

View File

@ -25,7 +25,7 @@ use matrix_sdk_common::{
locks::Mutex,
};
use super::{
use crate::{
identities::ReadOnlyDevice,
olm::{InboundGroupSession, Session},
};
@ -209,8 +209,8 @@ impl DeviceStore {
mod test {
use crate::{
identities::device::test::get_device,
memory_stores::{DeviceStore, GroupSessionStore, SessionStore},
olm::{test::get_account_and_session, InboundGroupSession},
store::caches::{DeviceStore, GroupSessionStore, SessionStore},
};
use matrix_sdk_common::identifiers::room_id;

View File

@ -21,11 +21,13 @@ use matrix_sdk_common::{
};
use matrix_sdk_common_macros::async_trait;
use super::{Account, CryptoStore, InboundGroupSession, Result, Session};
use crate::{
identities::{ReadOnlyDevice, UserIdentities},
memory_stores::{DeviceStore, GroupSessionStore, ReadOnlyUserDevices, SessionStore},
use super::{
caches::{DeviceStore, GroupSessionStore, ReadOnlyUserDevices, SessionStore},
Account, CryptoStore, InboundGroupSession, Result, Session,
};
use crate::identities::{ReadOnlyDevice, UserIdentities};
/// An in-memory only store that will forget all the E2EE key once it's dropped.
#[derive(Debug, Clone)]
pub struct MemoryStore {
sessions: SessionStore,
@ -36,8 +38,8 @@ pub struct MemoryStore {
identities: Arc<DashMap<UserId, UserIdentities>>,
}
impl MemoryStore {
pub fn new() -> Self {
impl Default for MemoryStore {
fn default() -> Self {
MemoryStore {
sessions: SessionStore::new(),
inbound_group_sessions: GroupSessionStore::new(),
@ -49,6 +51,13 @@ impl MemoryStore {
}
}
impl MemoryStore {
/// Create a new empty `MemoryStore`.
pub fn new() -> Self {
Self::default()
}
}
#[async_trait]
impl CryptoStore for MemoryStore {
async fn load_account(&self) -> Result<Option<Account>> {

View File

@ -12,8 +12,55 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//! Types and traits to implement the storage layer for the [`OlmMachine`]
//!
//! The storage layer for the [`OlmMachine`] can be customized using a trait.
//! Implementing your own [`CryptoStore`]
//!
//! An in-memory only store is provided as well as a SQLite based one, depending
//! on your needs and targets a custom store may be implemented, e.g. for
//! `wasm-unknown-unknown` an indexeddb store would be needed
//!
//! ```
//! # use matrix_sdk_crypto::{
//! # OlmMachine,
//! # store::MemoryStore,
//! # };
//! # use matrix_sdk_common::identifiers::{user_id, DeviceIdBox};
//! # let user_id = user_id!("@example:localhost");
//! # let device_id: DeviceIdBox = "TEST".into();
//! let store = Box::new(MemoryStore::new());
//!
//! let machine = OlmMachine::new_with_store(user_id, device_id, store);
//! ```
//!
//! [`OlmMachine`]: /matrix_sdk_crypto/struct.OlmMachine.html
//! [`CryptoStore`]: trait.Cryptostore.html
pub mod caches;
mod memorystore;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "sqlite_cryptostore")]
pub(crate) mod sqlite;
use caches::ReadOnlyUserDevices;
pub use memorystore::MemoryStore;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "sqlite_cryptostore")]
pub use sqlite::SqliteStore;
use std::{collections::HashSet, fmt::Debug, io::Error as IoError, sync::Arc};
use olm_rs::errors::{OlmAccountError, OlmGroupSessionError, OlmSessionError};
use serde_json::Error as SerdeError;
use thiserror::Error;
use url::ParseError;
#[cfg_attr(feature = "docs", doc(cfg(r#sqlite_cryptostore)))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "sqlite_cryptostore")]
use sqlx::Error as SqlxError;
use matrix_sdk_common::{
identifiers::{DeviceId, Error as IdentifierValidationError, RoomId, UserId},
locks::Mutex,
@ -22,28 +69,15 @@ use matrix_sdk_common_macros::async_trait;
#[cfg(not(target_arch = "wasm32"))]
use matrix_sdk_common_macros::send_sync;
use olm_rs::errors::{OlmAccountError, OlmGroupSessionError, OlmSessionError};
use serde_json::Error as SerdeError;
use thiserror::Error;
use url::ParseError;
use super::{
identities::{ReadOnlyDevice, UserIdentities},
memory_stores::ReadOnlyUserDevices,
olm::{Account, InboundGroupSession, Session},
};
use crate::error::SessionUnpicklingError;
pub mod memorystore;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "sqlite_cryptostore")]
pub mod sqlite;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "sqlite_cryptostore")]
use sqlx::Error as SqlxError;
/// A `CryptoStore` specific result type.
pub type Result<T> = std::result::Result<T, CryptoStoreError>;
#[derive(Error, Debug)]
/// The crypto store's error type.
@ -93,8 +127,6 @@ pub enum CryptoStoreError {
UrlParse(#[from] ParseError),
}
pub type Result<T> = std::result::Result<T, CryptoStoreError>;
/// Trait abstracting a store that the `OlmMachine` uses to store cryptographic
/// keys.
#[async_trait]

View File

@ -33,10 +33,12 @@ use sqlx::{query, query_as, sqlite::SqliteQueryAs, Connect, Executor, SqliteConn
use url::Url;
use zeroize::Zeroizing;
use super::{CryptoStore, CryptoStoreError, Result};
use super::{
caches::{DeviceStore, GroupSessionStore, ReadOnlyUserDevices, SessionStore},
CryptoStore, CryptoStoreError, Result,
};
use crate::{
identities::{LocalTrust, ReadOnlyDevice, UserIdentities},
memory_stores::{DeviceStore, GroupSessionStore, ReadOnlyUserDevices, SessionStore},
olm::{
Account, AccountPickle, IdentityKeys, InboundGroupSession, InboundGroupSessionPickle,
PickledAccount, PickledInboundGroupSession, PickledSession, PicklingMode, Session,
@ -44,8 +46,9 @@ use crate::{
},
};
#[derive(Clone)]
/// SQLite based implementation of a `CryptoStore`.
#[derive(Clone)]
#[cfg_attr(feature = "docs", doc(cfg(r#sqlite_cryptostore)))]
pub struct SqliteStore {
user_id: Arc<UserId>,
device_id: Arc<Box<DeviceId>>,

View File

@ -26,7 +26,11 @@ use matrix_sdk_common::{
};
use super::sas::{content_to_request, Sas};
use crate::{requests::OutgoingRequest, Account, CryptoStore, CryptoStoreError, ReadOnlyDevice};
use crate::{
requests::OutgoingRequest,
store::{CryptoStore, CryptoStoreError},
Account, ReadOnlyDevice,
};
#[derive(Clone, Debug)]
pub struct VerificationMachine {
@ -229,9 +233,9 @@ mod test {
use super::{Sas, VerificationMachine};
use crate::{
requests::OutgoingRequests,
store::memorystore::MemoryStore,
store::{CryptoStore, MemoryStore},
verification::test::{get_content_from_request, wrap_any_to_device_content},
Account, CryptoStore, ReadOnlyDevice,
Account, ReadOnlyDevice,
};
fn alice_id() -> UserId {

View File

@ -36,7 +36,8 @@ use matrix_sdk_common::{
use crate::{
identities::{LocalTrust, ReadOnlyDevice, UserIdentities},
Account, CryptoStore, CryptoStoreError,
store::{CryptoStore, CryptoStoreError},
Account,
};
pub use helpers::content_to_request;
@ -658,9 +659,9 @@ mod test {
};
use crate::{
store::memorystore::MemoryStore,
store::{CryptoStore, MemoryStore},
verification::test::{get_content_from_request, wrap_any_to_device_content},
Account, CryptoStore, ReadOnlyDevice,
Account, ReadOnlyDevice,
};
use super::{Accepted, Created, Sas, SasState, Started};