From f15b7cccea7bc81bdab264bf3866433102d78f1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 11 Mar 2020 10:04:04 +0100 Subject: [PATCH] crypto: Require a borrow of the user id when creating a new Olm machine. --- src/crypto/machine.rs | 24 +++++++++++++----------- src/crypto/olm.rs | 13 +++++++++++++ 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/crypto/machine.rs b/src/crypto/machine.rs index ab97dd8a..b2ea9fc6 100644 --- a/src/crypto/machine.rs +++ b/src/crypto/machine.rs @@ -34,7 +34,8 @@ use ruma_identifiers::{DeviceId, UserId}; pub type OneTimeKeys = HashMap; -struct OlmMachine { +#[derive(Debug)] +pub struct OlmMachine { /// The unique user id that owns this account. user_id: UserId, /// The unique device id of the device that holds this account. @@ -55,9 +56,9 @@ impl OlmMachine { ]; /// Create a new account. - pub fn new(user_id: UserId, device_id: &str) -> Self { + pub fn new(user_id: &UserId, device_id: &str) -> Self { OlmMachine { - user_id, + user_id: user_id.clone(), device_id: device_id.to_owned(), account: Account::new(), uploaded_signed_key_count: None, @@ -87,9 +88,10 @@ impl OlmMachine { /// # Arguments /// /// * `response` - The keys upload response of the request that the client - /// performed. + /// performed. pub async fn receive_keys_upload_response(&mut self, response: &keys::upload_keys::Response) { self.account.shared = true; + let one_time_key_count = response .one_time_key_counts .get(&keys::KeyAlgorithm::SignedCurve25519); @@ -351,13 +353,13 @@ mod test { #[test] fn create_olm_machine() { - let machine = OlmMachine::new(user_id(), DEVICE_ID); + let machine = OlmMachine::new(&user_id(), DEVICE_ID); assert!(machine.should_upload_keys()); } #[async_std::test] async fn receive_keys_upload_response() { - let mut machine = OlmMachine::new(user_id(), DEVICE_ID); + let mut machine = OlmMachine::new(&user_id(), DEVICE_ID); let mut response = keys_upload_response(); response @@ -386,7 +388,7 @@ mod test { #[async_std::test] async fn generate_one_time_keys() { - let mut machine = OlmMachine::new(user_id(), DEVICE_ID); + let mut machine = OlmMachine::new(&user_id(), DEVICE_ID); let mut response = keys_upload_response(); @@ -407,7 +409,7 @@ mod test { #[test] fn test_device_key_signing() { - let machine = OlmMachine::new(user_id(), DEVICE_ID); + let machine = OlmMachine::new(&user_id(), DEVICE_ID); let mut device_keys = machine.device_keys(); let identity_keys = machine.account.identity_keys(); @@ -424,7 +426,7 @@ mod test { #[test] fn test_invalid_signature() { - let machine = OlmMachine::new(user_id(), DEVICE_ID); + let machine = OlmMachine::new(&user_id(), DEVICE_ID); let mut device_keys = machine.device_keys(); @@ -439,7 +441,7 @@ mod test { #[test] fn test_one_time_key_signing() { - let mut machine = OlmMachine::new(user_id(), DEVICE_ID); + let mut machine = OlmMachine::new(&user_id(), DEVICE_ID); machine.uploaded_signed_key_count = Some(49); let mut one_time_keys = machine.signed_one_time_keys().unwrap(); @@ -459,7 +461,7 @@ mod test { #[async_std::test] async fn test_keys_for_upload() { - let mut machine = OlmMachine::new(user_id(), DEVICE_ID); + let mut machine = OlmMachine::new(&user_id(), DEVICE_ID); machine.uploaded_signed_key_count = Some(0); let identity_keys = machine.account.identity_keys(); diff --git a/src/crypto/olm.rs b/src/crypto/olm.rs index df3f9a42..13574bf5 100644 --- a/src/crypto/olm.rs +++ b/src/crypto/olm.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::fmt; + use olm_rs::account::{IdentityKeys, OlmAccount, OneTimeKeys}; pub struct Account { @@ -19,6 +21,17 @@ pub struct Account { pub(crate) shared: bool, } +impl fmt::Debug for Account { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "Olm Account: {:?}, shared: {}", + self.identity_keys(), + self.shared + ) + } +} + impl Account { /// Create a new account. pub fn new() -> Self {