diff --git a/ruma-client-api/CHANGELOG.md b/ruma-client-api/CHANGELOG.md index c04a9e47..4ba3f3d9 100644 --- a/ruma-client-api/CHANGELOG.md +++ b/ruma-client-api/CHANGELOG.md @@ -50,6 +50,8 @@ Breaking changes: * Update strum dependency to 0.19 * Rename `r0::message::{create_message_event => send_message_event}` * Rename `r0::state::{create_state_event_* => send_state_event_*}` +* Replace `r0::keys::{AlgorithmAndDeviceId, KeyAlgorithm}` with + `ruma_identifiers::{DeviceKeyId, DeviceKeyAlgorithm}`, respectively Improvements: diff --git a/ruma-client-api/src/r0/backup.rs b/ruma-client-api/src/r0/backup.rs index 600f8660..7d84a2a7 100644 --- a/ruma-client-api/src/r0/backup.rs +++ b/ruma-client-api/src/r0/backup.rs @@ -8,12 +8,10 @@ pub mod get_latest_backup; pub mod update_backup; use js_int::UInt; -use ruma_identifiers::UserId; +use ruma_identifiers::{DeviceKeyId, UserId}; use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; -use crate::r0::keys::AlgorithmAndDeviceId; - // TODO: remove /// A wrapper around a mapping of session IDs to key data. #[cfg(feature = "unstable-synapse-quirks")] @@ -33,7 +31,7 @@ pub enum BackupAlgorithm { /// The curve25519 public key used to encrypt the backups, encoded in unpadded base64. public_key: String, /// Signatures of the auth_data as Signed JSON. - signatures: BTreeMap>, + signatures: BTreeMap>, }, } diff --git a/ruma-client-api/src/r0/keys.rs b/ruma-client-api/src/r0/keys.rs index 3f82ab9e..b1bb765c 100644 --- a/ruma-client-api/src/r0/keys.rs +++ b/ruma-client-api/src/r0/keys.rs @@ -1,17 +1,10 @@ //! Endpoints for key management -use std::{ - collections::BTreeMap, - convert::TryFrom, - fmt::{self, Debug, Display, Formatter}, -}; +use std::{collections::BTreeMap, fmt::Debug}; use ruma_events::Algorithm; -use ruma_identifiers::{DeviceId, UserId}; -use serde::{ - de::{self, Unexpected}, - Deserialize, Deserializer, Serialize, Serializer, -}; +use ruma_identifiers::{DeviceId, DeviceKeyId, UserId}; +use serde::{Deserialize, Serialize}; pub mod claim_keys; pub mod get_key_changes; @@ -23,98 +16,6 @@ pub mod upload_signatures; #[cfg(feature = "unstable-pre-spec")] pub mod upload_signing_keys; -/// The basic key algorithms in the specification -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] -pub enum KeyAlgorithm { - /// The Ed25519 signature algorithm. - #[serde(rename = "ed25519")] - Ed25519, - - /// The Curve25519 ECDH algorithm. - #[serde(rename = "curve25519")] - Curve25519, - - /// The Curve25519 ECDH algorithm, but the key also contains signatures - #[serde(rename = "signed_curve25519")] - SignedCurve25519, -} - -impl Display for KeyAlgorithm { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - let algorithm_str = match *self { - KeyAlgorithm::Ed25519 => "ed25519", - KeyAlgorithm::Curve25519 => "curve25519", - KeyAlgorithm::SignedCurve25519 => "signed_curve25519", - }; - write!(f, "{}", algorithm_str)?; - Ok(()) - } -} - -impl TryFrom<&'_ str> for KeyAlgorithm { - type Error = &'static str; - fn try_from(s: &str) -> Result { - match s { - "ed25519" => Ok(KeyAlgorithm::Ed25519), - "curve25519" => Ok(KeyAlgorithm::Curve25519), - "signed_curve25519" => Ok(KeyAlgorithm::SignedCurve25519), - _ => Err("Unknown algorithm"), - } - } -} - -/// A key algorithm and a device id, combined with a ':' -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] -pub struct AlgorithmAndDeviceId(pub KeyAlgorithm, pub Box); - -impl Display for AlgorithmAndDeviceId { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "{}:{}", self.0, self.1) - } -} - -impl Serialize for AlgorithmAndDeviceId { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str(&self.to_string()) - } -} - -impl<'de> Deserialize<'de> for AlgorithmAndDeviceId { - #[allow(clippy::comparison_chain)] - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let value = String::deserialize(deserializer)?; - let parts = value.split(':').collect::>(); - - const EXPECTED: &str = "a string composed of an algorithm and a device id separated by ':'"; - - if parts.len() < 2 { - return Err(de::Error::invalid_type( - Unexpected::Other("string without a ':' separator"), - &EXPECTED, - )); - } else if parts.len() > 2 { - return Err(de::Error::invalid_type( - Unexpected::Other("string with more than one ':' separator"), - &EXPECTED, - )); - } - - let algorithm_result = KeyAlgorithm::try_from(parts[0]); - match algorithm_result { - Ok(algorithm) => Ok(AlgorithmAndDeviceId(algorithm, parts[1].into())), - Err(_) => { - Err(de::Error::invalid_value(Unexpected::Str(parts[0]), &"valid key algorithm")) - } - } - } -} - /// Identity keys for a device. #[derive(Debug, Clone, Serialize, Deserialize)] pub struct DeviceKeys { @@ -128,10 +29,10 @@ pub struct DeviceKeys { pub algorithms: Vec, /// Public identity keys. - pub keys: BTreeMap, + pub keys: BTreeMap, /// Signatures for the device key object. - pub signatures: BTreeMap>, + pub signatures: BTreeMap>, /// Additional data added to the device key information by intermediate servers, and /// not covered by the signatures. @@ -153,7 +54,7 @@ pub struct SignedKey { pub key: String, /// Signatures for the key object. - pub signatures: BTreeMap>, + pub signatures: BTreeMap>, } /// A one-time public key for "pre-key" messages. diff --git a/ruma-client-api/src/r0/keys/claim_keys.rs b/ruma-client-api/src/r0/keys/claim_keys.rs index 4cd08281..0b5f2bd9 100644 --- a/ruma-client-api/src/r0/keys/claim_keys.rs +++ b/ruma-client-api/src/r0/keys/claim_keys.rs @@ -1,14 +1,14 @@ -//! [POST /_matrix/client/r0/keys/claim](https://matrix.org/docs/spec/client_server/r0.6.0#post-matrix-client-r0-keys-claim) +//! [POST /_matrix/client/r0/keys/claim](https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-keys-claim) use std::collections::BTreeMap; use std::time::Duration; use ruma_api::ruma_api; -use ruma_identifiers::{DeviceId, UserId}; +use ruma_identifiers::{DeviceId, DeviceKeyAlgorithm, DeviceKeyId, UserId}; use serde_json::Value as JsonValue; -use super::{AlgorithmAndDeviceId, KeyAlgorithm, OneTimeKey}; +use super::OneTimeKey; ruma_api! { metadata: { @@ -31,7 +31,7 @@ ruma_api! { pub timeout: Option, /// The keys to be claimed. - pub one_time_keys: BTreeMap, KeyAlgorithm>>, + pub one_time_keys: BTreeMap, DeviceKeyAlgorithm>>, } response: { @@ -47,4 +47,4 @@ ruma_api! { } /// The one-time keys for a given device. -pub type OneTimeKeys = BTreeMap, BTreeMap>; +pub type OneTimeKeys = BTreeMap, BTreeMap>; diff --git a/ruma-client-api/src/r0/keys/upload_keys.rs b/ruma-client-api/src/r0/keys/upload_keys.rs index 0d649bde..43684808 100644 --- a/ruma-client-api/src/r0/keys/upload_keys.rs +++ b/ruma-client-api/src/r0/keys/upload_keys.rs @@ -4,8 +4,9 @@ use std::collections::BTreeMap; use js_int::UInt; use ruma_api::ruma_api; +use ruma_identifiers::{DeviceKeyAlgorithm, DeviceKeyId}; -use super::{AlgorithmAndDeviceId, DeviceKeys, KeyAlgorithm, OneTimeKey}; +use super::{DeviceKeys, OneTimeKey}; ruma_api! { metadata: { @@ -24,13 +25,13 @@ ruma_api! { /// One-time public keys for "pre-key" messages. #[serde(skip_serializing_if = "Option::is_none")] - pub one_time_keys: Option>, + pub one_time_keys: Option>, } response: { /// For each key algorithm, the number of unclaimed one-time keys of that /// type currently held on the server for this device. - pub one_time_key_counts: BTreeMap + pub one_time_key_counts: BTreeMap } error: crate::Error diff --git a/ruma-client-api/src/r0/sync/sync_events.rs b/ruma-client-api/src/r0/sync/sync_events.rs index e0a28dfa..8450fd28 100644 --- a/ruma-client-api/src/r0/sync/sync_events.rs +++ b/ruma-client-api/src/r0/sync/sync_events.rs @@ -9,10 +9,10 @@ use ruma_events::{ presence::PresenceEvent, AnyBasicEvent, AnyStrippedStateEvent, AnySyncEphemeralRoomEvent, AnySyncRoomEvent, AnySyncStateEvent, AnyToDeviceEvent, }; -use ruma_identifiers::{RoomId, UserId}; +use ruma_identifiers::{DeviceKeyAlgorithm, RoomId, UserId}; use serde::{Deserialize, Serialize}; -use crate::r0::{filter::FilterDefinition, keys::KeyAlgorithm}; +use crate::r0::filter::FilterDefinition; ruma_api! { metadata: { @@ -87,7 +87,7 @@ ruma_api! { /// For each key algorithm, the number of unclaimed one-time keys /// currently held on the server for a device. #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] - pub device_one_time_keys_count: BTreeMap, + pub device_one_time_keys_count: BTreeMap, } error: crate::Error