Merge branch 'up-ruma' into master

master
Damir Jelić 2020-08-10 14:58:47 +02:00
commit ef5201cf35
17 changed files with 205 additions and 199 deletions

View File

@ -24,6 +24,8 @@ use std::{
sync::Arc, sync::Arc,
}; };
#[cfg(feature = "encryption")]
use matrix_sdk_common::api::r0::to_device::send_event_to_device::Request as ToDeviceRequest;
use matrix_sdk_common::{ use matrix_sdk_common::{
identifiers::ServerName, identifiers::ServerName,
instant::{Duration, Instant}, instant::{Duration, Instant},
@ -31,7 +33,7 @@ use matrix_sdk_common::{
locks::RwLock, locks::RwLock,
presence::PresenceState, presence::PresenceState,
uuid::Uuid, uuid::Uuid,
FromHttpRequestError, FromHttpResponseError, Outgoing, FromHttpResponseError,
}; };
use futures_timer::Delay as sleep; use futures_timer::Delay as sleep;
@ -272,10 +274,6 @@ impl SyncSettings {
} }
} }
#[cfg(feature = "encryption")]
use api::r0::keys::{claim_keys, get_keys, upload_keys, KeyAlgorithm};
#[cfg(feature = "encryption")]
use api::r0::to_device::send_event_to_device;
use api::r0::{ use api::r0::{
account::register, account::register,
directory::{get_public_rooms, get_public_rooms_filtered}, directory::{get_public_rooms, get_public_rooms_filtered},
@ -284,7 +282,7 @@ use api::r0::{
invite_user::{self, InvitationRecipient}, invite_user::{self, InvitationRecipient},
join_room_by_id, join_room_by_id_or_alias, kick_user, leave_room, Invite3pid, join_room_by_id, join_room_by_id_or_alias, kick_user, leave_room, Invite3pid,
}, },
message::{create_message_event, get_message_events}, message::{get_message_events, send_message_event},
read_marker::set_read_marker, read_marker::set_read_marker,
receipt::create_receipt, receipt::create_receipt,
room::create_room, room::create_room,
@ -292,6 +290,14 @@ use api::r0::{
sync::sync_events, sync::sync_events,
typing::create_typing_event, typing::create_typing_event,
}; };
#[cfg(feature = "encryption")]
use matrix_sdk_common::{
api::r0::{
keys::{claim_keys, get_keys, upload_keys},
to_device::send_event_to_device,
},
identifiers::DeviceKeyAlgorithm,
};
impl Client { impl Client {
/// Creates a new client for making HTTP requests to the given homeserver. /// Creates a new client for making HTTP requests to the given homeserver.
@ -739,8 +745,6 @@ impl Client {
server: Option<&str>, server: Option<&str>,
) -> Result<get_public_rooms::Response> { ) -> Result<get_public_rooms::Response> {
let limit = limit.map(|n| UInt::try_from(n).ok()).flatten(); let limit = limit.map(|n| UInt::try_from(n).ok()).flatten();
let since = since.map(ToString::to_string);
let server = server.map(ToString::to_string);
let request = get_public_rooms::Request { let request = get_public_rooms::Request {
limit, limit,
@ -775,13 +779,13 @@ impl Client {
/// let mut builder = RoomListFilterBuilder::new(); /// let mut builder = RoomListFilterBuilder::new();
/// builder /// builder
/// .filter(Filter { generic_search_term, }) /// .filter(Filter { generic_search_term, })
/// .since(last_sync_token) /// .since(&last_sync_token)
/// .room_network(RoomNetwork::Matrix); /// .room_network(RoomNetwork::Matrix);
/// ///
/// client.public_rooms_filtered(builder).await; /// client.public_rooms_filtered(builder).await;
/// # }) /// # })
/// ``` /// ```
pub async fn public_rooms_filtered<R: Into<get_public_rooms_filtered::Request>>( pub async fn public_rooms_filtered<'a, R: Into<get_public_rooms_filtered::Request<'a>>>(
&self, &self,
room_search: R, room_search: R,
) -> Result<get_public_rooms_filtered::Response> { ) -> Result<get_public_rooms_filtered::Response> {
@ -830,7 +834,7 @@ impl Client {
/// `MessagesRequestBuilder`s `from` field as a starting point. /// `MessagesRequestBuilder`s `from` field as a starting point.
/// ///
/// Sends a request to `/_matrix/client/r0/rooms/{room_id}/messages` and /// Sends a request to `/_matrix/client/r0/rooms/{room_id}/messages` and
/// returns a `get_message_events::IncomingResponse` that contains chunks /// returns a `get_message_events::Response` that contains chunks
/// of `RoomEvents`. /// of `RoomEvents`.
/// ///
/// # Arguments /// # Arguments
@ -991,7 +995,7 @@ impl Client {
room_id: &RoomId, room_id: &RoomId,
content: MessageEventContent, content: MessageEventContent,
txn_id: Option<Uuid>, txn_id: Option<Uuid>,
) -> Result<create_message_event::Response> { ) -> Result<send_message_event::Response> {
#[allow(unused_mut)] #[allow(unused_mut)]
let mut event_type = EventType::RoomMessage; let mut event_type = EventType::RoomMessage;
#[allow(unused_mut)] #[allow(unused_mut)]
@ -1044,10 +1048,10 @@ impl Client {
} }
} }
let request = create_message_event::Request { let request = send_message_event::Request {
room_id: room_id.clone(), room_id: &room_id,
event_type, event_type,
txn_id: txn_id.unwrap_or_else(Uuid::new_v4).to_string(), txn_id: &txn_id.unwrap_or_else(Uuid::new_v4).to_string(),
data: raw_content, data: raw_content,
}; };
@ -1093,19 +1097,10 @@ impl Client {
/// // returned /// // returned
/// # }) /// # })
/// ``` /// ```
pub async fn send<Request>( pub async fn send<Request>(&self, request: Request) -> Result<Request::IncomingResponse>
&self,
request: Request,
) -> Result<<Request::Response as Outgoing>::Incoming>
where where
Request: Endpoint + Debug, Request: Endpoint + Debug,
<Request as Outgoing>::Incoming: crate::Error: From<FromHttpResponseError<Request::ResponseError>>,
TryFrom<http::Request<Vec<u8>>, Error = FromHttpRequestError>,
<Request::Response as Outgoing>::Incoming: TryFrom<
http::Response<Vec<u8>>,
Error = FromHttpResponseError<<Request as Endpoint>::ResponseError>,
>,
crate::Error: From<FromHttpResponseError<<Request as Endpoint>::ResponseError>>,
{ {
self.http_client self.http_client
.send(request, self.base_client.session().clone()) .send(request, self.base_client.session().clone())
@ -1240,12 +1235,16 @@ impl Client {
} }
} }
for request in self.base_client.outgoing_to_device_requests().await { for req in self.base_client.outgoing_to_device_requests().await {
let transaction_id = request.txn_id.clone(); let request = ToDeviceRequest {
event_type: req.event_type,
txn_id: &req.txn_id,
messages: req.messages,
};
if self.send(request).await.is_ok() { if self.send(request).await.is_ok() {
self.base_client self.base_client
.mark_to_device_request_as_sent(&transaction_id) .mark_to_device_request_as_sent(&req.txn_id)
.await; .await;
} }
} }
@ -1292,7 +1291,7 @@ impl Client {
#[instrument] #[instrument]
async fn claim_one_time_keys( async fn claim_one_time_keys(
&self, &self,
one_time_keys: BTreeMap<UserId, BTreeMap<Box<DeviceId>, KeyAlgorithm>>, one_time_keys: BTreeMap<UserId, BTreeMap<Box<DeviceId>, DeviceKeyAlgorithm>>,
) -> Result<claim_keys::Response> { ) -> Result<claim_keys::Response> {
let request = claim_keys::Request { let request = claim_keys::Request {
timeout: None, timeout: None,
@ -1326,7 +1325,13 @@ impl Client {
.await .await
.expect("Keys don't need to be uploaded"); .expect("Keys don't need to be uploaded");
for request in requests.drain(..) { for req in requests.drain(..) {
let request = ToDeviceRequest {
event_type: req.event_type,
txn_id: &req.txn_id,
messages: req.messages,
};
let _response: send_event_to_device::Response = self.send(request).await?; let _response: send_event_to_device::Response = self.send(request).await?;
} }
@ -1444,14 +1449,14 @@ mod test {
set_read_marker, Client, ClientConfig, Invite3pid, MessageEventContent, Session, set_read_marker, Client, ClientConfig, Invite3pid, MessageEventContent, Session,
SyncSettings, Url, SyncSettings, Url,
}; };
use crate::events::room::message::TextMessageEventContent; use crate::{RegistrationBuilder, RoomListFilterBuilder};
use crate::{
identifiers::{event_id, room_id, user_id},
RegistrationBuilder, RoomListFilterBuilder,
};
use matrix_sdk_base::JsonStore; use matrix_sdk_base::JsonStore;
use matrix_sdk_common::{
events::room::message::TextMessageEventContent,
identifiers::{event_id, room_id, user_id},
thirdparty,
};
use matrix_sdk_test::{test_json, EventBuilder, EventsJson}; use matrix_sdk_test::{test_json, EventBuilder, EventsJson};
use mockito::{mock, Matcher}; use mockito::{mock, Matcher};
use tempfile::tempdir; use tempfile::tempdir;
@ -1785,7 +1790,7 @@ mod test {
&Invite3pid { &Invite3pid {
id_server: "example.org".to_string(), id_server: "example.org".to_string(),
id_access_token: "IdToken".to_string(), id_access_token: "IdToken".to_string(),
medium: crate::api::r0::thirdparty::Medium::Email, medium: thirdparty::Medium::Email,
address: "address".to_string(), address: "address".to_string(),
}, },
) )

View File

@ -22,7 +22,7 @@ use reqwest::{Client, Response};
use tracing::trace; use tracing::trace;
use url::Url; use url::Url;
use matrix_sdk_common::{locks::RwLock, FromHttpRequestError, FromHttpResponseError, Outgoing}; use matrix_sdk_common::{locks::RwLock, FromHttpResponseError};
use crate::{Endpoint, Error, Result, Session}; use crate::{Endpoint, Error, Result, Session};
@ -33,20 +33,11 @@ pub(crate) struct HttpClient {
} }
impl HttpClient { impl HttpClient {
async fn send_request<Request>( async fn send_request<Request: Endpoint>(
&self, &self,
request: Request, request: Request,
session: Arc<RwLock<Option<Session>>>, session: Arc<RwLock<Option<Session>>>,
) -> Result<Response> ) -> Result<Response> {
where
Request: Endpoint,
<Request as Outgoing>::Incoming:
TryFrom<http::Request<Vec<u8>>, Error = FromHttpRequestError>,
<Request::Response as Outgoing>::Incoming: TryFrom<
http::Response<Vec<u8>>,
Error = FromHttpResponseError<<Request as Endpoint>::ResponseError>,
>,
{
let mut request = { let mut request = {
let read_guard; let read_guard;
let access_token = if Request::METADATA.requires_authentication { let access_token = if Request::METADATA.requires_authentication {
@ -95,16 +86,10 @@ impl HttpClient {
&self, &self,
request: Request, request: Request,
session: Arc<RwLock<Option<Session>>>, session: Arc<RwLock<Option<Session>>>,
) -> Result<<Request::Response as Outgoing>::Incoming> ) -> Result<Request::IncomingResponse>
where where
Request: Endpoint, Request: Endpoint,
<Request as Outgoing>::Incoming: Error: From<FromHttpResponseError<Request::ResponseError>>,
TryFrom<http::Request<Vec<u8>>, Error = FromHttpRequestError>,
<Request::Response as Outgoing>::Incoming: TryFrom<
http::Response<Vec<u8>>,
Error = FromHttpResponseError<<Request as Endpoint>::ResponseError>,
>,
Error: From<FromHttpResponseError<<Request as Endpoint>::ResponseError>>,
{ {
let response = self.send_request(request, session).await?; let response = self.send_request(request, session).await?;
@ -112,8 +97,6 @@ impl HttpClient {
let response = self.response_to_http_response(response).await?; let response = self.response_to_http_response(response).await?;
Ok(<Request::Response as Outgoing>::Incoming::try_from( Ok(Request::IncomingResponse::try_from(response)?)
response,
)?)
} }
} }

View File

@ -381,22 +381,22 @@ impl Into<register::Request> for RegistrationBuilder {
/// let mut builder = RoomListFilterBuilder::new(); /// let mut builder = RoomListFilterBuilder::new();
/// builder /// builder
/// .filter(Filter { generic_search_term, }) /// .filter(Filter { generic_search_term, })
/// .since(last_sync_token) /// .since(&last_sync_token)
/// .room_network(RoomNetwork::Matrix); /// .room_network(RoomNetwork::Matrix);
/// ///
/// client.public_rooms_filtered(builder).await.is_err(); /// client.public_rooms_filtered(builder).await.is_err();
/// # }) /// # })
/// ``` /// ```
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
pub struct RoomListFilterBuilder { pub struct RoomListFilterBuilder<'a> {
server: Option<String>, server: Option<&'a str>,
limit: Option<u32>, limit: Option<u32>,
since: Option<String>, since: Option<&'a str>,
filter: Option<Filter>, filter: Option<Filter>,
room_network: Option<RoomNetwork>, room_network: Option<RoomNetwork>,
} }
impl RoomListFilterBuilder { impl<'a> RoomListFilterBuilder<'a> {
/// Create a `RoomListFilterBuilder` builder to make a `get_message_events::Request`. /// Create a `RoomListFilterBuilder` builder to make a `get_message_events::Request`.
pub fn new() -> Self { pub fn new() -> Self {
Self::default() Self::default()
@ -405,8 +405,8 @@ impl RoomListFilterBuilder {
/// The server to fetch the public room lists from. /// The server to fetch the public room lists from.
/// ///
/// `None` means the server this request is sent to. /// `None` means the server this request is sent to.
pub fn server<S: Into<String>>(&mut self, server: S) -> &mut Self { pub fn server(&mut self, server: &'a str) -> &mut Self {
self.server = Some(server.into()); self.server = Some(server);
self self
} }
@ -417,8 +417,8 @@ impl RoomListFilterBuilder {
} }
/// Pagination token from a previous request. /// Pagination token from a previous request.
pub fn since<S: Into<String>>(&mut self, since: S) -> &mut Self { pub fn since(&mut self, since: &'a str) -> &mut Self {
self.since = Some(since.into()); self.since = Some(since);
self self
} }
@ -437,8 +437,8 @@ impl RoomListFilterBuilder {
} }
} }
impl Into<get_public_rooms_filtered::Request> for RoomListFilterBuilder { impl<'a> Into<get_public_rooms_filtered::Request<'a>> for RoomListFilterBuilder<'a> {
fn into(self) -> get_public_rooms_filtered::Request { fn into(self) -> get_public_rooms_filtered::Request<'a> {
get_public_rooms_filtered::Request { get_public_rooms_filtered::Request {
room_network: self.room_network.unwrap_or_default(), room_network: self.room_network.unwrap_or_default(),
limit: self.limit.map(UInt::from), limit: self.limit.map(UInt::from),

View File

@ -17,7 +17,9 @@ use std::sync::Arc;
use url::Url; use url::Url;
use matrix_sdk_base::{Device, Sas as BaseSas, Session}; use matrix_sdk_base::{Device, Sas as BaseSas, Session};
use matrix_sdk_common::locks::RwLock; use matrix_sdk_common::{
api::r0::to_device::send_event_to_device::Request as ToDeviceRequest, locks::RwLock,
};
use crate::{error::Result, http_client::HttpClient}; use crate::{error::Result, http_client::HttpClient};
@ -34,7 +36,13 @@ pub struct Sas {
impl Sas { impl Sas {
/// Accept the interactive verification flow. /// Accept the interactive verification flow.
pub async fn accept(&self) -> Result<()> { pub async fn accept(&self) -> Result<()> {
if let Some(request) = self.inner.accept() { if let Some(req) = self.inner.accept() {
let request = ToDeviceRequest {
event_type: req.event_type,
txn_id: &req.txn_id,
messages: req.messages,
};
self.http_client.send(request, self.session.clone()).await?; self.http_client.send(request, self.session.clone()).await?;
} }
Ok(()) Ok(())
@ -42,7 +50,13 @@ impl Sas {
/// Confirm that the short auth strings match on both sides. /// Confirm that the short auth strings match on both sides.
pub async fn confirm(&self) -> Result<()> { pub async fn confirm(&self) -> Result<()> {
if let Some(request) = self.inner.confirm().await? { if let Some(req) = self.inner.confirm().await? {
let request = ToDeviceRequest {
event_type: req.event_type,
txn_id: &req.txn_id,
messages: req.messages,
};
self.http_client.send(request, self.session.clone()).await?; self.http_client.send(request, self.session.clone()).await?;
} }
@ -51,7 +65,13 @@ impl Sas {
/// Cancel the interactive verification flow. /// Cancel the interactive verification flow.
pub async fn cancel(&self) -> Result<()> { pub async fn cancel(&self) -> Result<()> {
if let Some(request) = self.inner.cancel() { if let Some(req) = self.inner.cancel() {
let request = ToDeviceRequest {
event_type: req.event_type,
txn_id: &req.txn_id,
messages: req.messages,
};
self.http_client.send(request, self.session.clone()).await?; self.http_client.send(request, self.session.clone()).await?;
} }
Ok(()) Ok(())

View File

@ -42,13 +42,13 @@ use matrix_sdk_common::{
use matrix_sdk_common::{ use matrix_sdk_common::{
api::r0::keys::{ api::r0::keys::{
claim_keys::Response as KeysClaimResponse, get_keys::Response as KeysQueryResponse, claim_keys::Response as KeysClaimResponse, get_keys::Response as KeysQueryResponse,
upload_keys::Response as KeysUploadResponse, DeviceKeys, KeyAlgorithm, upload_keys::Response as KeysUploadResponse, DeviceKeys,
}, },
api::r0::to_device::send_event_to_device::Request as ToDeviceRequest, api::r0::to_device::send_event_to_device::IncomingRequest as OwnedToDeviceRequest,
events::room::{ events::room::{
encrypted::EncryptedEventContent, message::MessageEventContent as MsgEventContent, encrypted::EncryptedEventContent, message::MessageEventContent as MsgEventContent,
}, },
identifiers::DeviceId, identifiers::{DeviceId, DeviceKeyAlgorithm},
}; };
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
use matrix_sdk_crypto::{CryptoStore, OlmError, OlmMachine, OneTimeKeys, Sas}; use matrix_sdk_crypto::{CryptoStore, OlmError, OlmMachine, OneTimeKeys, Sas};
@ -1274,7 +1274,7 @@ impl BaseClient {
pub async fn get_missing_sessions( pub async fn get_missing_sessions(
&self, &self,
users: impl Iterator<Item = &UserId>, users: impl Iterator<Item = &UserId>,
) -> Result<BTreeMap<UserId, BTreeMap<Box<DeviceId>, KeyAlgorithm>>> { ) -> Result<BTreeMap<UserId, BTreeMap<Box<DeviceId>, DeviceKeyAlgorithm>>> {
let mut olm = self.olm.lock().await; let mut olm = self.olm.lock().await;
match &mut *olm { match &mut *olm {
@ -1286,7 +1286,7 @@ impl BaseClient {
/// Get a to-device request that will share a group session for a room. /// Get a to-device request that will share a group session for a room.
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))] #[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
pub async fn share_group_session(&self, room_id: &RoomId) -> Result<Vec<ToDeviceRequest>> { pub async fn share_group_session(&self, room_id: &RoomId) -> Result<Vec<OwnedToDeviceRequest>> {
let room = self.get_joined_room(room_id).await.expect("No room found"); let room = self.get_joined_room(room_id).await.expect("No room found");
let mut olm = self.olm.lock().await; let mut olm = self.olm.lock().await;
@ -1828,7 +1828,7 @@ impl BaseClient {
/// Get the to-device requests that need to be sent out. /// Get the to-device requests that need to be sent out.
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))] #[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
pub async fn outgoing_to_device_requests(&self) -> Vec<ToDeviceRequest> { pub async fn outgoing_to_device_requests(&self) -> Vec<OwnedToDeviceRequest> {
self.olm self.olm
.lock() .lock()
.await .await

View File

@ -17,7 +17,7 @@ js_int = "0.1.8"
[dependencies.ruma] [dependencies.ruma]
version = "0.0.1" version = "0.0.1"
git = "https://github.com/ruma/ruma" git = "https://github.com/ruma/ruma"
rev = "a589e92144e1af635ec3fd224265193e45dcdc95" rev = "9cf552f36186eedff44ebe0c6a32d598315f5860"
features = ["client-api"] features = ["client-api"]
[target.'cfg(not(target_arch = "wasm32"))'.dependencies] [target.'cfg(not(target_arch = "wasm32"))'.dependencies]

View File

@ -6,7 +6,7 @@ pub use ruma::{
error::{FromHttpRequestError, FromHttpResponseError, IntoHttpError, ServerError}, error::{FromHttpRequestError, FromHttpResponseError, IntoHttpError, ServerError},
Endpoint, EndpointError, Outgoing, Endpoint, EndpointError, Outgoing,
}, },
events, identifiers, presence, push, Raw, events, identifiers, presence, push, thirdparty, Raw,
}; };
pub use uuid; pub use uuid;

View File

@ -23,9 +23,9 @@ use std::{
use atomic::Atomic; use atomic::Atomic;
use matrix_sdk_common::{ use matrix_sdk_common::{
api::r0::keys::{AlgorithmAndDeviceId, DeviceKeys, KeyAlgorithm, SignedKey}, api::r0::keys::{DeviceKeys, SignedKey},
events::Algorithm, events::Algorithm,
identifiers::{DeviceId, UserId}, identifiers::{DeviceId, DeviceKeyAlgorithm, DeviceKeyId, UserId},
}; };
use serde_json::{json, Value}; use serde_json::{json, Value};
@ -40,8 +40,8 @@ pub struct Device {
user_id: Arc<UserId>, user_id: Arc<UserId>,
device_id: Arc<Box<DeviceId>>, device_id: Arc<Box<DeviceId>>,
algorithms: Arc<Vec<Algorithm>>, algorithms: Arc<Vec<Algorithm>>,
keys: Arc<BTreeMap<AlgorithmAndDeviceId, String>>, keys: Arc<BTreeMap<DeviceKeyId, String>>,
signatures: Arc<BTreeMap<UserId, BTreeMap<AlgorithmAndDeviceId, String>>>, signatures: Arc<BTreeMap<UserId, BTreeMap<DeviceKeyId, String>>>,
display_name: Arc<Option<String>>, display_name: Arc<Option<String>>,
deleted: Arc<AtomicBool>, deleted: Arc<AtomicBool>,
trust_state: Arc<Atomic<TrustState>>, trust_state: Arc<Atomic<TrustState>>,
@ -80,8 +80,8 @@ impl Device {
display_name: Option<String>, display_name: Option<String>,
trust_state: TrustState, trust_state: TrustState,
algorithms: Vec<Algorithm>, algorithms: Vec<Algorithm>,
keys: BTreeMap<AlgorithmAndDeviceId, String>, keys: BTreeMap<DeviceKeyId, String>,
signatures: BTreeMap<UserId, BTreeMap<AlgorithmAndDeviceId, String>>, signatures: BTreeMap<UserId, BTreeMap<DeviceKeyId, String>>,
) -> Self { ) -> Self {
Device { Device {
user_id: Arc::new(user_id), user_id: Arc::new(user_id),
@ -111,20 +111,18 @@ impl Device {
} }
/// Get the key of the given key algorithm belonging to this device. /// Get the key of the given key algorithm belonging to this device.
pub fn get_key(&self, algorithm: KeyAlgorithm) -> Option<&String> { pub fn get_key(&self, algorithm: DeviceKeyAlgorithm) -> Option<&String> {
self.keys.get(&AlgorithmAndDeviceId( self.keys
algorithm, .get(&DeviceKeyId::from_parts(algorithm, &self.device_id))
self.device_id.as_ref().clone(),
))
} }
/// Get a map containing all the device keys. /// Get a map containing all the device keys.
pub fn keys(&self) -> &BTreeMap<AlgorithmAndDeviceId, String> { pub fn keys(&self) -> &BTreeMap<DeviceKeyId, String> {
&self.keys &self.keys
} }
/// Get a map containing all the device signatures. /// Get a map containing all the device signatures.
pub fn signatures(&self) -> &BTreeMap<UserId, BTreeMap<AlgorithmAndDeviceId, String>> { pub fn signatures(&self) -> &BTreeMap<UserId, BTreeMap<DeviceKeyId, String>> {
&self.signatures &self.signatures
} }
@ -173,7 +171,7 @@ impl Device {
fn is_signed_by_device(&self, json: &mut Value) -> Result<(), SignatureError> { fn is_signed_by_device(&self, json: &mut Value) -> Result<(), SignatureError> {
let signing_key = self let signing_key = self
.get_key(KeyAlgorithm::Ed25519) .get_key(DeviceKeyAlgorithm::Ed25519)
.ok_or(SignatureError::MissingSigningKey)?; .ok_or(SignatureError::MissingSigningKey)?;
verify_json(&self.user_id, &self.device_id.as_str(), signing_key, json) verify_json(&self.user_id, &self.device_id.as_str(), signing_key, json)
@ -249,8 +247,8 @@ pub(crate) mod test {
use crate::device::{Device, TrustState}; use crate::device::{Device, TrustState};
use matrix_sdk_common::{ use matrix_sdk_common::{
api::r0::keys::{DeviceKeys, KeyAlgorithm}, api::r0::keys::DeviceKeys,
identifiers::user_id, identifiers::{user_id, DeviceKeyAlgorithm},
}; };
fn device_keys() -> DeviceKeys { fn device_keys() -> DeviceKeys {
@ -299,11 +297,11 @@ pub(crate) mod test {
device.display_name().as_ref().unwrap() device.display_name().as_ref().unwrap()
); );
assert_eq!( assert_eq!(
device.get_key(KeyAlgorithm::Curve25519).unwrap(), device.get_key(DeviceKeyAlgorithm::Curve25519).unwrap(),
"xfgbLIC5WAl1OIkpOzoxpCe8FsRDT6nch7NQsOb15nc" "xfgbLIC5WAl1OIkpOzoxpCe8FsRDT6nch7NQsOb15nc"
); );
assert_eq!( assert_eq!(
device.get_key(KeyAlgorithm::Ed25519).unwrap(), device.get_key(DeviceKeyAlgorithm::Ed25519).unwrap(),
"2/5LWJMow5zhJqakV88SIc7q/1pa8fmkfgAzx72w9G4" "2/5LWJMow5zhJqakV88SIc7q/1pa8fmkfgAzx72w9G4"
); );
} }

View File

@ -45,10 +45,7 @@ pub use store::{CryptoStore, CryptoStoreError};
pub use verification::Sas; pub use verification::Sas;
use error::SignatureError; use error::SignatureError;
use matrix_sdk_common::{ use matrix_sdk_common::identifiers::{DeviceKeyAlgorithm, DeviceKeyId, UserId};
api::r0::keys::{AlgorithmAndDeviceId, KeyAlgorithm},
identifiers::UserId,
};
use olm_rs::utility::OlmUtility; use olm_rs::utility::OlmUtility;
use serde_json::Value; use serde_json::Value;
@ -86,7 +83,7 @@ pub(crate) fn verify_json(
json_object.insert("unsigned".to_string(), u); json_object.insert("unsigned".to_string(), u);
} }
let key_id = AlgorithmAndDeviceId(KeyAlgorithm::Ed25519, key_id.into()); let key_id = DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, key_id.into());
let signatures = signatures.ok_or(SignatureError::NoSignatureFound)?; let signatures = signatures.ok_or(SignatureError::NoSignatureFound)?;
let signature_object = signatures let signature_object = signatures

View File

@ -23,10 +23,11 @@ use std::{
}; };
use api::r0::{ use api::r0::{
keys, keys::{claim_keys, get_keys, upload_keys, DeviceKeys, OneTimeKey},
keys::{AlgorithmAndDeviceId, DeviceKeys, KeyAlgorithm, OneTimeKey},
sync::sync_events::Response as SyncResponse, sync::sync_events::Response as SyncResponse,
to_device::{send_event_to_device::Request as ToDeviceRequest, DeviceIdOrAllDevices}, to_device::{
send_event_to_device::IncomingRequest as OwnedToDeviceRequest, DeviceIdOrAllDevices,
},
}; };
use matrix_sdk_common::{ use matrix_sdk_common::{
api, api,
@ -37,7 +38,7 @@ use matrix_sdk_common::{
room_key_request::RoomKeyRequestEventContent, room_key_request::RoomKeyRequestEventContent,
Algorithm, AnySyncRoomEvent, AnyToDeviceEvent, EventType, SyncMessageEvent, ToDeviceEvent, Algorithm, AnySyncRoomEvent, AnyToDeviceEvent, EventType, SyncMessageEvent, ToDeviceEvent,
}, },
identifiers::{DeviceId, RoomId, UserId}, identifiers::{DeviceId, DeviceKeyAlgorithm, DeviceKeyId, RoomId, UserId},
locks::RwLock, locks::RwLock,
uuid::Uuid, uuid::Uuid,
Raw, Raw,
@ -62,7 +63,7 @@ use super::{
/// A map from the algorithm and device id to a one-time key. /// A map from the algorithm and device id to a one-time key.
/// ///
/// These keys need to be periodically uploaded to the server. /// These keys need to be periodically uploaded to the server.
pub type OneTimeKeys = BTreeMap<AlgorithmAndDeviceId, OneTimeKey>; pub type OneTimeKeys = BTreeMap<DeviceKeyId, OneTimeKey>;
/// State machine implementation of the Olm/Megolm encryption protocol used for /// State machine implementation of the Olm/Megolm encryption protocol used for
/// Matrix end to end encryption. /// Matrix end to end encryption.
@ -226,7 +227,7 @@ impl OlmMachine {
#[instrument] #[instrument]
pub async fn receive_keys_upload_response( pub async fn receive_keys_upload_response(
&mut self, &mut self,
response: &keys::upload_keys::Response, response: &upload_keys::Response,
) -> OlmResult<()> { ) -> OlmResult<()> {
if !self.account.shared() { if !self.account.shared() {
debug!("Marking account as shared"); debug!("Marking account as shared");
@ -235,7 +236,7 @@ impl OlmMachine {
let one_time_key_count = response let one_time_key_count = response
.one_time_key_counts .one_time_key_counts
.get(&keys::KeyAlgorithm::SignedCurve25519); .get(&DeviceKeyAlgorithm::SignedCurve25519);
let count: u64 = one_time_key_count.map_or(0, |c| (*c).into()); let count: u64 = one_time_key_count.map_or(0, |c| (*c).into());
debug!( debug!(
@ -277,14 +278,14 @@ impl OlmMachine {
pub async fn get_missing_sessions( pub async fn get_missing_sessions(
&mut self, &mut self,
users: impl Iterator<Item = &UserId>, users: impl Iterator<Item = &UserId>,
) -> OlmResult<BTreeMap<UserId, BTreeMap<Box<DeviceId>, KeyAlgorithm>>> { ) -> OlmResult<BTreeMap<UserId, BTreeMap<Box<DeviceId>, DeviceKeyAlgorithm>>> {
let mut missing = BTreeMap::new(); let mut missing = BTreeMap::new();
for user_id in users { for user_id in users {
let user_devices = self.store.read().await.get_user_devices(user_id).await?; let user_devices = self.store.read().await.get_user_devices(user_id).await?;
for device in user_devices.devices() { for device in user_devices.devices() {
let sender_key = if let Some(k) = device.get_key(KeyAlgorithm::Curve25519) { let sender_key = if let Some(k) = device.get_key(DeviceKeyAlgorithm::Curve25519) {
k k
} else { } else {
continue; continue;
@ -304,8 +305,10 @@ impl OlmMachine {
} }
let user_map = missing.get_mut(user_id).unwrap(); let user_map = missing.get_mut(user_id).unwrap();
let _ = let _ = user_map.insert(
user_map.insert(device.device_id().into(), KeyAlgorithm::SignedCurve25519); device.device_id().into(),
DeviceKeyAlgorithm::SignedCurve25519,
);
} }
} }
} }
@ -321,7 +324,7 @@ impl OlmMachine {
/// * `response` - The response containing the claimed one-time keys. /// * `response` - The response containing the claimed one-time keys.
pub async fn receive_keys_claim_response( pub async fn receive_keys_claim_response(
&mut self, &mut self,
response: &keys::claim_keys::Response, response: &claim_keys::Response,
) -> OlmResult<()> { ) -> OlmResult<()> {
// TODO log the failures here // TODO log the failures here
@ -472,7 +475,7 @@ impl OlmMachine {
/// performed. /// performed.
pub async fn receive_keys_query_response( pub async fn receive_keys_query_response(
&mut self, &mut self,
response: &keys::get_keys::Response, response: &get_keys::Response,
) -> OlmResult<Vec<Device>> { ) -> OlmResult<Vec<Device>> {
let changed_devices = self let changed_devices = self
.handle_devices_from_key_query(&response.device_keys) .handle_devices_from_key_query(&response.device_keys)
@ -654,13 +657,13 @@ impl OlmMachine {
.ok_or_else(|| EventError::MissingField("recipient".to_string()))?; .ok_or_else(|| EventError::MissingField("recipient".to_string()))?;
let recipient: UserId = serde_json::from_value(recipient)?; let recipient: UserId = serde_json::from_value(recipient)?;
let recipient_keys: BTreeMap<KeyAlgorithm, String> = serde_json::from_value( let recipient_keys: BTreeMap<DeviceKeyAlgorithm, String> = serde_json::from_value(
decrypted_json decrypted_json
.get("recipient_keys") .get("recipient_keys")
.cloned() .cloned()
.ok_or_else(|| EventError::MissingField("recipient_keys".to_string()))?, .ok_or_else(|| EventError::MissingField("recipient_keys".to_string()))?,
)?; )?;
let keys: BTreeMap<KeyAlgorithm, String> = serde_json::from_value( let keys: BTreeMap<DeviceKeyAlgorithm, String> = serde_json::from_value(
decrypted_json decrypted_json
.get("keys") .get("keys")
.cloned() .cloned()
@ -673,14 +676,14 @@ impl OlmMachine {
if self.account.identity_keys().ed25519() if self.account.identity_keys().ed25519()
!= recipient_keys != recipient_keys
.get(&KeyAlgorithm::Ed25519) .get(&DeviceKeyAlgorithm::Ed25519)
.ok_or(EventError::MissingSigningKey)? .ok_or(EventError::MissingSigningKey)?
{ {
return Err(EventError::MissmatchedKeys.into()); return Err(EventError::MissmatchedKeys.into());
} }
let signing_key = keys let signing_key = keys
.get(&KeyAlgorithm::Ed25519) .get(&DeviceKeyAlgorithm::Ed25519)
.ok_or(EventError::MissingSigningKey)?; .ok_or(EventError::MissingSigningKey)?;
Ok(( Ok((
@ -872,7 +875,7 @@ impl OlmMachine {
event_type: EventType, event_type: EventType,
content: Value, content: Value,
) -> OlmResult<EncryptedEventContent> { ) -> OlmResult<EncryptedEventContent> {
let sender_key = if let Some(k) = recipient_device.get_key(KeyAlgorithm::Curve25519) { let sender_key = if let Some(k) = recipient_device.get_key(DeviceKeyAlgorithm::Curve25519) {
k k
} else { } else {
warn!( warn!(
@ -943,7 +946,7 @@ impl OlmMachine {
&mut self, &mut self,
room_id: &RoomId, room_id: &RoomId,
users: I, users: I,
) -> OlmResult<Vec<ToDeviceRequest>> ) -> OlmResult<Vec<OwnedToDeviceRequest>>
where where
I: IntoIterator<Item = &'a UserId>, I: IntoIterator<Item = &'a UserId>,
{ {
@ -1008,7 +1011,7 @@ impl OlmMachine {
); );
} }
requests.push(ToDeviceRequest { requests.push(OwnedToDeviceRequest {
event_type: EventType::RoomEncrypted, event_type: EventType::RoomEncrypted,
txn_id: Uuid::new_v4().to_string(), txn_id: Uuid::new_v4().to_string(),
messages, messages,
@ -1076,7 +1079,7 @@ impl OlmMachine {
} }
/// Get the to-device requests that need to be sent out. /// Get the to-device requests that need to be sent out.
pub fn outgoing_to_device_requests(&self) -> Vec<ToDeviceRequest> { pub fn outgoing_to_device_requests(&self) -> Vec<OwnedToDeviceRequest> {
self.verification_machine.outgoing_to_device_requests() self.verification_machine.outgoing_to_device_requests()
} }
@ -1106,7 +1109,7 @@ impl OlmMachine {
let one_time_key_count = response let one_time_key_count = response
.device_one_time_keys_count .device_one_time_keys_count
.get(&keys::KeyAlgorithm::SignedCurve25519); .get(&DeviceKeyAlgorithm::SignedCurve25519);
let count: u64 = one_time_key_count.map_or(0, |c| (*c).into()); let count: u64 = one_time_key_count.map_or(0, |c| (*c).into());
self.update_key_count(count); self.update_key_count(count);
@ -1285,7 +1288,7 @@ mod test {
}; };
use matrix_sdk_common::{ use matrix_sdk_common::{
api::r0::{keys, to_device::send_event_to_device::Request as ToDeviceRequest}, api::r0::{keys, to_device::send_event_to_device::IncomingRequest as OwnedToDeviceRequest},
events::{ events::{
room::{ room::{
encrypted::EncryptedEventContent, encrypted::EncryptedEventContent,
@ -1294,7 +1297,7 @@ mod test {
AnySyncMessageEvent, AnySyncRoomEvent, AnyToDeviceEvent, EventType, SyncMessageEvent, AnySyncMessageEvent, AnySyncRoomEvent, AnyToDeviceEvent, EventType, SyncMessageEvent,
ToDeviceEvent, Unsigned, ToDeviceEvent, Unsigned,
}, },
identifiers::{event_id, room_id, user_id, DeviceId, UserId}, identifiers::{event_id, room_id, user_id, DeviceId, DeviceKeyAlgorithm, UserId},
Raw, Raw,
}; };
use matrix_sdk_test::test_json; use matrix_sdk_test::test_json;
@ -1328,7 +1331,7 @@ mod test {
keys::get_keys::Response::try_from(data).expect("Can't parse the keys upload response") keys::get_keys::Response::try_from(data).expect("Can't parse the keys upload response")
} }
fn to_device_requests_to_content(requests: Vec<ToDeviceRequest>) -> EncryptedEventContent { fn to_device_requests_to_content(requests: Vec<OwnedToDeviceRequest>) -> EncryptedEventContent {
let to_device_request = &requests[0]; let to_device_request = &requests[0];
let content: Raw<EncryptedEventContent> = serde_json::from_str( let content: Raw<EncryptedEventContent> = serde_json::from_str(
@ -1462,7 +1465,7 @@ mod test {
response response
.one_time_key_counts .one_time_key_counts
.remove(&keys::KeyAlgorithm::SignedCurve25519) .remove(&DeviceKeyAlgorithm::SignedCurve25519)
.unwrap(); .unwrap();
assert!(machine.should_upload_keys().await); assert!(machine.should_upload_keys().await);
@ -1474,7 +1477,7 @@ mod test {
response response
.one_time_key_counts .one_time_key_counts
.insert(keys::KeyAlgorithm::SignedCurve25519, uint!(10)); .insert(DeviceKeyAlgorithm::SignedCurve25519, uint!(10));
machine machine
.receive_keys_upload_response(&response) .receive_keys_upload_response(&response)
.await .await
@ -1483,7 +1486,7 @@ mod test {
response response
.one_time_key_counts .one_time_key_counts
.insert(keys::KeyAlgorithm::SignedCurve25519, uint!(50)); .insert(DeviceKeyAlgorithm::SignedCurve25519, uint!(50));
machine machine
.receive_keys_upload_response(&response) .receive_keys_upload_response(&response)
.await .await
@ -1508,7 +1511,7 @@ mod test {
response response
.one_time_key_counts .one_time_key_counts
.insert(keys::KeyAlgorithm::SignedCurve25519, uint!(50)); .insert(DeviceKeyAlgorithm::SignedCurve25519, uint!(50));
machine machine
.receive_keys_upload_response(&response) .receive_keys_upload_response(&response)
.await .await
@ -1615,7 +1618,7 @@ mod test {
let mut response = keys_upload_response(); let mut response = keys_upload_response();
response.one_time_key_counts.insert( response.one_time_key_counts.insert(
keys::KeyAlgorithm::SignedCurve25519, DeviceKeyAlgorithm::SignedCurve25519,
(one_time_keys.unwrap().len() as u64).try_into().unwrap(), (one_time_keys.unwrap().len() as u64).try_into().unwrap(),
); );

View File

@ -23,9 +23,9 @@ use std::{
}; };
use matrix_sdk_common::{ use matrix_sdk_common::{
api::r0::keys::{AlgorithmAndDeviceId, DeviceKeys, KeyAlgorithm, OneTimeKey, SignedKey}, api::r0::keys::{DeviceKeys, OneTimeKey, SignedKey},
events::Algorithm, events::Algorithm,
identifiers::{DeviceId, RoomId, UserId}, identifiers::{DeviceId, DeviceKeyAlgorithm, DeviceKeyId, RoomId, UserId},
instant::Instant, instant::Instant,
locks::Mutex, locks::Mutex,
}; };
@ -204,7 +204,7 @@ impl Account {
) -> Result< ) -> Result<
( (
Option<DeviceKeys>, Option<DeviceKeys>,
Option<BTreeMap<AlgorithmAndDeviceId, OneTimeKey>>, Option<BTreeMap<DeviceKeyId, OneTimeKey>>,
), ),
(), (),
> { > {
@ -286,11 +286,11 @@ impl Account {
let mut keys = BTreeMap::new(); let mut keys = BTreeMap::new();
keys.insert( keys.insert(
AlgorithmAndDeviceId(KeyAlgorithm::Curve25519, (*self.device_id).clone()), DeviceKeyId::from_parts(DeviceKeyAlgorithm::Curve25519, &self.device_id),
identity_keys.curve25519().to_owned(), identity_keys.curve25519().to_owned(),
); );
keys.insert( keys.insert(
AlgorithmAndDeviceId(KeyAlgorithm::Ed25519, (*self.device_id).clone()), DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, &self.device_id),
identity_keys.ed25519().to_owned(), identity_keys.ed25519().to_owned(),
); );
@ -305,7 +305,7 @@ impl Account {
let mut signature = BTreeMap::new(); let mut signature = BTreeMap::new();
signature.insert( signature.insert(
AlgorithmAndDeviceId(KeyAlgorithm::Ed25519, (*self.device_id).clone()), DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, &self.device_id),
self.sign_json(&device_keys).await, self.sign_json(&device_keys).await,
); );
signatures.insert((*self.user_id).clone(), signature); signatures.insert((*self.user_id).clone(), signature);
@ -345,7 +345,7 @@ impl Account {
/// If no one-time keys need to be uploaded returns an empty error. /// If no one-time keys need to be uploaded returns an empty error.
pub(crate) async fn signed_one_time_keys( pub(crate) async fn signed_one_time_keys(
&self, &self,
) -> Result<BTreeMap<AlgorithmAndDeviceId, OneTimeKey>, ()> { ) -> Result<BTreeMap<DeviceKeyId, OneTimeKey>, ()> {
let _ = self.generate_one_time_keys().await?; let _ = self.generate_one_time_keys().await?;
let one_time_keys = self.one_time_keys().await; let one_time_keys = self.one_time_keys().await;
@ -361,7 +361,7 @@ impl Account {
let mut signature_map = BTreeMap::new(); let mut signature_map = BTreeMap::new();
signature_map.insert( signature_map.insert(
AlgorithmAndDeviceId(KeyAlgorithm::Ed25519, (*self.device_id).clone()), DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, &self.device_id),
signature, signature,
); );
@ -374,7 +374,10 @@ impl Account {
}; };
one_time_key_map.insert( one_time_key_map.insert(
AlgorithmAndDeviceId(KeyAlgorithm::SignedCurve25519, key_id.as_str().into()), DeviceKeyId::from_parts(
DeviceKeyAlgorithm::SignedCurve25519,
key_id.as_str().into(),
),
OneTimeKey::SignedKey(signed_key), OneTimeKey::SignedKey(signed_key),
); );
} }
@ -432,7 +435,7 @@ impl Account {
pub(crate) async fn create_outbound_session( pub(crate) async fn create_outbound_session(
&self, &self,
device: Device, device: Device,
key_map: &BTreeMap<AlgorithmAndDeviceId, OneTimeKey>, key_map: &BTreeMap<DeviceKeyId, OneTimeKey>,
) -> Result<Session, SessionCreationError> { ) -> Result<Session, SessionCreationError> {
let one_time_key = key_map.values().next().ok_or_else(|| { let one_time_key = key_map.values().next().ok_or_else(|| {
SessionCreationError::OneTimeKeyMissing( SessionCreationError::OneTimeKeyMissing(
@ -459,12 +462,14 @@ impl Account {
) )
})?; })?;
let curve_key = device.get_key(KeyAlgorithm::Curve25519).ok_or_else(|| { let curve_key = device
SessionCreationError::DeviceMissingCurveKey( .get_key(DeviceKeyAlgorithm::Curve25519)
device.user_id().to_owned(), .ok_or_else(|| {
device.device_id().into(), SessionCreationError::DeviceMissingCurveKey(
) device.user_id().to_owned(),
})?; device.device_id().into(),
)
})?;
self.create_outbound_session_helper(curve_key, &one_time_key) self.create_outbound_session_helper(curve_key, &one_time_key)
.await .await

View File

@ -15,12 +15,11 @@
use std::{collections::BTreeMap, fmt, sync::Arc}; use std::{collections::BTreeMap, fmt, sync::Arc};
use matrix_sdk_common::{ use matrix_sdk_common::{
api::r0::keys::KeyAlgorithm,
events::{ events::{
room::encrypted::{CiphertextInfo, EncryptedEventContent, OlmV1Curve25519AesSha2Content}, room::encrypted::{CiphertextInfo, EncryptedEventContent, OlmV1Curve25519AesSha2Content},
EventType, EventType,
}, },
identifiers::{DeviceId, UserId}, identifiers::{DeviceId, DeviceKeyAlgorithm, UserId},
instant::Instant, instant::Instant,
locks::Mutex, locks::Mutex,
}; };
@ -109,7 +108,7 @@ impl Session {
content: Value, content: Value,
) -> OlmResult<EncryptedEventContent> { ) -> OlmResult<EncryptedEventContent> {
let recipient_signing_key = recipient_device let recipient_signing_key = recipient_device
.get_key(KeyAlgorithm::Ed25519) .get_key(DeviceKeyAlgorithm::Ed25519)
.ok_or(EventError::MissingSigningKey)?; .ok_or(EventError::MissingSigningKey)?;
let payload = json!({ let payload = json!({

View File

@ -22,9 +22,8 @@ use std::{
use async_trait::async_trait; use async_trait::async_trait;
use matrix_sdk_common::{ use matrix_sdk_common::{
api::r0::keys::{AlgorithmAndDeviceId, KeyAlgorithm},
events::Algorithm, events::Algorithm,
identifiers::{DeviceId, RoomId, UserId}, identifiers::{DeviceId, DeviceKeyAlgorithm, DeviceKeyId, RoomId, UserId},
instant::{Duration, Instant}, instant::{Duration, Instant},
locks::Mutex, locks::Mutex,
}; };
@ -497,14 +496,14 @@ impl SqliteStore {
.fetch_all(&mut *connection) .fetch_all(&mut *connection)
.await?; .await?;
let keys: BTreeMap<AlgorithmAndDeviceId, String> = key_rows let keys: BTreeMap<DeviceKeyId, String> = key_rows
.into_iter() .into_iter()
.filter_map(|row| { .filter_map(|row| {
let algorithm = KeyAlgorithm::try_from(&*row.0).ok()?; let algorithm = row.0.parse::<DeviceKeyAlgorithm>().ok()?;
let key = row.1; let key = row.1;
Some(( Some((
AlgorithmAndDeviceId(algorithm, device_id.as_str().into()), DeviceKeyId::from_parts(algorithm, device_id.as_str().into()),
key, key,
)) ))
}) })
@ -518,8 +517,7 @@ impl SqliteStore {
.fetch_all(&mut *connection) .fetch_all(&mut *connection)
.await?; .await?;
let mut signatures: BTreeMap<UserId, BTreeMap<AlgorithmAndDeviceId, String>> = let mut signatures: BTreeMap<UserId, BTreeMap<DeviceKeyId, String>> = BTreeMap::new();
BTreeMap::new();
for row in signature_rows { for row in signature_rows {
let user_id = if let Ok(u) = UserId::try_from(&*row.0) { let user_id = if let Ok(u) = UserId::try_from(&*row.0) {
@ -528,7 +526,7 @@ impl SqliteStore {
continue; continue;
}; };
let key_algorithm = if let Ok(k) = KeyAlgorithm::try_from(&*row.1) { let key_algorithm = if let Ok(k) = row.1.parse::<DeviceKeyAlgorithm>() {
k k
} else { } else {
continue; continue;
@ -542,7 +540,7 @@ impl SqliteStore {
let user_map = signatures.get_mut(&user_id).unwrap(); let user_map = signatures.get_mut(&user_id).unwrap();
user_map.insert( user_map.insert(
AlgorithmAndDeviceId(key_algorithm, device_id.as_str().into()), DeviceKeyId::from_parts(key_algorithm, device_id.as_str().into()),
signature.to_owned(), signature.to_owned(),
); );
} }
@ -610,7 +608,7 @@ impl SqliteStore {
.await?; .await?;
} }
for (key_algorithm, key) in device.keys() { for (key_id, key) in device.keys() {
query( query(
"INSERT OR IGNORE INTO device_keys ( "INSERT OR IGNORE INTO device_keys (
device_id, algorithm, key device_id, algorithm, key
@ -618,7 +616,7 @@ impl SqliteStore {
", ",
) )
.bind(device_row_id) .bind(device_row_id)
.bind(key_algorithm.0.to_string()) .bind(key_id.algorithm().to_string())
.bind(key) .bind(key)
.execute(&mut *connection) .execute(&mut *connection)
.await?; .await?;
@ -634,7 +632,7 @@ impl SqliteStore {
) )
.bind(device_row_id) .bind(device_row_id)
.bind(user_id.as_str()) .bind(user_id.as_str())
.bind(key_id.0.to_string()) .bind(key_id.algorithm().to_string())
.bind(signature) .bind(signature)
.execute(&mut *connection) .execute(&mut *connection)
.await?; .await?;

View File

@ -19,7 +19,7 @@ use dashmap::DashMap;
use tracing::{trace, warn}; use tracing::{trace, warn};
use matrix_sdk_common::{ use matrix_sdk_common::{
api::r0::to_device::send_event_to_device::Request as ToDeviceRequest, api::r0::to_device::send_event_to_device::IncomingRequest as OwnedToDeviceRequest,
events::{AnyToDeviceEvent, AnyToDeviceEventContent}, events::{AnyToDeviceEvent, AnyToDeviceEventContent},
identifiers::{DeviceId, UserId}, identifiers::{DeviceId, UserId},
locks::RwLock, locks::RwLock,
@ -33,7 +33,7 @@ pub struct VerificationMachine {
account: Account, account: Account,
store: Arc<RwLock<Box<dyn CryptoStore>>>, store: Arc<RwLock<Box<dyn CryptoStore>>>,
verifications: Arc<DashMap<String, Sas>>, verifications: Arc<DashMap<String, Sas>>,
outgoing_to_device_messages: Arc<DashMap<String, ToDeviceRequest>>, outgoing_to_device_messages: Arc<DashMap<String, OwnedToDeviceRequest>>,
} }
impl VerificationMachine { impl VerificationMachine {
@ -73,11 +73,15 @@ impl VerificationMachine {
self.outgoing_to_device_messages.remove(uuid); self.outgoing_to_device_messages.remove(uuid);
} }
pub fn outgoing_to_device_requests(&self) -> Vec<ToDeviceRequest> { pub fn outgoing_to_device_requests(&self) -> Vec<OwnedToDeviceRequest> {
#[allow(clippy::map_clone)] #[allow(clippy::map_clone)]
self.outgoing_to_device_messages self.outgoing_to_device_messages
.iter() .iter()
.map(|r| r.clone()) .map(|r| OwnedToDeviceRequest {
event_type: r.event_type.clone(),
txn_id: r.txn_id.clone(),
messages: r.messages.clone(),
})
.collect() .collect()
} }

View File

@ -25,7 +25,7 @@ pub(crate) mod test {
use serde_json::Value; use serde_json::Value;
use matrix_sdk_common::{ use matrix_sdk_common::{
api::r0::to_device::send_event_to_device::Request as ToDeviceRequest, api::r0::to_device::send_event_to_device::IncomingRequest as OwnedToDeviceRequest,
events::{AnyToDeviceEvent, AnyToDeviceEventContent, EventType, ToDeviceEvent}, events::{AnyToDeviceEvent, AnyToDeviceEventContent, EventType, ToDeviceEvent},
identifiers::UserId, identifiers::UserId,
}; };
@ -64,7 +64,9 @@ pub(crate) mod test {
} }
} }
pub(crate) fn get_content_from_request(request: &ToDeviceRequest) -> AnyToDeviceEventContent { pub(crate) fn get_content_from_request(
request: &OwnedToDeviceRequest,
) -> AnyToDeviceEventContent {
let json: Value = serde_json::from_str( let json: Value = serde_json::from_str(
request request
.messages .messages

View File

@ -5,15 +5,14 @@ use tracing::trace;
use olm_rs::sas::OlmSas; use olm_rs::sas::OlmSas;
use matrix_sdk_common::{ use matrix_sdk_common::{
api::r0::{ api::r0::to_device::{
keys::{AlgorithmAndDeviceId, KeyAlgorithm}, send_event_to_device::IncomingRequest as OwnedToDeviceRequest, DeviceIdOrAllDevices,
to_device::{send_event_to_device::Request as ToDeviceRequest, DeviceIdOrAllDevices},
}, },
events::{ events::{
key::verification::{cancel::CancelCode, mac::MacEventContent}, key::verification::{cancel::CancelCode, mac::MacEventContent},
AnyToDeviceEventContent, EventType, ToDeviceEvent, AnyToDeviceEventContent, EventType, ToDeviceEvent,
}, },
identifiers::{DeviceId, UserId}, identifiers::{DeviceId, DeviceKeyAlgorithm, DeviceKeyId, UserId},
uuid::Uuid, uuid::Uuid,
}; };
@ -161,20 +160,10 @@ pub fn receive_mac_event(
} }
for (key_id, key_mac) in &event.content.mac { for (key_id, key_mac) in &event.content.mac {
let split: Vec<&str> = key_id.splitn(2, ':').collect(); let device_key_id: DeviceKeyId = match key_id.as_str().try_into() {
Ok(id) => id,
if split.len() != 2 { Err(_) => continue,
continue;
}
let algorithm: KeyAlgorithm = if let Ok(a) = split[0].try_into() {
a
} else {
continue;
}; };
let id = split[1];
let device_key_id = AlgorithmAndDeviceId(algorithm, id.into());
if let Some(key) = ids.other_device.keys().get(&device_key_id) { if let Some(key) = ids.other_device.keys().get(&device_key_id) {
if key_mac if key_mac
@ -231,7 +220,7 @@ fn extra_mac_info_send(ids: &SasIds, flow_id: &str) -> String {
pub fn get_mac_content(sas: &OlmSas, ids: &SasIds, flow_id: &str) -> MacEventContent { pub fn get_mac_content(sas: &OlmSas, ids: &SasIds, flow_id: &str) -> MacEventContent {
let mut mac: BTreeMap<String, String> = BTreeMap::new(); let mut mac: BTreeMap<String, String> = BTreeMap::new();
let key_id = AlgorithmAndDeviceId(KeyAlgorithm::Ed25519, ids.account.device_id().into()); let key_id = DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, ids.account.device_id());
let key = ids.account.identity_keys().ed25519(); let key = ids.account.identity_keys().ed25519();
let info = extra_mac_info_send(ids, flow_id); let info = extra_mac_info_send(ids, flow_id);
@ -423,7 +412,7 @@ pub fn content_to_request(
recipient: &UserId, recipient: &UserId,
recipient_device: &DeviceId, recipient_device: &DeviceId,
content: AnyToDeviceEventContent, content: AnyToDeviceEventContent,
) -> ToDeviceRequest { ) -> OwnedToDeviceRequest {
let mut messages = BTreeMap::new(); let mut messages = BTreeMap::new();
let mut user_messages = BTreeMap::new(); let mut user_messages = BTreeMap::new();
@ -442,7 +431,7 @@ pub fn content_to_request(
_ => unreachable!(), _ => unreachable!(),
}; };
ToDeviceRequest { OwnedToDeviceRequest {
txn_id: Uuid::new_v4().to_string(), txn_id: Uuid::new_v4().to_string(),
event_type, event_type,
messages, messages,

View File

@ -19,7 +19,7 @@ use std::sync::{Arc, Mutex};
use tracing::{info, trace, warn}; use tracing::{info, trace, warn};
use matrix_sdk_common::{ use matrix_sdk_common::{
api::r0::to_device::send_event_to_device::Request as ToDeviceRequest, api::r0::to_device::send_event_to_device::IncomingRequest as OwnedToDeviceRequest,
events::{ events::{
key::verification::{ key::verification::{
accept::AcceptEventContent, cancel::CancelCode, mac::MacEventContent, accept::AcceptEventContent, cancel::CancelCode, mac::MacEventContent,
@ -138,7 +138,7 @@ impl Sas {
/// ///
/// This does nothing if the verification was already accepted, otherwise it /// This does nothing if the verification was already accepted, otherwise it
/// returns an `AcceptEventContent` that needs to be sent out. /// returns an `AcceptEventContent` that needs to be sent out.
pub fn accept(&self) -> Option<ToDeviceRequest> { pub fn accept(&self) -> Option<OwnedToDeviceRequest> {
self.inner.lock().unwrap().accept().map(|c| { self.inner.lock().unwrap().accept().map(|c| {
let content = AnyToDeviceEventContent::KeyVerificationAccept(c); let content = AnyToDeviceEventContent::KeyVerificationAccept(c);
self.content_to_request(content) self.content_to_request(content)
@ -152,7 +152,7 @@ impl Sas {
/// Does nothing if we're not in a state where we can confirm the short auth /// Does nothing if we're not in a state where we can confirm the short auth
/// string, otherwise returns a `MacEventContent` that needs to be sent to /// string, otherwise returns a `MacEventContent` that needs to be sent to
/// the server. /// the server.
pub async fn confirm(&self) -> Result<Option<ToDeviceRequest>, CryptoStoreError> { pub async fn confirm(&self) -> Result<Option<OwnedToDeviceRequest>, CryptoStoreError> {
let (content, done) = { let (content, done) = {
let mut guard = self.inner.lock().unwrap(); let mut guard = self.inner.lock().unwrap();
let sas: InnerSas = (*guard).clone(); let sas: InnerSas = (*guard).clone();
@ -234,7 +234,7 @@ impl Sas {
/// ///
/// Returns None if the `Sas` object is already in a canceled state, /// Returns None if the `Sas` object is already in a canceled state,
/// otherwise it returns a request that needs to be sent out. /// otherwise it returns a request that needs to be sent out.
pub fn cancel(&self) -> Option<ToDeviceRequest> { pub fn cancel(&self) -> Option<OwnedToDeviceRequest> {
let mut guard = self.inner.lock().unwrap(); let mut guard = self.inner.lock().unwrap();
let sas: InnerSas = (*guard).clone(); let sas: InnerSas = (*guard).clone();
let (sas, content) = sas.cancel(); let (sas, content) = sas.cancel();
@ -291,7 +291,10 @@ impl Sas {
self.inner.lock().unwrap().verified_devices() self.inner.lock().unwrap().verified_devices()
} }
pub(crate) fn content_to_request(&self, content: AnyToDeviceEventContent) -> ToDeviceRequest { pub(crate) fn content_to_request(
&self,
content: AnyToDeviceEventContent,
) -> OwnedToDeviceRequest {
content_to_request(self.other_user_id(), self.other_device_id(), content) content_to_request(self.other_user_id(), self.other_device_id(), content)
} }
} }