rust: Rework the rest of the sas verification methods

This commit is contained in:
Damir Jelić 2021-06-18 12:16:38 +02:00
parent a4e1a5bbcb
commit 0cb9f6be10
3 changed files with 106 additions and 58 deletions

View File

@ -7,7 +7,7 @@ mod responses;
pub use device::Device; pub use device::Device;
pub use error::{CryptoStoreError, DecryptionError, KeyImportError, MachineCreationError}; pub use error::{CryptoStoreError, DecryptionError, KeyImportError, MachineCreationError};
pub use logger::{set_logger, Logger}; pub use logger::{set_logger, Logger};
pub use machine::{KeyRequestPair, OlmMachine, Sas, VerificationRequest}; pub use machine::{KeyRequestPair, OlmMachine, Sas, VerificationRequest, StartSasResult};
pub use responses::{ pub use responses::{
DeviceLists, KeysImportResult, OutgoingVerificationRequest, Request, RequestType, DeviceLists, KeysImportResult, OutgoingVerificationRequest, Request, RequestType,
}; };

View File

@ -30,7 +30,7 @@ use tokio::runtime::Runtime;
use matrix_sdk_common::{deserialized_responses::AlgorithmInfo, uuid::Uuid}; use matrix_sdk_common::{deserialized_responses::AlgorithmInfo, uuid::Uuid};
use matrix_sdk_crypto::{ use matrix_sdk_crypto::{
decrypt_key_export, encrypt_key_export, EncryptionSettings, OlmMachine as InnerMachine, decrypt_key_export, encrypt_key_export, EncryptionSettings, OlmMachine as InnerMachine,
Sas as InnerSas, VerificationRequest as InnerVerificationRequest, Sas as InnerSas, Verification, VerificationRequest as InnerVerificationRequest,
}; };
use crate::{ use crate::{
@ -56,6 +56,11 @@ pub struct Sas {
pub timed_out: bool, pub timed_out: bool,
} }
pub struct StartSasResult {
pub sas: Sas,
pub request: OutgoingVerificationRequest,
}
impl From<InnerSas> for Sas { impl From<InnerSas> for Sas {
fn from(sas: InnerSas) -> Self { fn from(sas: InnerSas) -> Self {
Self { Self {
@ -639,72 +644,110 @@ impl OlmMachine {
} }
} }
pub fn get_verification(&self, flow_id: &str) -> Option<Sas> { pub fn request_verification(&self, user_id: &str) {
todo!() let _user_id = UserId::try_from(user_id).unwrap();
// self.inner.get_verification(flow_id).map(|s| s.into())
}
pub fn request_verification(&self) {
todo!() todo!()
} }
pub fn start_verification(&self, device: &Device) -> Result<Sas, CryptoStoreError> { pub fn get_verification(&self, user_id: &str, _flow_id: &str) -> Option<Sas> {
let user_id = UserId::try_from(device.user_id.clone())?; let _user_id = UserId::try_from(user_id).ok()?;
let device_id = device.device_id.as_str().into(); todo!()
// TODO remove the unwrap
let device = self
.runtime
.block_on(self.inner.get_device(&user_id, device_id))?
.unwrap();
// TODO we need to return the request as well.
let (sas, _) = self.runtime.block_on(device.start_verification())?;
Ok(sas.into())
} }
pub fn accept_verification(&self, flow_id: &str) -> Option<OutgoingVerificationRequest> { pub fn start_sas_verification(
todo!() &self,
// self.inner user_id: &str,
// .get_verification(flow_id) flow_id: &str,
// .and_then(|s| s.accept().map(|r| r.into())) ) -> Result<Option<StartSasResult>, CryptoStoreError> {
let user_id = UserId::try_from(user_id)?;
Ok(
if let Some(verification) = self.inner.get_verification_request(&user_id, flow_id) {
self.runtime
.block_on(verification.start_sas())?
.map(|(sas, r)| StartSasResult {
sas: sas.into(),
request: r.into(),
})
} else {
None
},
)
} }
pub fn cancel_verification(&self, flow_id: &str) -> Option<OutgoingVerificationRequest> { pub fn accept_sas_verification(
todo!() &self,
// self.inner user_id: &str,
// .get_verification(flow_id) flow_id: &str,
// .and_then(|s| s.cancel().map(|r| r.into())) ) -> Option<OutgoingVerificationRequest> {
let user_id = UserId::try_from(user_id).ok()?;
self.inner
.get_verification(&user_id, flow_id)
.and_then(|s| s.sas_v1())
.and_then(|s| s.accept().map(|r| r.into()))
}
pub fn cancel_verification(
&self,
user_id: &str,
flow_id: &str,
) -> Option<OutgoingVerificationRequest> {
let user_id = UserId::try_from(user_id).ok()?;
if let Some(verification) = self.inner.get_verification(&user_id, flow_id) {
match verification {
Verification::SasV1(v) => v.cancel().map(|r| r.into()),
Verification::QrV1(v) => v.cancel().map(|r| r.into()),
}
} else {
None
}
} }
pub fn confirm_verification( pub fn confirm_verification(
&self, &self,
user_id: &str,
flow_id: &str, flow_id: &str,
) -> Result<Option<OutgoingVerificationRequest>, CryptoStoreError> { ) -> Result<Option<OutgoingVerificationRequest>, CryptoStoreError> {
todo!() let user_id = UserId::try_from(user_id)?;
// let sas = self.inner.get_verification(flow_id);
// if let Some(sas) = sas { Ok(
// let (request, _) = self.runtime.block_on(sas.confirm())?; if let Some(verification) = self.inner.get_verification(&user_id, flow_id) {
// Ok(request.map(|r| r.into())) match verification {
// } else { Verification::SasV1(v) => {
// Ok(None) self.runtime.block_on(v.confirm())?.0.map(|r| r.into())
// } }
Verification::QrV1(v) => v.confirm_scanning().map(|r| r.into()),
}
} else {
None
},
)
} }
pub fn get_emoji_index(&self, flow_id: &str) -> Option<Vec<i32>> { pub fn get_emoji_index(&self, user_id: &str, flow_id: &str) -> Option<Vec<i32>> {
todo!() let user_id = UserId::try_from(user_id).ok()?;
// self.inner.get_verification(flow_id).and_then(|s| {
// s.emoji_index() self.inner
// .map(|v| v.iter().map(|i| (*i).into()).collect()) .get_verification(&user_id, flow_id)
// }) .and_then(|s| {
s.sas_v1().and_then(|s| {
s.emoji_index()
.map(|v| v.iter().map(|i| (*i).into()).collect())
})
})
} }
pub fn get_decimals(&self, flow_id: &str) -> Option<Vec<i32>> { pub fn get_decimals(&self, user_id: &str, flow_id: &str) -> Option<Vec<i32>> {
todo!() let user_id = UserId::try_from(user_id).ok()?;
// self.inner.get_verification(flow_id).and_then(|s| {
// s.decimals() self.inner
// .map(|v| [v.0.into(), v.1.into(), v.2.into()].to_vec()) .get_verification(&user_id, flow_id)
// }) .and_then(|s| {
s.sas_v1().and_then(|s| {
s.decimals()
.map(|v| [v.0.into(), v.1.into(), v.2.into()].to_vec())
})
})
} }
} }

View File

@ -63,6 +63,11 @@ dictionary Device {
boolean is_blocked; boolean is_blocked;
}; };
dictionary StartSasResult {
Sas sas;
OutgoingVerificationRequest request;
};
dictionary Sas { dictionary Sas {
string other_user_id; string other_user_id;
string other_device_id; string other_device_id;
@ -166,7 +171,7 @@ interface OlmMachine {
sequence<VerificationRequest> get_verification_requests([ByRef] string user_id); sequence<VerificationRequest> get_verification_requests([ByRef] string user_id);
VerificationRequest? get_verification_request([ByRef] string user_id, [ByRef] string flow_id); VerificationRequest? get_verification_request([ByRef] string user_id, [ByRef] string flow_id);
Sas? get_verification([ByRef] string flow_id); Sas? get_verification([ByRef] string user_id, [ByRef] string flow_id);
OutgoingVerificationRequest? accept_verification_request( OutgoingVerificationRequest? accept_verification_request(
[ByRef] string user_id, [ByRef] string user_id,
@ -175,14 +180,14 @@ interface OlmMachine {
); );
[Throws=CryptoStoreError] [Throws=CryptoStoreError]
Sas start_verification([ByRef] Device device); StartSasResult? start_sas_verification([ByRef] string user_id, [ByRef] string flow_id);
[Throws=CryptoStoreError] [Throws=CryptoStoreError]
OutgoingVerificationRequest? confirm_verification([ByRef] string flow_id); OutgoingVerificationRequest? confirm_verification([ByRef] string user_id, [ByRef] string flow_id);
OutgoingVerificationRequest? cancel_verification([ByRef] string flow_id); OutgoingVerificationRequest? cancel_verification([ByRef] string user_id, [ByRef] string flow_id);
OutgoingVerificationRequest? accept_verification([ByRef] string flow_id); OutgoingVerificationRequest? accept_sas_verification([ByRef] string user_id, [ByRef] string flow_id);
sequence<i32>? get_emoji_index([ByRef] string flow_id); sequence<i32>? get_emoji_index([ByRef] string user_id, [ByRef] string flow_id);
sequence<i32>? get_decimals([ByRef] string flow_id); sequence<i32>? get_decimals([ByRef] string user_id, [ByRef] string flow_id);
[Throws=DecryptionError] [Throws=DecryptionError]
KeyRequestPair request_room_key([ByRef] string event, [ByRef] string room_id); KeyRequestPair request_room_key([ByRef] string event, [ByRef] string room_id);