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

This commit is contained in:
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>;
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();

View file

@ -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 {