crypto: Add public methods to request verifications with devices

master
Damir Jelić 2021-07-19 10:26:39 +02:00
parent 8f03679935
commit 5566886f20
1 changed files with 46 additions and 3 deletions

View File

@ -28,7 +28,8 @@ use ruma::{
encryption::{DeviceKeys, SignedKey}, encryption::{DeviceKeys, SignedKey},
events::{ events::{
forwarded_room_key::ForwardedRoomKeyToDeviceEventContent, forwarded_room_key::ForwardedRoomKeyToDeviceEventContent,
room::encrypted::EncryptedEventContent, AnyToDeviceEventContent, key::verification::VerificationMethod, room::encrypted::EncryptedEventContent,
AnyToDeviceEventContent,
}, },
DeviceId, DeviceIdBox, DeviceKeyAlgorithm, DeviceKeyId, EventEncryptionAlgorithm, UserId, DeviceId, DeviceIdBox, DeviceKeyAlgorithm, DeviceKeyId, EventEncryptionAlgorithm, UserId,
}; };
@ -43,7 +44,7 @@ use crate::{
olm::{InboundGroupSession, PrivateCrossSigningIdentity, Session, Utility}, olm::{InboundGroupSession, PrivateCrossSigningIdentity, Session, Utility},
store::{Changes, CryptoStore, DeviceChanges, Result as StoreResult}, store::{Changes, CryptoStore, DeviceChanges, Result as StoreResult},
verification::VerificationMachine, verification::VerificationMachine,
OutgoingVerificationRequest, Sas, ToDeviceRequest, OutgoingVerificationRequest, Sas, ToDeviceRequest, VerificationRequest,
}; };
#[cfg(test)] #[cfg(test)]
use crate::{OlmMachine, ReadOnlyAccount}; use crate::{OlmMachine, ReadOnlyAccount};
@ -125,7 +126,13 @@ impl Deref for Device {
impl Device { impl Device {
/// Start a interactive verification with this `Device` /// Start a interactive verification with this `Device`
/// ///
/// Returns a `Sas` object and to-device request that needs to be sent out. /// Returns a `Sas` object and a to-device request that needs to be sent
/// out.
///
/// This method has been deprecated in the spec and the
/// [`request_verification()`] method should be used instead.
///
/// [`request_verification()`]: #method.request_verification
pub async fn start_verification(&self) -> StoreResult<(Sas, ToDeviceRequest)> { pub async fn start_verification(&self) -> StoreResult<(Sas, ToDeviceRequest)> {
let (sas, request) = self.verification_machine.start_sas(self.inner.clone()).await?; let (sas, request) = self.verification_machine.start_sas(self.inner.clone()).await?;
@ -136,6 +143,42 @@ impl Device {
} }
} }
/// Request an interacitve verification with this `Device`
///
/// Returns a `VerificationRequest` object and a to-device request that
/// needs to be sent out.
pub async fn request_verification(&self) -> (VerificationRequest, OutgoingVerificationRequest) {
self.request_verification_helper(None).await
}
/// Request an interacitve verification with this `Device`
///
/// Returns a `VerificationRequest` object and a to-device request that
/// needs to be sent out.
///
/// # Arguments
///
/// * `methods` - The verification methods that we want to support.
pub async fn request_verification_with_methods(
&self,
methods: Vec<VerificationMethod>,
) -> (VerificationRequest, OutgoingVerificationRequest) {
self.request_verification_helper(Some(methods)).await
}
async fn request_verification_helper(
&self,
methods: Option<Vec<VerificationMethod>>,
) -> (VerificationRequest, OutgoingVerificationRequest) {
self.verification_machine
.request_to_device_verification(
self.user_id(),
vec![self.device_id().to_owned()],
methods,
)
.await
}
/// Get the Olm sessions that belong to this device. /// Get the Olm sessions that belong to this device.
pub(crate) async fn get_sessions(&self) -> StoreResult<Option<Arc<Mutex<Vec<Session>>>>> { pub(crate) async fn get_sessions(&self) -> StoreResult<Option<Arc<Mutex<Vec<Session>>>>> {
if let Some(k) = self.get_key(DeviceKeyAlgorithm::Curve25519) { if let Some(k) = self.get_key(DeviceKeyAlgorithm::Curve25519) {