rust: Bind the initial verification request type and methods

This commit is contained in:
Damir Jelić 2021-06-17 13:36:44 +02:00
parent c0bac69733
commit e46578a087
4 changed files with 158 additions and 8 deletions

View file

@ -24,11 +24,11 @@ features = ["lax_deserialize"]
[dependencies.matrix-sdk-common]
git = "https://github.com/matrix-org/matrix-rust-sdk/"
branch = "verification-qr"
rev = "0fb3dedd1cd3b0766fa7378754480d52d38e8ef2"
[dependencies.matrix-sdk-crypto]
git = "https://github.com/matrix-org/matrix-rust-sdk/"
branch = "verification-qr"
rev = "0fb3dedd1cd3b0766fa7378754480d52d38e8ef2"
features = ["sled_cryptostore"]
[dependencies.tokio]

View file

@ -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};
pub use machine::{KeyRequestPair, OlmMachine, Sas, VerificationRequest};
pub use responses::{
DeviceLists, KeysImportResult, OutgoingVerificationRequest, Request, RequestType,
};
@ -30,4 +30,35 @@ pub struct DecryptedEvent {
pub forwarding_curve25519_chain: Vec<String>,
}
pub enum CancelCode {
User,
Timeout,
UnknownTransaction,
UnknownMethod,
UnexpectedMessage,
KeyMismatch,
UserMismatch,
InvalidMessage,
Accepted,
}
impl From<ruma::events::key::verification::cancel::CancelCode> for CancelCode {
fn from(c: ruma::events::key::verification::cancel::CancelCode) -> Self {
use ruma::events::key::verification::cancel::CancelCode as RumaCancelCode;
match c {
RumaCancelCode::User => Self::User,
RumaCancelCode::Timeout => Self::Timeout,
RumaCancelCode::UnknownTransaction => Self::UnknownTransaction,
RumaCancelCode::UnknownMethod => Self::UnknownMethod,
RumaCancelCode::UnexpectedMessage => Self::UnexpectedMessage,
RumaCancelCode::KeyMismatch => Self::KeyMismatch,
RumaCancelCode::UserMismatch => Self::UserMismatch,
RumaCancelCode::InvalidMessage => Self::InvalidMessage,
RumaCancelCode::Accepted => Self::Accepted,
RumaCancelCode::_Custom(_) => Self::User,
}
}
}
include!(concat!(env!("OUT_DIR"), "/olm.uniffi.rs"));

View file

@ -18,8 +18,8 @@ use ruma::{
IncomingResponse,
},
events::{
room::encrypted::EncryptedEventContent, AnyMessageEventContent, EventContent,
SyncMessageEvent,
key::verification::VerificationMethod, room::encrypted::EncryptedEventContent,
AnyMessageEventContent, EventContent, SyncMessageEvent,
},
DeviceKeyAlgorithm, RoomId, UserId,
};
@ -30,14 +30,14 @@ 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,
Sas as InnerSas, VerificationRequest as InnerVerificationRequest,
};
use crate::{
error::{CryptoStoreError, DecryptionError, MachineCreationError},
responses::{response_from_string, OutgoingVerificationRequest, OwnedResponse},
DecryptedEvent, Device, DeviceLists, KeyImportError, KeysImportResult, ProgressListener,
Request, RequestType,
CancelCode, DecryptedEvent, Device, DeviceLists, KeyImportError, KeysImportResult,
ProgressListener, Request, RequestType,
};
/// A high level state machine that handles E2EE for Matrix.
@ -70,6 +70,44 @@ impl From<InnerSas> for Sas {
}
}
pub struct VerificationRequest {
pub other_user_id: String,
pub other_device_id: Option<String>,
pub flow_id: String,
pub is_cancelled: bool,
pub is_done: bool,
pub is_ready: bool,
pub room_id: Option<String>,
pub cancel_code: Option<CancelCode>,
pub we_started: bool,
pub is_passive: bool,
pub their_methods: Option<Vec<String>>,
pub our_methods: Option<Vec<String>>,
}
impl From<InnerVerificationRequest> for VerificationRequest {
fn from(v: InnerVerificationRequest) -> Self {
Self {
other_user_id: v.other_user().to_string(),
other_device_id: v.other_device_id().map(|d| d.to_string()),
flow_id: v.flow_id().as_str().to_owned(),
is_cancelled: v.is_cancelled(),
is_done: v.is_done(),
is_ready: v.is_ready(),
room_id: v.room_id().map(|r| r.to_string()),
cancel_code: v.cancel_code().map(|c| c.clone().into()),
we_started: v.we_started(),
is_passive: v.is_passive(),
their_methods: v
.their_supported_methods()
.map(|v| v.into_iter().map(|m| m.to_string()).collect()),
our_methods: v
.our_supported_methods()
.map(|v| v.into_iter().map(|m| m.to_string()).collect()),
}
}
}
/// A pair of outgoing room key requests, both of those are sendToDevice
/// requests.
pub struct KeyRequestPair {
@ -556,6 +594,51 @@ impl OlmMachine {
Ok(())
}
pub fn get_verification_requests(&self, user_id: &str) -> Vec<VerificationRequest> {
let user_id = if let Ok(user_id) = UserId::try_from(user_id) {
user_id
} else {
return vec![];
};
self.inner
.get_verification_requests(&user_id)
.into_iter()
.map(|v| v.into())
.collect()
}
pub fn get_verification_request(
&self,
user_id: &str,
flow_id: &str,
) -> Option<VerificationRequest> {
let user_id = UserId::try_from(user_id).ok()?;
self.inner
.get_verification_request(&user_id, flow_id)
.map(|v| v.into())
}
pub fn accept_verification_request(
&self,
user_id: &str,
flow_id: &str,
methods: Vec<String>,
) -> Option<OutgoingVerificationRequest> {
let user_id = UserId::try_from(user_id).ok()?;
let methods = methods
.into_iter()
.map(|m| VerificationMethod::from(m))
.collect();
if let Some(verification) = self.inner.get_verification_request(&user_id, flow_id) {
verification.accept_with_methods(methods).map(|r| r.into())
} else {
None
}
}
pub fn get_verification(&self, flow_id: &str) -> Option<Sas> {
todo!()
// self.inner.get_verification(flow_id).map(|s| s.into())

View file

@ -73,6 +73,34 @@ dictionary Sas {
boolean timed_out;
};
dictionary VerificationRequest {
string other_user_id;
string? other_device_id;
string flow_id;
boolean is_cancelled;
boolean is_done;
boolean is_ready;
boolean we_started;
boolean is_passive;
string? room_id;
CancelCode? cancel_code;
sequence<string>? their_methods;
sequence<string>? our_methods;
};
enum CancelCode {
"User",
"Timeout",
"UnknownTransaction",
"UnknownMethod",
"UnexpectedMessage",
"KeyMismatch",
"UserMismatch",
"InvalidMessage",
"Accepted",
};
dictionary KeyRequestPair {
Request? cancellation;
Request key_request;
@ -136,8 +164,16 @@ interface OlmMachine {
[Throws=CryptoStoreError]
sequence<Request> share_room_key([ByRef] string room_id, sequence<string> users);
sequence<VerificationRequest> 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);
OutgoingVerificationRequest? accept_verification_request(
[ByRef] string user_id,
[ByRef] string flow_id,
sequence<string> methods
);
[Throws=CryptoStoreError]
Sas start_verification([ByRef] Device device);
[Throws=CryptoStoreError]