feat(crypto-js): Start implementing OlmMachine.get_identity.

This commit is contained in:
Ivan Enderlin
2022-09-15 10:32:38 +02:00
parent 01f1b9b846
commit f4e0c6e243
4 changed files with 112 additions and 2 deletions

View File

@@ -288,7 +288,7 @@ impl ServerName {
/// [event ID]: https://spec.matrix.org/v1.2/appendices/#room-ids-and-event-ids
#[wasm_bindgen]
pub struct EventId {
inner: ruma::OwnedEventId,
pub(crate) inner: ruma::OwnedEventId,
}
#[wasm_bindgen]

View File

@@ -0,0 +1,98 @@
//! User identities.
use js_sys::{Array, Promise};
use ruma::events::key::verification::VerificationMethod as RumaVerificationMethod;
use wasm_bindgen::prelude::*;
use crate::{future::future_to_promise, identifiers, impl_from_to_inner, requests, verification};
pub(crate) struct UserIdentities {
inner: matrix_sdk_crypto::UserIdentities,
}
impl_from_to_inner!(matrix_sdk_crypto::UserIdentities => UserIdentities);
impl From<UserIdentities> for JsValue {
fn from(user_identities: UserIdentities) -> Self {
use matrix_sdk_crypto::UserIdentities::*;
match user_identities.inner {
Own(own) => JsValue::from(OwnUserIdentity::from(own)),
Other(other) => JsValue::from(UserIdentity::from(other)),
}
}
}
#[wasm_bindgen]
#[derive(Debug)]
pub struct OwnUserIdentity {
inner: matrix_sdk_crypto::OwnUserIdentity,
}
impl_from_to_inner!(matrix_sdk_crypto::OwnUserIdentity => OwnUserIdentity);
#[wasm_bindgen]
#[derive(Debug)]
pub struct UserIdentity {
inner: matrix_sdk_crypto::UserIdentity,
}
impl_from_to_inner!(matrix_sdk_crypto::UserIdentity => UserIdentity);
#[wasm_bindgen]
impl UserIdentity {
/// Is this user identity verified?
#[wasm_bindgen(js_name = "isVerified")]
pub fn is_verified(&self) -> bool {
self.inner.is_verified()
}
/// Manually verify this user.
///
/// This method will attempt to sign the user identity using our private
/// cross signing key.
///
/// This method fails if we don't have the private part of our user-signing
/// key.
///
/// Returns a request that needs to be sent out for the user to be marked as
/// verified.
pub fn verify(&self) -> Promise {
let me = self.inner.clone();
future_to_promise(async move {
Ok(requests::SignatureUploadRequest::try_from(&me.verify().await?)?)
})
}
/// Create a `VerificationRequest` object after the verification
/// request content has been sent out. }
#[wasm_bindgen(js_name = "requestVerification")]
pub fn request_verification(
&self,
room_id: &identifiers::RoomId,
request_event_id: &identifiers::EventId,
methods: Option<Array>,
) -> Result<Promise, JsError> {
let me = self.inner.clone();
let room_id = room_id.inner.clone();
let request_event_id = request_event_id.inner.clone();
let methods: Option<Vec<RumaVerificationMethod>> = methods
.map(|array| {
array
.iter()
.map(|method| {
verification::VerificationMethod::try_from(method).map(Into::into)
})
.collect::<Result<_, _>>()
})
.transpose()?;
Ok(future_to_promise::<_, verification::VerificationRequest>(async move {
Ok(me
.request_verification(room_id.as_ref(), request_event_id.as_ref(), methods)
.await
.into())
}))
}
}

View File

@@ -23,6 +23,7 @@ pub mod encryption;
pub mod events;
mod future;
pub mod identifiers;
pub mod identities;
pub mod machine;
pub mod olm;
mod macros;

View File

@@ -10,7 +10,7 @@ use wasm_bindgen::prelude::*;
use crate::{
device, downcast, encryption,
future::future_to_promise,
identifiers, olm, requests,
identifiers, identities, olm, requests,
requests::OutgoingRequest,
responses::{self, response_from_string},
store, sync_events, types, verification, vodozemac,
@@ -413,6 +413,17 @@ impl OlmMachine {
})
}
/// Get the cross signing user identity of a user.
#[wasm_bindgen(js_name = "getIdentity")]
pub fn get_identity(&self, user_id: &identifiers::UserId) -> Promise {
let me = self.inner.clone();
let user_id = user_id.inner.clone();
future_to_promise(async move {
Ok(me.get_identity(user_id.as_ref(), None).await?.map(identities::UserIdentities::from))
})
}
/// Sign the given message using our device key and if available
/// cross-signing master key.
pub fn sign(&self, message: String) -> Promise {