crypto: Put the user id and device id into the account.
This commit is contained in:
parent
4ee245dcce
commit
58d79ca9c6
3 changed files with 73 additions and 42 deletions
|
@ -111,11 +111,11 @@ impl OlmMachine {
|
|||
/// * `user_id` - The unique id of the user that owns this machine.
|
||||
///
|
||||
/// * `device_id` - The unique id of the device that owns this machine.
|
||||
pub fn new(user_id: &UserId, device_id: &str) -> Self {
|
||||
pub fn new(user_id: &UserId, device_id: &DeviceId) -> Self {
|
||||
OlmMachine {
|
||||
user_id: user_id.clone(),
|
||||
device_id: device_id.to_owned(),
|
||||
account: Account::new(),
|
||||
account: Account::new(user_id, &device_id),
|
||||
uploaded_signed_key_count: None,
|
||||
store: Box::new(MemoryStore::new()),
|
||||
outbound_group_sessions: HashMap::new(),
|
||||
|
@ -151,13 +151,13 @@ impl OlmMachine {
|
|||
}
|
||||
None => {
|
||||
debug!("Creating a new account");
|
||||
Account::new()
|
||||
Account::new(&user_id, &device_id)
|
||||
}
|
||||
};
|
||||
|
||||
Ok(OlmMachine {
|
||||
user_id: user_id.clone(),
|
||||
device_id: device_id.to_owned(),
|
||||
user_id,
|
||||
device_id,
|
||||
account,
|
||||
uploaded_signed_key_count: None,
|
||||
store,
|
||||
|
@ -1554,7 +1554,6 @@ impl OlmMachine {
|
|||
#[cfg(test)]
|
||||
mod test {
|
||||
static USER_ID: &str = "@bob:example.org";
|
||||
static DEVICE_ID: &str = "DEVICEID";
|
||||
|
||||
use matrix_sdk_common::js_int::UInt;
|
||||
use std::collections::BTreeMap;
|
||||
|
@ -1631,7 +1630,7 @@ mod test {
|
|||
}
|
||||
|
||||
async fn get_prepared_machine() -> (OlmMachine, OneTimeKeys) {
|
||||
let mut machine = OlmMachine::new(&user_id(), DEVICE_ID);
|
||||
let mut machine = OlmMachine::new(&user_id(), &alice_device_id());
|
||||
machine.uploaded_signed_key_count = Some(AtomicU64::new(0));
|
||||
let (_, otk) = machine
|
||||
.keys_for_upload()
|
||||
|
@ -1731,13 +1730,13 @@ mod test {
|
|||
|
||||
#[tokio::test]
|
||||
async fn create_olm_machine() {
|
||||
let machine = OlmMachine::new(&user_id(), DEVICE_ID);
|
||||
let machine = OlmMachine::new(&user_id(), &alice_device_id());
|
||||
assert!(machine.should_upload_keys().await);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn receive_keys_upload_response() {
|
||||
let mut machine = OlmMachine::new(&user_id(), DEVICE_ID);
|
||||
let mut machine = OlmMachine::new(&user_id(), &alice_device_id());
|
||||
let mut response = keys_upload_response();
|
||||
|
||||
response
|
||||
|
@ -1775,7 +1774,7 @@ mod test {
|
|||
|
||||
#[tokio::test]
|
||||
async fn generate_one_time_keys() {
|
||||
let mut machine = OlmMachine::new(&user_id(), DEVICE_ID);
|
||||
let mut machine = OlmMachine::new(&user_id(), &alice_device_id());
|
||||
|
||||
let mut response = keys_upload_response();
|
||||
|
||||
|
@ -1802,7 +1801,7 @@ mod test {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_device_key_signing() {
|
||||
let machine = OlmMachine::new(&user_id(), DEVICE_ID);
|
||||
let machine = OlmMachine::new(&user_id(), &alice_device_id());
|
||||
|
||||
let mut device_keys = machine.device_keys().await;
|
||||
let identity_keys = machine.account.identity_keys();
|
||||
|
@ -1819,7 +1818,7 @@ mod test {
|
|||
|
||||
#[tokio::test]
|
||||
async fn tests_session_invalidation() {
|
||||
let mut machine = OlmMachine::new(&user_id(), DEVICE_ID);
|
||||
let mut machine = OlmMachine::new(&user_id(), &alice_device_id());
|
||||
let room_id = RoomId::try_from("!test:example.org").unwrap();
|
||||
|
||||
machine
|
||||
|
@ -1835,7 +1834,7 @@ mod test {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_invalid_signature() {
|
||||
let machine = OlmMachine::new(&user_id(), DEVICE_ID);
|
||||
let machine = OlmMachine::new(&user_id(), &alice_device_id());
|
||||
|
||||
let mut device_keys = machine.device_keys().await;
|
||||
|
||||
|
@ -1850,7 +1849,7 @@ mod test {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_one_time_key_signing() {
|
||||
let mut machine = OlmMachine::new(&user_id(), DEVICE_ID);
|
||||
let mut machine = OlmMachine::new(&user_id(), &alice_device_id());
|
||||
machine.uploaded_signed_key_count = Some(AtomicU64::new(49));
|
||||
|
||||
let mut one_time_keys = machine.signed_one_time_keys().await.unwrap();
|
||||
|
@ -1870,7 +1869,7 @@ mod test {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_keys_for_upload() {
|
||||
let mut machine = OlmMachine::new(&user_id(), DEVICE_ID);
|
||||
let mut machine = OlmMachine::new(&user_id(), &alice_device_id());
|
||||
machine.uploaded_signed_key_count = Some(AtomicU64::default());
|
||||
|
||||
let identity_keys = machine.account.identity_keys();
|
||||
|
|
|
@ -35,7 +35,7 @@ pub use olm_rs::{
|
|||
};
|
||||
|
||||
use matrix_sdk_common::api::r0::keys::SignedKey;
|
||||
use matrix_sdk_common::identifiers::RoomId;
|
||||
use matrix_sdk_common::identifiers::{DeviceId, RoomId, UserId};
|
||||
|
||||
/// Account holding identity keys for which sessions can be created.
|
||||
///
|
||||
|
@ -43,6 +43,8 @@ use matrix_sdk_common::identifiers::RoomId;
|
|||
/// devices.
|
||||
#[derive(Clone)]
|
||||
pub struct Account {
|
||||
user_id: Arc<UserId>,
|
||||
device_id: Arc<DeviceId>,
|
||||
inner: Arc<Mutex<OlmAccount>>,
|
||||
identity_keys: Arc<IdentityKeys>,
|
||||
shared: Arc<AtomicBool>,
|
||||
|
@ -58,20 +60,15 @@ impl fmt::Debug for Account {
|
|||
}
|
||||
}
|
||||
|
||||
// #[cfg_attr(tarpaulin, skip)]
|
||||
impl Default for Account {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Account {
|
||||
/// Create a fresh new account, this will generate the identity key-pair.
|
||||
pub fn new() -> Self {
|
||||
pub fn new(user_id: &UserId, device_id: &DeviceId) -> Self {
|
||||
let account = OlmAccount::new();
|
||||
let identity_keys = account.parsed_identity_keys();
|
||||
|
||||
Account {
|
||||
user_id: Arc::new(user_id.to_owned()),
|
||||
device_id: Arc::new(device_id.to_owned()),
|
||||
inner: Arc::new(Mutex::new(account)),
|
||||
identity_keys: Arc::new(identity_keys),
|
||||
shared: Arc::new(AtomicBool::new(false)),
|
||||
|
@ -150,11 +147,15 @@ impl Account {
|
|||
pickle: String,
|
||||
pickle_mode: PicklingMode,
|
||||
shared: bool,
|
||||
user_id: &UserId,
|
||||
device_id: &DeviceId,
|
||||
) -> Result<Self, OlmAccountError> {
|
||||
let account = OlmAccount::unpickle(pickle, pickle_mode)?;
|
||||
let identity_keys = account.parsed_identity_keys();
|
||||
|
||||
Ok(Account {
|
||||
user_id: Arc::new(user_id.to_owned()),
|
||||
device_id: Arc::new(device_id.to_owned()),
|
||||
inner: Arc::new(Mutex::new(account)),
|
||||
identity_keys: Arc::new(identity_keys),
|
||||
shared: Arc::new(AtomicBool::from(shared)),
|
||||
|
@ -659,15 +660,30 @@ impl std::fmt::Debug for OutboundGroupSession {
|
|||
pub(crate) mod test {
|
||||
use crate::olm::{Account, InboundGroupSession, OutboundGroupSession, Session};
|
||||
use matrix_sdk_common::api::r0::keys::SignedKey;
|
||||
use matrix_sdk_common::identifiers::RoomId;
|
||||
use matrix_sdk_common::identifiers::{DeviceId, RoomId, UserId};
|
||||
use olm_rs::session::OlmMessage;
|
||||
use std::collections::BTreeMap;
|
||||
use std::convert::TryFrom;
|
||||
|
||||
pub(crate) async fn get_account_and_session() -> (Account, Session) {
|
||||
let alice = Account::new();
|
||||
fn alice_id() -> UserId {
|
||||
UserId::try_from("@alice:example.org").unwrap()
|
||||
}
|
||||
|
||||
let bob = Account::new();
|
||||
fn alice_device_id() -> DeviceId {
|
||||
"ALICEDEVICE".to_string()
|
||||
}
|
||||
|
||||
fn bob_id() -> UserId {
|
||||
UserId::try_from("@bob:example.org").unwrap()
|
||||
}
|
||||
|
||||
fn bob_device_id() -> DeviceId {
|
||||
"BOBDEVICE".to_string()
|
||||
}
|
||||
|
||||
pub(crate) async fn get_account_and_session() -> (Account, Session) {
|
||||
let alice = Account::new(&alice_id(), &alice_device_id());
|
||||
let bob = Account::new(&bob_id(), &bob_device_id());
|
||||
|
||||
bob.generate_one_time_keys(1).await;
|
||||
let one_time_key = bob
|
||||
|
@ -694,7 +710,7 @@ pub(crate) mod test {
|
|||
|
||||
#[test]
|
||||
fn account_creation() {
|
||||
let account = Account::new();
|
||||
let account = Account::new(&alice_id(), &alice_device_id());
|
||||
let identyty_keys = account.identity_keys();
|
||||
|
||||
assert!(!account.shared());
|
||||
|
@ -715,7 +731,7 @@ pub(crate) mod test {
|
|||
|
||||
#[tokio::test]
|
||||
async fn one_time_keys_creation() {
|
||||
let account = Account::new();
|
||||
let account = Account::new(&alice_id(), &alice_device_id());
|
||||
let one_time_keys = account.one_time_keys().await;
|
||||
|
||||
assert!(one_time_keys.curve25519().is_empty());
|
||||
|
@ -742,8 +758,8 @@ pub(crate) mod test {
|
|||
|
||||
#[tokio::test]
|
||||
async fn session_creation() {
|
||||
let alice = Account::new();
|
||||
let bob = Account::new();
|
||||
let alice = Account::new(&alice_id(), &alice_device_id());
|
||||
let bob = Account::new(&bob_id(), &bob_device_id());
|
||||
let alice_keys = alice.identity_keys();
|
||||
alice.generate_one_time_keys(1).await;
|
||||
let one_time_keys = alice.one_time_keys().await;
|
||||
|
|
|
@ -35,7 +35,7 @@ use matrix_sdk_common::identifiers::{DeviceId, RoomId, UserId};
|
|||
|
||||
/// SQLite based implementation of a `CryptoStore`.
|
||||
pub struct SqliteStore {
|
||||
user_id: Arc<String>,
|
||||
user_id: Arc<UserId>,
|
||||
device_id: Arc<String>,
|
||||
account_id: Option<i64>,
|
||||
path: PathBuf,
|
||||
|
@ -117,7 +117,7 @@ impl SqliteStore {
|
|||
|
||||
let connection = SqliteConnection::connect(url.as_ref()).await?;
|
||||
let store = SqliteStore {
|
||||
user_id: Arc::new(user_id.to_string()),
|
||||
user_id: Arc::new(user_id.to_owned()),
|
||||
device_id: Arc::new(device_id.to_owned()),
|
||||
account_id: None,
|
||||
sessions: SessionStore::new(),
|
||||
|
@ -568,7 +568,7 @@ impl CryptoStore for SqliteStore {
|
|||
"SELECT id, pickle, shared FROM accounts
|
||||
WHERE user_id = ? and device_id = ?",
|
||||
)
|
||||
.bind(&*self.user_id)
|
||||
.bind(self.user_id.as_str())
|
||||
.bind(&*self.device_id)
|
||||
.fetch_optional(&mut *connection)
|
||||
.await?;
|
||||
|
@ -579,6 +579,8 @@ impl CryptoStore for SqliteStore {
|
|||
pickle,
|
||||
self.get_pickle_mode(),
|
||||
shared,
|
||||
&self.user_id,
|
||||
&self.device_id,
|
||||
)?)
|
||||
} else {
|
||||
return Ok(None);
|
||||
|
@ -788,13 +790,12 @@ mod test {
|
|||
use crate::device::test::get_device;
|
||||
use crate::olm::GroupSessionKey;
|
||||
use matrix_sdk_common::api::r0::keys::SignedKey;
|
||||
use matrix_sdk_common::identifiers::{DeviceId, UserId};
|
||||
use olm_rs::outbound_group_session::OlmOutboundGroupSession;
|
||||
use std::collections::BTreeMap;
|
||||
use tempfile::tempdir;
|
||||
|
||||
use super::{
|
||||
Account, CryptoStore, InboundGroupSession, RoomId, Session, SqliteStore, TryFrom, UserId,
|
||||
};
|
||||
use super::{Account, CryptoStore, InboundGroupSession, RoomId, Session, SqliteStore, TryFrom};
|
||||
|
||||
static USER_ID: &str = "@example:localhost";
|
||||
static DEVICE_ID: &str = "DEVICEID";
|
||||
|
@ -829,14 +830,29 @@ mod test {
|
|||
(account, store, dir)
|
||||
}
|
||||
|
||||
fn alice_id() -> UserId {
|
||||
UserId::try_from("@alice:example.org").unwrap()
|
||||
}
|
||||
|
||||
fn alice_device_id() -> DeviceId {
|
||||
"ALICEDEVICE".to_string()
|
||||
}
|
||||
|
||||
fn bob_id() -> UserId {
|
||||
UserId::try_from("@bob:example.org").unwrap()
|
||||
}
|
||||
|
||||
fn bob_device_id() -> DeviceId {
|
||||
"BOBDEVICE".to_string()
|
||||
}
|
||||
|
||||
fn get_account() -> Account {
|
||||
Account::new()
|
||||
Account::new(&alice_id(), &alice_device_id())
|
||||
}
|
||||
|
||||
async fn get_account_and_session() -> (Account, Session) {
|
||||
let alice = Account::new();
|
||||
|
||||
let bob = Account::new();
|
||||
let alice = Account::new(&alice_id(), &alice_device_id());
|
||||
let bob = Account::new(&bob_id(), &bob_device_id());
|
||||
|
||||
bob.generate_one_time_keys(1).await;
|
||||
let one_time_key = bob
|
||||
|
|
Loading…
Reference in a new issue