Skip to content

Commit 3b423df

Browse files
committed
client-api: Add support for fallback keys
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
1 parent 59566e6 commit 3b423df

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

crates/ruma-client-api/src/r0/keys/upload_keys.rs

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ ruma_api! {
2929
/// One-time public keys for "pre-key" messages.
3030
#[serde(skip_serializing_if = "Option::is_none")]
3131
pub one_time_keys: Option<BTreeMap<Box<DeviceKeyId>, Raw<OneTimeKey>>>,
32+
33+
/// Fallback public keys for "pre-key" messages.
34+
#[cfg(feature = "unstable-pre-spec")]
35+
#[serde(skip_serializing_if = "Option::is_none", rename = "org.matrix.msc2732.fallback_keys")]
36+
pub fallback_keys: Option<BTreeMap<Box<DeviceKeyId>, Raw<OneTimeKey>>>,
3237
}
3338

3439
response: {

crates/ruma-client-api/src/r0/sync/sync_events.rs

+11
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ ruma_api! {
9393
/// currently held on the server for a device.
9494
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
9595
pub device_one_time_keys_count: BTreeMap<DeviceKeyAlgorithm, UInt>,
96+
97+
/// For each key algorithm, the number of unclaimed one-time keys
98+
/// currently held on the server for a device.
99+
///
100+
/// The presence of this field indicates that the server supports
101+
/// fallback keys.
102+
#[cfg(feature = "unstable-pre-spec")]
103+
#[serde(rename = "org.matrix.msc2732.device_unused_fallback_key_types")]
104+
pub device_unused_fallback_key_types: Option<Vec<DeviceKeyAlgorithm>>,
96105
}
97106

98107
error: crate::Error
@@ -116,6 +125,8 @@ impl Response {
116125
to_device: Default::default(),
117126
device_lists: Default::default(),
118127
device_one_time_keys_count: BTreeMap::new(),
128+
#[cfg(feature = "unstable-pre-spec")]
129+
device_unused_fallback_key_types: None,
119130
}
120131
}
121132
}

crates/ruma-common/src/encryption.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,33 @@ pub struct SignedKey {
8383

8484
/// Signatures for the key object.
8585
pub signatures: SignedKeySignatures,
86+
87+
/// Is this key considered to be a fallback key, defaults to false.
88+
#[cfg(feature = "unstable-pre-spec")]
89+
#[serde(default, skip_serializing_if = "is_false")]
90+
pub fallback: bool,
91+
}
92+
93+
#[cfg(feature = "unstable-pre-spec")]
94+
fn is_false(value: &bool) -> bool {
95+
*value == false
8696
}
8797

8898
impl SignedKey {
8999
/// Creates a new `SignedKey` with the given key and signatures.
90100
pub fn new(key: String, signatures: SignedKeySignatures) -> Self {
91-
Self { key, signatures }
101+
Self {
102+
key,
103+
signatures,
104+
#[cfg(feature = "unstable-pre-spec")]
105+
fallback: false,
106+
}
107+
}
108+
109+
/// Creates a new fallback `SignedKey` with the given key and signatures.
110+
#[cfg(feature = "unstable-pre-spec")]
111+
pub fn new_fallback(key: String, signatures: SignedKeySignatures) -> Self {
112+
Self { key, signatures, fallback: true }
92113
}
93114
}
94115

0 commit comments

Comments
 (0)