diff --git a/rust-sdk/src/lib.rs b/rust-sdk/src/lib.rs index c1c37f3455..fea61ce7b5 100644 --- a/rust-sdk/src/lib.rs +++ b/rust-sdk/src/lib.rs @@ -7,7 +7,7 @@ mod responses; pub use device::Device; pub use error::{CryptoStoreError, DecryptionError, KeyImportError, MachineCreationError}; pub use logger::{set_logger, Logger}; -pub use machine::{KeyRequestPair, OlmMachine, Sas, VerificationRequest}; +pub use machine::{KeyRequestPair, OlmMachine, Sas, VerificationRequest, StartSasResult}; pub use responses::{ DeviceLists, KeysImportResult, OutgoingVerificationRequest, Request, RequestType, }; diff --git a/rust-sdk/src/machine.rs b/rust-sdk/src/machine.rs index 4cd216977a..ff657953ef 100644 --- a/rust-sdk/src/machine.rs +++ b/rust-sdk/src/machine.rs @@ -30,7 +30,7 @@ use tokio::runtime::Runtime; use matrix_sdk_common::{deserialized_responses::AlgorithmInfo, uuid::Uuid}; use matrix_sdk_crypto::{ 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::{ @@ -56,6 +56,11 @@ pub struct Sas { pub timed_out: bool, } +pub struct StartSasResult { + pub sas: Sas, + pub request: OutgoingVerificationRequest, +} + impl From for Sas { fn from(sas: InnerSas) -> Self { Self { @@ -639,72 +644,110 @@ impl OlmMachine { } } - pub fn get_verification(&self, flow_id: &str) -> Option { - todo!() - // self.inner.get_verification(flow_id).map(|s| s.into()) - } - - pub fn request_verification(&self) { + pub fn request_verification(&self, user_id: &str) { + let _user_id = UserId::try_from(user_id).unwrap(); todo!() } - pub fn start_verification(&self, device: &Device) -> Result { - let user_id = UserId::try_from(device.user_id.clone())?; - let device_id = device.device_id.as_str().into(); - // 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 get_verification(&self, user_id: &str, _flow_id: &str) -> Option { + let _user_id = UserId::try_from(user_id).ok()?; + todo!() } - pub fn accept_verification(&self, flow_id: &str) -> Option { - todo!() - // self.inner - // .get_verification(flow_id) - // .and_then(|s| s.accept().map(|r| r.into())) + pub fn start_sas_verification( + &self, + user_id: &str, + flow_id: &str, + ) -> Result, 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 { - todo!() - // self.inner - // .get_verification(flow_id) - // .and_then(|s| s.cancel().map(|r| r.into())) + pub fn accept_sas_verification( + &self, + user_id: &str, + flow_id: &str, + ) -> Option { + 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 { + 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( &self, + user_id: &str, flow_id: &str, ) -> Result, CryptoStoreError> { - todo!() - // let sas = self.inner.get_verification(flow_id); + let user_id = UserId::try_from(user_id)?; - // if let Some(sas) = sas { - // let (request, _) = self.runtime.block_on(sas.confirm())?; - // Ok(request.map(|r| r.into())) - // } else { - // Ok(None) - // } + Ok( + if let Some(verification) = self.inner.get_verification(&user_id, flow_id) { + match verification { + Verification::SasV1(v) => { + 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> { - todo!() - // self.inner.get_verification(flow_id).and_then(|s| { - // s.emoji_index() - // .map(|v| v.iter().map(|i| (*i).into()).collect()) - // }) + pub fn get_emoji_index(&self, user_id: &str, flow_id: &str) -> Option> { + 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.emoji_index() + .map(|v| v.iter().map(|i| (*i).into()).collect()) + }) + }) } - pub fn get_decimals(&self, flow_id: &str) -> Option> { - todo!() - // self.inner.get_verification(flow_id).and_then(|s| { - // s.decimals() - // .map(|v| [v.0.into(), v.1.into(), v.2.into()].to_vec()) - // }) + pub fn get_decimals(&self, user_id: &str, flow_id: &str) -> Option> { + 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.decimals() + .map(|v| [v.0.into(), v.1.into(), v.2.into()].to_vec()) + }) + }) } } diff --git a/rust-sdk/src/olm.udl b/rust-sdk/src/olm.udl index 01b0b84135..be552c6643 100644 --- a/rust-sdk/src/olm.udl +++ b/rust-sdk/src/olm.udl @@ -63,6 +63,11 @@ dictionary Device { boolean is_blocked; }; +dictionary StartSasResult { + Sas sas; + OutgoingVerificationRequest request; +}; + dictionary Sas { string other_user_id; string other_device_id; @@ -166,7 +171,7 @@ interface OlmMachine { sequence get_verification_requests([ByRef] string user_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( [ByRef] string user_id, @@ -175,14 +180,14 @@ interface OlmMachine { ); [Throws=CryptoStoreError] - Sas start_verification([ByRef] Device device); + StartSasResult? start_sas_verification([ByRef] string user_id, [ByRef] string flow_id); [Throws=CryptoStoreError] - OutgoingVerificationRequest? confirm_verification([ByRef] string flow_id); - OutgoingVerificationRequest? cancel_verification([ByRef] string flow_id); - OutgoingVerificationRequest? accept_verification([ByRef] string flow_id); + OutgoingVerificationRequest? confirm_verification([ByRef] string user_id, [ByRef] string flow_id); + OutgoingVerificationRequest? cancel_verification([ByRef] string user_id, [ByRef] string flow_id); + OutgoingVerificationRequest? accept_sas_verification([ByRef] string user_id, [ByRef] string flow_id); - sequence? get_emoji_index([ByRef] string flow_id); - sequence? get_decimals([ByRef] string flow_id); + sequence? get_emoji_index([ByRef] string user_id, [ByRef] string flow_id); + sequence? get_decimals([ByRef] string user_id, [ByRef] string flow_id); [Throws=DecryptionError] KeyRequestPair request_room_key([ByRef] string event, [ByRef] string room_id);