1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use std::collections::BTreeMap;
use ruma::{events::secret::request::SecretName, OwnedTransactionId};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use zeroize::Zeroize;
use super::{EventType, ToDeviceEvent};
pub type SecretSendEvent = ToDeviceEvent<SecretSendContent>;
#[derive(Serialize, Deserialize)]
pub struct SecretSendContent {
pub request_id: OwnedTransactionId,
pub secret: String,
#[serde(rename = "name", skip_serializing_if = "Option::is_none")]
pub secret_name: Option<SecretName>,
#[serde(flatten)]
other: BTreeMap<String, Value>,
}
impl SecretSendContent {
pub fn new(request_id: OwnedTransactionId, secret: String) -> Self {
Self { request_id, secret, secret_name: None, other: Default::default() }
}
}
impl Zeroize for SecretSendContent {
fn zeroize(&mut self) {
self.secret.zeroize();
}
}
impl Drop for SecretSendContent {
fn drop(&mut self) {
self.zeroize()
}
}
impl std::fmt::Debug for SecretSendContent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SecretSendContent")
.field("request_id", &self.request_id)
.field("secret_name", &self.secret_name)
.finish_non_exhaustive()
}
}
impl EventType for SecretSendContent {
const EVENT_TYPE: &'static str = "m.secret.send";
}
#[cfg(test)]
pub(crate) mod test {
use serde_json::{json, Value};
use super::SecretSendEvent;
pub(crate) fn json() -> Value {
json!({
"sender": "@alice:example.org",
"content": {
"request_id": "randomly_generated_id_9573",
"secret": "ThisIsASecretDon'tTellAnyone"
},
"type": "m.secret.send",
})
}
#[test]
fn deserialization() -> Result<(), serde_json::Error> {
let json = json();
let event: SecretSendEvent = serde_json::from_value(json.clone())?;
assert_eq!(&event.content.secret, "ThisIsASecretDon'tTellAnyone");
let serialized = serde_json::to_value(event)?;
assert_eq!(json, serialized);
Ok(())
}
}