Skip to content

Commit

Permalink
client-api: Add support for fallback keys
Browse files Browse the repository at this point in the history
This implements support for MSC2732[1], fallback keys. Only support to
upload and get notifications about fallback keys via `/sync` is
implemented.

[1]: matrix-org/matrix-spec-proposals#2732
  • Loading branch information
poljar committed Dec 3, 2021
1 parent 59566e6 commit 3b423df
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
5 changes: 5 additions & 0 deletions crates/ruma-client-api/src/r0/keys/upload_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ ruma_api! {
/// One-time public keys for "pre-key" messages.
#[serde(skip_serializing_if = "Option::is_none")]
pub one_time_keys: Option<BTreeMap<Box<DeviceKeyId>, Raw<OneTimeKey>>>,

/// Fallback public keys for "pre-key" messages.
#[cfg(feature = "unstable-pre-spec")]
#[serde(skip_serializing_if = "Option::is_none", rename = "org.matrix.msc2732.fallback_keys")]
pub fallback_keys: Option<BTreeMap<Box<DeviceKeyId>, Raw<OneTimeKey>>>,
}

response: {
Expand Down
11 changes: 11 additions & 0 deletions crates/ruma-client-api/src/r0/sync/sync_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ ruma_api! {
/// currently held on the server for a device.
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub device_one_time_keys_count: BTreeMap<DeviceKeyAlgorithm, UInt>,

/// For each key algorithm, the number of unclaimed one-time keys
/// currently held on the server for a device.
///
/// The presence of this field indicates that the server supports
/// fallback keys.
#[cfg(feature = "unstable-pre-spec")]
#[serde(rename = "org.matrix.msc2732.device_unused_fallback_key_types")]
pub device_unused_fallback_key_types: Option<Vec<DeviceKeyAlgorithm>>,
}

error: crate::Error
Expand All @@ -116,6 +125,8 @@ impl Response {
to_device: Default::default(),
device_lists: Default::default(),
device_one_time_keys_count: BTreeMap::new(),
#[cfg(feature = "unstable-pre-spec")]
device_unused_fallback_key_types: None,
}
}
}
Expand Down
23 changes: 22 additions & 1 deletion crates/ruma-common/src/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,33 @@ pub struct SignedKey {

/// Signatures for the key object.
pub signatures: SignedKeySignatures,

/// Is this key considered to be a fallback key, defaults to false.
#[cfg(feature = "unstable-pre-spec")]
#[serde(default, skip_serializing_if = "is_false")]
pub fallback: bool,
}

#[cfg(feature = "unstable-pre-spec")]
fn is_false(value: &bool) -> bool {
*value == false
}

impl SignedKey {
/// Creates a new `SignedKey` with the given key and signatures.
pub fn new(key: String, signatures: SignedKeySignatures) -> Self {
Self { key, signatures }
Self {
key,
signatures,
#[cfg(feature = "unstable-pre-spec")]
fallback: false,
}
}

/// Creates a new fallback `SignedKey` with the given key and signatures.
#[cfg(feature = "unstable-pre-spec")]
pub fn new_fallback(key: String, signatures: SignedKeySignatures) -> Self {
Self { key, signatures, fallback: true }
}
}

Expand Down

0 comments on commit 3b423df

Please sign in to comment.