crypto: Require a borrow of the user id when creating a new Olm machine.

master
Damir Jelić 2020-03-11 10:04:04 +01:00
parent 740bc2a6c1
commit f15b7cccea
2 changed files with 26 additions and 11 deletions

View File

@ -34,7 +34,8 @@ use ruma_identifiers::{DeviceId, UserId};
pub type OneTimeKeys = HashMap<AlgorithmAndDeviceId, OneTimeKey>; pub type OneTimeKeys = HashMap<AlgorithmAndDeviceId, OneTimeKey>;
struct OlmMachine { #[derive(Debug)]
pub struct OlmMachine {
/// The unique user id that owns this account. /// The unique user id that owns this account.
user_id: UserId, user_id: UserId,
/// The unique device id of the device that holds this account. /// The unique device id of the device that holds this account.
@ -55,9 +56,9 @@ impl OlmMachine {
]; ];
/// Create a new account. /// 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 { OlmMachine {
user_id, user_id: user_id.clone(),
device_id: device_id.to_owned(), device_id: device_id.to_owned(),
account: Account::new(), account: Account::new(),
uploaded_signed_key_count: None, uploaded_signed_key_count: None,
@ -87,9 +88,10 @@ impl OlmMachine {
/// # Arguments /// # Arguments
/// ///
/// * `response` - The keys upload response of the request that the client /// * `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) { pub async fn receive_keys_upload_response(&mut self, response: &keys::upload_keys::Response) {
self.account.shared = true; self.account.shared = true;
let one_time_key_count = response let one_time_key_count = response
.one_time_key_counts .one_time_key_counts
.get(&keys::KeyAlgorithm::SignedCurve25519); .get(&keys::KeyAlgorithm::SignedCurve25519);
@ -351,13 +353,13 @@ mod test {
#[test] #[test]
fn create_olm_machine() { 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()); assert!(machine.should_upload_keys());
} }
#[async_std::test] #[async_std::test]
async fn receive_keys_upload_response() { 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(); let mut response = keys_upload_response();
response response
@ -386,7 +388,7 @@ mod test {
#[async_std::test] #[async_std::test]
async fn generate_one_time_keys() { 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(); let mut response = keys_upload_response();
@ -407,7 +409,7 @@ mod test {
#[test] #[test]
fn test_device_key_signing() { 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 mut device_keys = machine.device_keys();
let identity_keys = machine.account.identity_keys(); let identity_keys = machine.account.identity_keys();
@ -424,7 +426,7 @@ mod test {
#[test] #[test]
fn test_invalid_signature() { 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(); let mut device_keys = machine.device_keys();
@ -439,7 +441,7 @@ mod test {
#[test] #[test]
fn test_one_time_key_signing() { 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); machine.uploaded_signed_key_count = Some(49);
let mut one_time_keys = machine.signed_one_time_keys().unwrap(); let mut one_time_keys = machine.signed_one_time_keys().unwrap();
@ -459,7 +461,7 @@ mod test {
#[async_std::test] #[async_std::test]
async fn test_keys_for_upload() { 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); machine.uploaded_signed_key_count = Some(0);
let identity_keys = machine.account.identity_keys(); let identity_keys = machine.account.identity_keys();

View File

@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use std::fmt;
use olm_rs::account::{IdentityKeys, OlmAccount, OneTimeKeys}; use olm_rs::account::{IdentityKeys, OlmAccount, OneTimeKeys};
pub struct Account { pub struct Account {
@ -19,6 +21,17 @@ pub struct Account {
pub(crate) shared: bool, 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 { impl Account {
/// Create a new account. /// Create a new account.
pub fn new() -> Self { pub fn new() -> Self {