Use DeviceKeyId and DeviceKeyAlgorithm from ruma-identifiers

This commit is contained in:
Isaiah Inuwa 2020-07-31 16:15:16 -05:00 committed by Jonas Platte
parent 9fa96268b4
commit 7240184c1d
6 changed files with 22 additions and 120 deletions

View File

@ -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:

View File

@ -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<UserId, BTreeMap<AlgorithmAndDeviceId, String>>,
signatures: BTreeMap<UserId, BTreeMap<DeviceKeyId, String>>,
},
}

View File

@ -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<Self, Self::Error> {
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<DeviceId>);
impl Display for AlgorithmAndDeviceId {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", self.0, self.1)
}
}
impl Serialize for AlgorithmAndDeviceId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}
impl<'de> Deserialize<'de> for AlgorithmAndDeviceId {
#[allow(clippy::comparison_chain)]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let value = String::deserialize(deserializer)?;
let parts = value.split(':').collect::<Vec<_>>();
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<Algorithm>,
/// Public identity keys.
pub keys: BTreeMap<AlgorithmAndDeviceId, String>,
pub keys: BTreeMap<DeviceKeyId, String>,
/// Signatures for the device key object.
pub signatures: BTreeMap<UserId, BTreeMap<AlgorithmAndDeviceId, String>>,
pub signatures: BTreeMap<UserId, BTreeMap<DeviceKeyId, String>>,
/// 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<UserId, BTreeMap<AlgorithmAndDeviceId, String>>,
pub signatures: BTreeMap<UserId, BTreeMap<DeviceKeyId, String>>,
}
/// A one-time public key for "pre-key" messages.

View File

@ -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<Duration>,
/// The keys to be claimed.
pub one_time_keys: BTreeMap<UserId, BTreeMap<Box<DeviceId>, KeyAlgorithm>>,
pub one_time_keys: BTreeMap<UserId, BTreeMap<Box<DeviceId>, DeviceKeyAlgorithm>>,
}
response: {
@ -47,4 +47,4 @@ ruma_api! {
}
/// The one-time keys for a given device.
pub type OneTimeKeys = BTreeMap<Box<DeviceId>, BTreeMap<AlgorithmAndDeviceId, OneTimeKey>>;
pub type OneTimeKeys = BTreeMap<Box<DeviceId>, BTreeMap<DeviceKeyId, OneTimeKey>>;

View File

@ -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<BTreeMap<AlgorithmAndDeviceId, OneTimeKey>>,
pub one_time_keys: Option<BTreeMap<DeviceKeyId, OneTimeKey>>,
}
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<KeyAlgorithm, UInt>
pub one_time_key_counts: BTreeMap<DeviceKeyAlgorithm, UInt>
}
error: crate::Error

View File

@ -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<KeyAlgorithm, UInt>,
pub device_one_time_keys_count: BTreeMap<DeviceKeyAlgorithm, UInt>,
}
error: crate::Error