use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use crate::{
serde::{Base64, StringEnum},
EventEncryptionAlgorithm, OwnedDeviceId, OwnedDeviceKeyId, OwnedUserId, PrivOwnedStr,
};
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct DeviceKeys {
pub user_id: OwnedUserId,
pub device_id: OwnedDeviceId,
pub algorithms: Vec<EventEncryptionAlgorithm>,
pub keys: BTreeMap<OwnedDeviceKeyId, String>,
pub signatures: BTreeMap<OwnedUserId, BTreeMap<OwnedDeviceKeyId, String>>,
#[serde(default, skip_serializing_if = "UnsignedDeviceInfo::is_empty")]
pub unsigned: UnsignedDeviceInfo,
}
impl DeviceKeys {
pub fn new(
user_id: OwnedUserId,
device_id: OwnedDeviceId,
algorithms: Vec<EventEncryptionAlgorithm>,
keys: BTreeMap<OwnedDeviceKeyId, String>,
signatures: BTreeMap<OwnedUserId, BTreeMap<OwnedDeviceKeyId, String>>,
) -> Self {
Self { user_id, device_id, algorithms, keys, signatures, unsigned: Default::default() }
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct UnsignedDeviceInfo {
#[serde(skip_serializing_if = "Option::is_none")]
pub device_display_name: Option<String>,
}
impl UnsignedDeviceInfo {
pub fn new() -> Self {
Default::default()
}
pub fn is_empty(&self) -> bool {
self.device_display_name.is_none()
}
}
pub type SignedKeySignatures = BTreeMap<OwnedUserId, BTreeMap<OwnedDeviceKeyId, String>>;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct SignedKey {
pub key: Base64,
pub signatures: SignedKeySignatures,
#[serde(default, skip_serializing_if = "crate::serde::is_default")]
pub fallback: bool,
}
impl SignedKey {
pub fn new(key: Base64, signatures: SignedKeySignatures) -> Self {
Self { key, signatures, fallback: false }
}
pub fn new_fallback(key: Base64, signatures: SignedKeySignatures) -> Self {
Self { key, signatures, fallback: true }
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[serde(untagged)]
pub enum OneTimeKey {
SignedKey(SignedKey),
Key(String),
}
pub type CrossSigningKeySignatures = BTreeMap<OwnedUserId, BTreeMap<OwnedDeviceKeyId, String>>;
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct CrossSigningKey {
pub user_id: OwnedUserId,
pub usage: Vec<KeyUsage>,
pub keys: BTreeMap<OwnedDeviceKeyId, String>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub signatures: CrossSigningKeySignatures,
}
impl CrossSigningKey {
pub fn new(
user_id: OwnedUserId,
usage: Vec<KeyUsage>,
keys: BTreeMap<OwnedDeviceKeyId, String>,
signatures: CrossSigningKeySignatures,
) -> Self {
Self { user_id, usage, keys, signatures }
}
}
#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))]
#[derive(Clone, PartialEq, Eq, StringEnum)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_enum(rename_all = "snake_case")]
pub enum KeyUsage {
Master,
SelfSigning,
UserSigning,
#[doc(hidden)]
_Custom(PrivOwnedStr),
}