Use DeviceKeyId and DeviceKeyAlgorithm from ruma-identifiers
This commit is contained in:
parent
9fa96268b4
commit
7240184c1d
@ -50,6 +50,8 @@ Breaking changes:
|
|||||||
* Update strum dependency to 0.19
|
* Update strum dependency to 0.19
|
||||||
* Rename `r0::message::{create_message_event => send_message_event}`
|
* Rename `r0::message::{create_message_event => send_message_event}`
|
||||||
* Rename `r0::state::{create_state_event_* => send_state_event_*}`
|
* Rename `r0::state::{create_state_event_* => send_state_event_*}`
|
||||||
|
* Replace `r0::keys::{AlgorithmAndDeviceId, KeyAlgorithm}` with
|
||||||
|
`ruma_identifiers::{DeviceKeyId, DeviceKeyAlgorithm}`, respectively
|
||||||
|
|
||||||
Improvements:
|
Improvements:
|
||||||
|
|
||||||
|
@ -8,12 +8,10 @@ pub mod get_latest_backup;
|
|||||||
pub mod update_backup;
|
pub mod update_backup;
|
||||||
|
|
||||||
use js_int::UInt;
|
use js_int::UInt;
|
||||||
use ruma_identifiers::UserId;
|
use ruma_identifiers::{DeviceKeyId, UserId};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
use crate::r0::keys::AlgorithmAndDeviceId;
|
|
||||||
|
|
||||||
// TODO: remove
|
// TODO: remove
|
||||||
/// A wrapper around a mapping of session IDs to key data.
|
/// A wrapper around a mapping of session IDs to key data.
|
||||||
#[cfg(feature = "unstable-synapse-quirks")]
|
#[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.
|
/// The curve25519 public key used to encrypt the backups, encoded in unpadded base64.
|
||||||
public_key: String,
|
public_key: String,
|
||||||
/// Signatures of the auth_data as Signed JSON.
|
/// Signatures of the auth_data as Signed JSON.
|
||||||
signatures: BTreeMap<UserId, BTreeMap<AlgorithmAndDeviceId, String>>,
|
signatures: BTreeMap<UserId, BTreeMap<DeviceKeyId, String>>,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,17 +1,10 @@
|
|||||||
//! Endpoints for key management
|
//! Endpoints for key management
|
||||||
|
|
||||||
use std::{
|
use std::{collections::BTreeMap, fmt::Debug};
|
||||||
collections::BTreeMap,
|
|
||||||
convert::TryFrom,
|
|
||||||
fmt::{self, Debug, Display, Formatter},
|
|
||||||
};
|
|
||||||
|
|
||||||
use ruma_events::Algorithm;
|
use ruma_events::Algorithm;
|
||||||
use ruma_identifiers::{DeviceId, UserId};
|
use ruma_identifiers::{DeviceId, DeviceKeyId, UserId};
|
||||||
use serde::{
|
use serde::{Deserialize, Serialize};
|
||||||
de::{self, Unexpected},
|
|
||||||
Deserialize, Deserializer, Serialize, Serializer,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub mod claim_keys;
|
pub mod claim_keys;
|
||||||
pub mod get_key_changes;
|
pub mod get_key_changes;
|
||||||
@ -23,98 +16,6 @@ pub mod upload_signatures;
|
|||||||
#[cfg(feature = "unstable-pre-spec")]
|
#[cfg(feature = "unstable-pre-spec")]
|
||||||
pub mod upload_signing_keys;
|
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.
|
/// Identity keys for a device.
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct DeviceKeys {
|
pub struct DeviceKeys {
|
||||||
@ -128,10 +29,10 @@ pub struct DeviceKeys {
|
|||||||
pub algorithms: Vec<Algorithm>,
|
pub algorithms: Vec<Algorithm>,
|
||||||
|
|
||||||
/// Public identity keys.
|
/// Public identity keys.
|
||||||
pub keys: BTreeMap<AlgorithmAndDeviceId, String>,
|
pub keys: BTreeMap<DeviceKeyId, String>,
|
||||||
|
|
||||||
/// Signatures for the device key object.
|
/// 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
|
/// Additional data added to the device key information by intermediate servers, and
|
||||||
/// not covered by the signatures.
|
/// not covered by the signatures.
|
||||||
@ -153,7 +54,7 @@ pub struct SignedKey {
|
|||||||
pub key: String,
|
pub key: String,
|
||||||
|
|
||||||
/// Signatures for the key object.
|
/// 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.
|
/// A one-time public key for "pre-key" messages.
|
||||||
|
@ -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::collections::BTreeMap;
|
||||||
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use ruma_api::ruma_api;
|
use ruma_api::ruma_api;
|
||||||
use ruma_identifiers::{DeviceId, UserId};
|
use ruma_identifiers::{DeviceId, DeviceKeyAlgorithm, DeviceKeyId, UserId};
|
||||||
use serde_json::Value as JsonValue;
|
use serde_json::Value as JsonValue;
|
||||||
|
|
||||||
use super::{AlgorithmAndDeviceId, KeyAlgorithm, OneTimeKey};
|
use super::OneTimeKey;
|
||||||
|
|
||||||
ruma_api! {
|
ruma_api! {
|
||||||
metadata: {
|
metadata: {
|
||||||
@ -31,7 +31,7 @@ ruma_api! {
|
|||||||
pub timeout: Option<Duration>,
|
pub timeout: Option<Duration>,
|
||||||
|
|
||||||
/// The keys to be claimed.
|
/// 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: {
|
response: {
|
||||||
@ -47,4 +47,4 @@ ruma_api! {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The one-time keys for a given device.
|
/// 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>>;
|
||||||
|
@ -4,8 +4,9 @@ use std::collections::BTreeMap;
|
|||||||
|
|
||||||
use js_int::UInt;
|
use js_int::UInt;
|
||||||
use ruma_api::ruma_api;
|
use ruma_api::ruma_api;
|
||||||
|
use ruma_identifiers::{DeviceKeyAlgorithm, DeviceKeyId};
|
||||||
|
|
||||||
use super::{AlgorithmAndDeviceId, DeviceKeys, KeyAlgorithm, OneTimeKey};
|
use super::{DeviceKeys, OneTimeKey};
|
||||||
|
|
||||||
ruma_api! {
|
ruma_api! {
|
||||||
metadata: {
|
metadata: {
|
||||||
@ -24,13 +25,13 @@ ruma_api! {
|
|||||||
|
|
||||||
/// One-time public keys for "pre-key" messages.
|
/// One-time public keys for "pre-key" messages.
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub one_time_keys: Option<BTreeMap<AlgorithmAndDeviceId, OneTimeKey>>,
|
pub one_time_keys: Option<BTreeMap<DeviceKeyId, OneTimeKey>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
response: {
|
response: {
|
||||||
/// For each key algorithm, the number of unclaimed one-time keys of that
|
/// For each key algorithm, the number of unclaimed one-time keys of that
|
||||||
/// type currently held on the server for this device.
|
/// 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
|
error: crate::Error
|
||||||
|
@ -9,10 +9,10 @@ use ruma_events::{
|
|||||||
presence::PresenceEvent, AnyBasicEvent, AnyStrippedStateEvent, AnySyncEphemeralRoomEvent,
|
presence::PresenceEvent, AnyBasicEvent, AnyStrippedStateEvent, AnySyncEphemeralRoomEvent,
|
||||||
AnySyncRoomEvent, AnySyncStateEvent, AnyToDeviceEvent,
|
AnySyncRoomEvent, AnySyncStateEvent, AnyToDeviceEvent,
|
||||||
};
|
};
|
||||||
use ruma_identifiers::{RoomId, UserId};
|
use ruma_identifiers::{DeviceKeyAlgorithm, RoomId, UserId};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::r0::{filter::FilterDefinition, keys::KeyAlgorithm};
|
use crate::r0::filter::FilterDefinition;
|
||||||
|
|
||||||
ruma_api! {
|
ruma_api! {
|
||||||
metadata: {
|
metadata: {
|
||||||
@ -87,7 +87,7 @@ ruma_api! {
|
|||||||
/// For each key algorithm, the number of unclaimed one-time keys
|
/// For each key algorithm, the number of unclaimed one-time keys
|
||||||
/// currently held on the server for a device.
|
/// currently held on the server for a device.
|
||||||
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
|
#[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
|
error: crate::Error
|
||||||
|
Loading…
x
Reference in New Issue
Block a user