Convert m.key.verification.{accept,cancel,key,mac,request} and

m.room_key to the new API.
This commit is contained in:
Jimmy Cuadra 2019-06-20 17:20:03 -07:00
parent 3c70dac634
commit 20d2482108
11 changed files with 209 additions and 177 deletions

View File

@ -37,9 +37,7 @@ mod tests {
content.insert(alice.clone(), room.clone());
let event = DirectEvent {
content,
};
let event = DirectEvent { content };
assert_eq!(
to_string(&event).unwrap(),

View File

@ -29,10 +29,8 @@ mod tests {
use super::{DummyEvent, Empty};
#[test]
fn serialization() {
let dummy_event = DummyEvent {
content: Empty,
};
fn serialization() {
let dummy_event = DummyEvent { content: Empty };
let actual = serde_json::to_string(&dummy_event).unwrap();
let expected = r#"{"content":{},"type":"m.dummy"}"#;

View File

@ -5,7 +5,7 @@ use std::collections::HashMap;
use ruma_identifiers::UserId;
use serde::{ser::SerializeStruct, Deserialize, Deserializer, Serialize, Serializer};
use crate::{Empty, Event};
use crate::{Empty, Event, EventType};
/// A list of users to ignore.
#[derive(Clone, Debug, PartialEq)]
@ -37,7 +37,7 @@ impl IgnoredUserListEvent {
impl Serialize for IgnoredUserListEvent {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer
S: Serializer,
{
let mut state = serializer.serialize_struct("IgnoredUserListEvent", 2)?;
@ -49,9 +49,6 @@ impl Serialize for IgnoredUserListEvent {
}
impl crate::Event for IgnoredUserListEvent {
/// The type of the event.
const EVENT_TYPE: crate::EventType = crate::EventType::IgnoredUserList;
/// The type of this event's `content` field.
type Content = IgnoredUserListEventContent;
@ -59,12 +56,17 @@ impl crate::Event for IgnoredUserListEvent {
fn content(&self) -> &Self::Content {
&self.content
}
/// The type of the event.
fn event_type(&self) -> EventType {
EventType::IgnoredUserList
}
}
impl Serialize for IgnoredUserListEventContent {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer
S: Serializer,
{
let mut map = HashMap::new();
@ -72,17 +74,15 @@ impl Serialize for IgnoredUserListEventContent {
map.insert(user_id.clone(), Empty);
}
let raw = raw::IgnoredUserListEventContent {
ignored_users: map,
};
let raw = raw::IgnoredUserListEventContent { ignored_users: map };
raw.serialize(serializer)
}
}
mod raw {
use crate::Empty;
use super::*;
use crate::Empty;
/// A list of users to ignore.
#[derive(Clone, Debug, Deserialize)]

View File

@ -7,9 +7,9 @@ use serde::{Deserialize, Serialize};
pub mod accept;
pub mod cancel;
pub mod key;
pub mod mac;
// pub mod mac;
pub mod request;
pub mod start;
// pub mod start;
/// A hash algorithm.
#[derive(Clone, Copy, Debug, Serialize, PartialEq, Deserialize)]

View File

@ -1,51 +1,51 @@
//! Types for the *m.key.verification.accept* event.
use serde::{Deserialize, Serialize};
use ruma_events_macros::ruma_event;
use super::{
HashAlgorithm, KeyAgreementProtocol, MessageAuthenticationCode, ShortAuthenticationString,
VerificationMethod,
};
event! {
ruma_event! {
/// Accepts a previously sent *m.key.verification.start* messge.
///
/// Typically sent as a to-device event.
pub struct AcceptEvent(AcceptEventContent) {}
}
/// The payload of an *m.key.verification.accept* event.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct AcceptEventContent {
/// An opaque identifier for the verification process.
///
/// Must be the same as the one used for the *m.key.verification.start* message.
pub transaction_id: String,
/// The verification method to use.
///
/// Must be `m.sas.v1`.
pub method: VerificationMethod,
/// The key agreement protocol the device is choosing to use, out of the options in the
/// *m.key.verification.start* message.
pub key_agreement_protocol: KeyAgreementProtocol,
/// The hash method the device is choosing to use, out of the options in the
/// *m.key.verification.start* message.
pub hash: HashAlgorithm,
/// The message authentication code the device is choosing to use, out of the options in the
/// *m.key.verification.start* message.
pub message_authentication_code: MessageAuthenticationCode,
/// The SAS methods both devices involved in the verification process understand.
///
/// Must be a subset of the options in the *m.key.verification.start* message.
pub short_authentication_string: Vec<ShortAuthenticationString>,
/// The hash (encoded as unpadded base64) of the concatenation of the device's ephemeral public
/// key (encoded as unpadded base64) and the canonical JSON representation of the
/// *m.key.verification.start* message.
pub commitment: String,
AcceptEvent {
kind: Event,
event_type: KeyVerificationAccept,
content: {
/// An opaque identifier for the verification process.
///
/// Must be the same as the one used for the *m.key.verification.start* message.
pub transaction_id: String,
/// The verification method to use.
///
/// Must be `m.sas.v1`.
pub method: VerificationMethod,
/// The key agreement protocol the device is choosing to use, out of the options in the
/// *m.key.verification.start* message.
pub key_agreement_protocol: KeyAgreementProtocol,
/// The hash method the device is choosing to use, out of the options in the
/// *m.key.verification.start* message.
pub hash: HashAlgorithm,
/// The message authentication code the device is choosing to use, out of the options in the
/// *m.key.verification.start* message.
pub message_authentication_code: MessageAuthenticationCode,
/// The SAS methods both devices involved in the verification process understand.
///
/// Must be a subset of the options in the *m.key.verification.start* message.
pub short_authentication_string: Vec<ShortAuthenticationString>,
/// The hash (encoded as unpadded base64) of the concatenation of the device's ephemeral public
/// key (encoded as unpadded base64) and the canonical JSON representation of the
/// *m.key.verification.start* message.
pub commitment: String,
},
}
}

View File

@ -2,31 +2,32 @@
use std::fmt::{Display, Formatter, Result as FmtResult};
use ruma_events_macros::ruma_event;
use serde::{
de::{Error as SerdeError, Visitor},
Deserialize, Deserializer, Serialize, Serializer,
};
event! {
ruma_event! {
/// Cancels a key verification process/request.
///
/// Typically sent as a to-device event.
pub struct CancelEvent(CancelEventContent) {}
}
CancelEvent {
kind: Event,
event_type: KeyVerificationCancel,
content: {
/// The opaque identifier for the verification process/request.
pub transaction_id: String,
/// The payload of an *m.key.verification.cancel* event.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct CancelEventContent {
/// The opaque identifier for the verification process/request.
pub transaction_id: String,
/// A human readable description of the `code`.
///
/// The client should only rely on this string if it does not understand the `code`.
pub reason: String,
/// A human readable description of the `code`.
///
/// The client should only rely on this string if it does not understand the `code`.
pub reason: String,
/// The error code for why the process/request was cancelled by the user.
pub code: CancelCode,
/// The error code for why the process/request was cancelled by the user.
pub code: CancelCode,
},
}
}
/// An error code for why the process/request was cancelled by the user.

View File

@ -1,22 +1,22 @@
//! Types for the *m.key.verification.key* event.
use serde::{Deserialize, Serialize};
use ruma_events_macros::ruma_event;
event! {
ruma_event! {
/// Sends the ephemeral public key for a device to the partner device.
///
/// Typically sent as a to-device event.
pub struct KeyEvent(KeyEventContent) {}
}
KeyEvent {
kind: Event,
event_type: KeyVerificationKey,
content: {
/// An opaque identifier for the verification process.
///
/// Must be the same as the one used for the *m.key.verification.start* message.
pub transaction_id: String,
/// The payload of an *m.key.verification.key* event.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct KeyEventContent {
/// An opaque identifier for the verification process.
///
/// Must be the same as the one used for the *m.key.verification.start* message.
pub transaction_id: String,
/// The device's ephemeral public key, encoded as unpadded Base64.
pub key: String,
/// The device's ephemeral public key, encoded as unpadded Base64.
pub key: String,
},
}
}

View File

@ -1,29 +1,29 @@
//! Types for the *m.key.verification.mac* event.
use ruma_events_macros::ruma_event;
use ruma_signatures::SignatureSet;
use serde::{Deserialize, Serialize};
event! {
ruma_event! {
/// Sends the MAC of a device's key to the partner device.
///
/// Typically sent as a to-device event.
pub struct MacEvent(MacEventContent) {}
}
/// The payload for an *m.key.verification.mac* event.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MacEventContent {
/// An opaque identifier for the verification process.
///
/// Must be the same as the one used for the *m.key.verification.start* message.
pub transaction_id: String,
/// A map of the key ID to the MAC of the key, using the algorithm in the verification process.
///
/// The MAC is encoded as unpadded Base64.
pub mac: SignatureSet,
/// The MAC of the comma-separated, sorted, list of key IDs given in the `mac` property, encoded
/// as unpadded Base64.
pub keys: String,
MacEvent {
kind: Event,
event_type: KeyVerificationMac,
content: {
/// An opaque identifier for the verification process.
///
/// Must be the same as the one used for the *m.key.verification.start* message.
pub transaction_id: String,
/// A map of the key ID to the MAC of the key, using the algorithm in the verification process.
///
/// The MAC is encoded as unpadded Base64.
pub mac: SignatureSet,
/// The MAC of the comma-separated, sorted, list of key IDs given in the `mac` property, encoded
/// as unpadded Base64.
pub keys: String,
},
}
}

View File

@ -1,35 +1,35 @@
//! Types for the *m.key.verification.request* event.
use js_int::UInt;
use ruma_events_macros::ruma_event;
use ruma_identifiers::DeviceId;
use serde::{Deserialize, Serialize};
use super::VerificationMethod;
event! {
ruma_event! {
/// Requests a key verification with another user's devices.
///
/// Typically sent as a to-device event.
pub struct RequestEvent(RequestEventContent) {}
}
/// The payload of an *m.key.verification.request* event.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct RequestEventContent {
/// The device ID which is initiating the request.
pub from_device: DeviceId,
/// An opaque identifier for the verification request.
///
/// Must be unique with respect to the devices involved.
pub transaction_id: String,
/// The verification methods supported by the sender.
pub methods: Vec<VerificationMethod>,
/// The POSIX timestamp in milliseconds for when the request was made.
///
/// If the request is in the future by more than 5 minutes or more than 10 minutes in the past,
/// the message should be ignored by the receiver.
pub timestamp: UInt,
RequestEvent {
kind: Event,
event_type: KeyVerificationRequest,
content: {
/// The device ID which is initiating the request.
pub from_device: DeviceId,
/// An opaque identifier for the verification request.
///
/// Must be unique with respect to the devices involved.
pub transaction_id: String,
/// The verification methods supported by the sender.
pub methods: Vec<VerificationMethod>,
/// The POSIX timestamp in milliseconds for when the request was made.
///
/// If the request is in the future by more than 5 minutes or more than 10 minutes in
/// the past, the message should be ignored by the receiver.
pub timestamp: UInt,
},
}
}

View File

@ -113,6 +113,10 @@ use serde::{
};
use serde_json::Value;
pub use custom::CustomEvent;
pub use custom_room::CustomRoomEvent;
pub use custom_state::CustomStateEvent;
#[macro_use]
mod macros;
@ -127,12 +131,12 @@ pub mod dummy;
pub mod forwarded_room_key;
pub mod fully_read;
pub mod ignored_user_list;
// pub mod key;
pub mod key;
pub mod presence;
// pub mod push_rules;
pub mod receipt;
pub mod room;
// pub mod room_key;
pub mod room_key;
pub mod room_key_request;
pub mod sticker;
// pub mod stripped;
@ -223,7 +227,7 @@ pub struct Empty;
impl Serialize for Empty {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer
S: Serializer,
{
serializer.serialize_map(Some(0))?.end()
}
@ -232,11 +236,11 @@ impl Serialize for Empty {
impl<'de> Deserialize<'de> for Empty {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>
D: Deserializer<'de>,
{
struct EmptyMapVisitor;
impl <'de> Visitor<'de> for EmptyMapVisitor {
impl<'de> Visitor<'de> for EmptyMapVisitor {
type Value = Empty;
fn expecting(&self, f: &mut Formatter) -> FmtResult {
@ -245,7 +249,7 @@ impl<'de> Deserialize<'de> for Empty {
fn visit_map<A>(self, _map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>
A: MapAccess<'de>,
{
Ok(Empty)
}
@ -401,9 +405,6 @@ pub trait Event
where
Self: Debug + Serialize,
{
/// The type of the event.
const EVENT_TYPE: EventType;
/// The type of this event's `content` field.
type Content: Debug + Serialize;
@ -411,9 +412,7 @@ where
fn content(&self) -> &Self::Content;
/// The type of the event.
fn event_type(&self) -> EventType {
Self::EVENT_TYPE
}
fn event_type(&self) -> EventType;
}
/// An event within the context of a room.
@ -447,20 +446,56 @@ pub trait StateEvent: RoomEvent {
fn state_key(&self) -> &str;
}
// event! {
// /// A custom basic event not covered by the Matrix specification.
// pub struct CustomEvent(Value) {}
// }
mod custom {
use ruma_events_macros::ruma_event;
use serde_json::Value;
// room_event! {
// /// A custom room event not covered by the Matrix specification.
// pub struct CustomRoomEvent(Value) {}
// }
ruma_event! {
/// A custom basic event not covered by the Matrix specification.
CustomEvent {
kind: Event,
event_type: Custom,
content_type_alias: {
/// The payload for `CustomEvent`.
Value
},
}
}
}
// state_event! {
// /// A custom state event not covered by the Matrix specification.
// pub struct CustomStateEvent(Value) {}
// }
mod custom_room {
use ruma_events_macros::ruma_event;
use serde_json::Value;
ruma_event! {
/// A custom room event not covered by the Matrix specification.
CustomRoomEvent {
kind: RoomEvent,
event_type: Custom,
content_type_alias: {
/// The payload for `CustomRoomEvent`.
Value
},
}
}
}
mod custom_state {
use ruma_events_macros::ruma_event;
use serde_json::Value;
ruma_event! {
/// A custom state event not covered by the Matrix specification.
CustomStateEvent {
kind: StateEvent,
event_type: Custom,
content_type_alias: {
/// The payload for `CustomStateEvent`.
Value
},
}
}
}
impl Display for EventType {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {

View File

@ -1,31 +1,31 @@
//! Types for the *m.room_key* event.
use ruma_events_macros::ruma_event;
use ruma_identifiers::RoomId;
use serde::{Deserialize, Serialize};
use super::Algorithm;
event! {
ruma_event! {
/// This event type is used to exchange keys for end-to-end encryption.
///
/// Typically it is encrypted as an *m.room.encrypted* event, then sent as a to-device event.
pub struct RoomKeyEvent(RoomKeyEventContent) {}
}
/// The payload of an *m.room_key* event.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct RoomKeyEventContent {
/// The encryption algorithm the key in this event is to be used with.
///
/// Must be `m.megolm.v1.aes-sha2`.
pub algorithm: Algorithm,
/// The room where the key is used.
pub room_id: RoomId,
/// The ID of the session that the key is for.
pub session_id: String,
/// The key to be exchanged.
pub session_key: String,
RoomKeyEvent {
kind: Event,
event_type: RoomKey,
content: {
/// The encryption algorithm the key in this event is to be used with.
///
/// Must be `m.megolm.v1.aes-sha2`.
pub algorithm: Algorithm,
/// The room where the key is used.
pub room_id: RoomId,
/// The ID of the session that the key is for.
pub session_id: String,
/// The key to be exchanged.
pub session_key: String,
}
}
}