1use std::collections::BTreeMap;
6
7use ruma_common::{OwnedTransactionId, serde::Base64};
8use ruma_macros::EventContent;
9use serde::{Deserialize, Serialize};
10use serde_json::Value as JsonValue;
11
12use super::{
13 HashAlgorithm, KeyAgreementProtocol, MessageAuthenticationCode, ShortAuthenticationString,
14};
15use crate::relation::Reference;
16
17#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
21#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
22#[ruma_event(type = "m.key.verification.accept", kind = ToDevice)]
23pub struct ToDeviceKeyVerificationAcceptEventContent {
24 pub transaction_id: OwnedTransactionId,
28
29 #[serde(flatten)]
31 pub method: AcceptMethod,
32}
33
34impl ToDeviceKeyVerificationAcceptEventContent {
35 pub fn new(transaction_id: OwnedTransactionId, method: AcceptMethod) -> Self {
38 Self { transaction_id, method }
39 }
40}
41
42#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
46#[ruma_event(type = "m.key.verification.accept", kind = MessageLike)]
47#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
48pub struct KeyVerificationAcceptEventContent {
49 #[serde(flatten)]
51 pub method: AcceptMethod,
52
53 #[serde(rename = "m.relates_to")]
55 pub relates_to: Reference,
56}
57
58impl KeyVerificationAcceptEventContent {
59 pub fn new(method: AcceptMethod, relates_to: Reference) -> Self {
62 Self { method, relates_to }
63 }
64}
65
66#[derive(Clone, Debug, Deserialize, Serialize)]
68#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
69#[serde(untagged)]
70pub enum AcceptMethod {
71 SasV1(SasV1Content),
73
74 #[doc(hidden)]
76 _Custom(_CustomContent),
77}
78
79#[doc(hidden)]
81#[derive(Clone, Debug, Deserialize, Serialize)]
82#[allow(clippy::exhaustive_structs)]
83pub struct _CustomContent {
84 pub method: String,
86
87 #[serde(flatten)]
89 pub data: BTreeMap<String, JsonValue>,
90}
91
92#[derive(Clone, Debug, Deserialize, Serialize)]
94#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
95#[serde(rename = "m.sas.v1", tag = "method")]
96pub struct SasV1Content {
97 pub key_agreement_protocol: KeyAgreementProtocol,
100
101 pub hash: HashAlgorithm,
104
105 pub message_authentication_code: MessageAuthenticationCode,
108
109 pub short_authentication_string: Vec<ShortAuthenticationString>,
115
116 pub commitment: Base64,
120}
121
122#[derive(Debug)]
124#[allow(clippy::exhaustive_structs)]
125pub struct SasV1ContentInit {
126 pub key_agreement_protocol: KeyAgreementProtocol,
129
130 pub hash: HashAlgorithm,
133
134 pub message_authentication_code: MessageAuthenticationCode,
136
137 pub short_authentication_string: Vec<ShortAuthenticationString>,
143
144 pub commitment: Base64,
148}
149
150impl From<SasV1ContentInit> for SasV1Content {
151 fn from(init: SasV1ContentInit) -> Self {
153 SasV1Content {
154 hash: init.hash,
155 key_agreement_protocol: init.key_agreement_protocol,
156 message_authentication_code: init.message_authentication_code,
157 short_authentication_string: init.short_authentication_string,
158 commitment: init.commitment,
159 }
160 }
161}
162
163#[cfg(test)]
164mod tests {
165 use std::collections::BTreeMap;
166
167 use assert_matches2::assert_matches;
168 use ruma_common::{
169 canonical_json::assert_to_canonical_json_eq,
170 event_id,
171 serde::{Base64, Raw},
172 };
173 use serde_json::{Value as JsonValue, from_value as from_json_value, json};
174
175 use super::{
176 _CustomContent, AcceptMethod, HashAlgorithm, KeyAgreementProtocol,
177 KeyVerificationAcceptEventContent, MessageAuthenticationCode, SasV1Content,
178 ShortAuthenticationString, ToDeviceKeyVerificationAcceptEventContent,
179 };
180 use crate::{ToDeviceEvent, relation::Reference};
181
182 #[test]
183 fn serialization() {
184 let key_verification_accept_content = ToDeviceKeyVerificationAcceptEventContent {
185 transaction_id: "456".into(),
186 method: AcceptMethod::SasV1(SasV1Content {
187 hash: HashAlgorithm::Sha256,
188 key_agreement_protocol: KeyAgreementProtocol::Curve25519,
189 message_authentication_code: MessageAuthenticationCode::HkdfHmacSha256V2,
190 short_authentication_string: vec![ShortAuthenticationString::Decimal],
191 commitment: Base64::new(b"hello".to_vec()),
192 }),
193 };
194
195 assert_to_canonical_json_eq!(
196 key_verification_accept_content,
197 json!({
198 "transaction_id": "456",
199 "method": "m.sas.v1",
200 "commitment": "aGVsbG8",
201 "key_agreement_protocol": "curve25519",
202 "hash": "sha256",
203 "message_authentication_code": "hkdf-hmac-sha256.v2",
204 "short_authentication_string": ["decimal"],
205 }),
206 );
207
208 let key_verification_accept_content = ToDeviceKeyVerificationAcceptEventContent {
209 transaction_id: "456".into(),
210 method: AcceptMethod::_Custom(_CustomContent {
211 method: "m.sas.custom".to_owned(),
212 data: vec![("test".to_owned(), JsonValue::from("field"))]
213 .into_iter()
214 .collect::<BTreeMap<String, JsonValue>>(),
215 }),
216 };
217
218 assert_to_canonical_json_eq!(
219 key_verification_accept_content,
220 json!({
221 "transaction_id": "456",
222 "method": "m.sas.custom",
223 "test": "field",
224 }),
225 );
226 }
227
228 #[test]
229 fn in_room_serialization() {
230 let event_id = event_id!("$1598361704261elfgc:localhost");
231
232 let key_verification_accept_content = KeyVerificationAcceptEventContent {
233 relates_to: Reference { event_id: event_id.to_owned() },
234 method: AcceptMethod::SasV1(SasV1Content {
235 hash: HashAlgorithm::Sha256,
236 key_agreement_protocol: KeyAgreementProtocol::Curve25519,
237 message_authentication_code: MessageAuthenticationCode::HkdfHmacSha256V2,
238 short_authentication_string: vec![ShortAuthenticationString::Decimal],
239 commitment: Base64::new(b"hello".to_vec()),
240 }),
241 };
242
243 assert_to_canonical_json_eq!(
244 key_verification_accept_content,
245 json!({
246 "method": "m.sas.v1",
247 "commitment": "aGVsbG8",
248 "key_agreement_protocol": "curve25519",
249 "hash": "sha256",
250 "message_authentication_code": "hkdf-hmac-sha256.v2",
251 "short_authentication_string": ["decimal"],
252 "m.relates_to": {
253 "rel_type": "m.reference",
254 "event_id": event_id,
255 },
256 }),
257 );
258 }
259
260 #[test]
261 fn deserialization() {
262 let json = json!({
263 "transaction_id": "456",
264 "commitment": "aGVsbG8",
265 "method": "m.sas.v1",
266 "hash": "sha256",
267 "key_agreement_protocol": "curve25519",
268 "message_authentication_code": "hkdf-hmac-sha256.v2",
269 "short_authentication_string": ["decimal"]
270 });
271
272 let content = from_json_value::<ToDeviceKeyVerificationAcceptEventContent>(json).unwrap();
274 assert_eq!(content.transaction_id, "456");
275
276 assert_matches!(content.method, AcceptMethod::SasV1(sas));
277 assert_eq!(sas.commitment.encode(), "aGVsbG8");
278 assert_eq!(sas.hash, HashAlgorithm::Sha256);
279 assert_eq!(sas.key_agreement_protocol, KeyAgreementProtocol::Curve25519);
280 assert_eq!(sas.message_authentication_code, MessageAuthenticationCode::HkdfHmacSha256V2);
281 assert_eq!(sas.short_authentication_string, vec![ShortAuthenticationString::Decimal]);
282
283 let json = json!({
284 "content": {
285 "commitment": "aGVsbG8",
286 "transaction_id": "456",
287 "method": "m.sas.v1",
288 "key_agreement_protocol": "curve25519",
289 "hash": "sha256",
290 "message_authentication_code": "hkdf-hmac-sha256.v2",
291 "short_authentication_string": ["decimal"]
292 },
293 "type": "m.key.verification.accept",
294 "sender": "@example:localhost",
295 });
296
297 let ev = from_json_value::<ToDeviceEvent<ToDeviceKeyVerificationAcceptEventContent>>(json)
298 .unwrap();
299 assert_eq!(ev.content.transaction_id, "456");
300 assert_eq!(ev.sender, "@example:localhost");
301
302 assert_matches!(ev.content.method, AcceptMethod::SasV1(sas));
303 assert_eq!(sas.commitment.encode(), "aGVsbG8");
304 assert_eq!(sas.hash, HashAlgorithm::Sha256);
305 assert_eq!(sas.key_agreement_protocol, KeyAgreementProtocol::Curve25519);
306 assert_eq!(sas.message_authentication_code, MessageAuthenticationCode::HkdfHmacSha256V2);
307 assert_eq!(sas.short_authentication_string, vec![ShortAuthenticationString::Decimal]);
308
309 let json = json!({
310 "content": {
311 "from_device": "123",
312 "transaction_id": "456",
313 "method": "m.sas.custom",
314 "test": "field",
315 },
316 "type": "m.key.verification.accept",
317 "sender": "@example:localhost",
318 });
319
320 let ev = from_json_value::<ToDeviceEvent<ToDeviceKeyVerificationAcceptEventContent>>(json)
321 .unwrap();
322 assert_eq!(ev.content.transaction_id, "456");
323 assert_eq!(ev.sender, "@example:localhost");
324
325 assert_matches!(ev.content.method, AcceptMethod::_Custom(custom));
326 assert_eq!(custom.method, "m.sas.custom");
327 assert_eq!(custom.data.get("test"), Some(&JsonValue::from("field")));
328 }
329
330 #[test]
331 fn in_room_deserialization() {
332 let json = json!({
333 "commitment": "aGVsbG8",
334 "method": "m.sas.v1",
335 "hash": "sha256",
336 "key_agreement_protocol": "curve25519",
337 "message_authentication_code": "hkdf-hmac-sha256.v2",
338 "short_authentication_string": ["decimal"],
339 "m.relates_to": {
340 "rel_type": "m.reference",
341 "event_id": "$1598361704261elfgc:localhost",
342 }
343 });
344
345 let content = from_json_value::<KeyVerificationAcceptEventContent>(json).unwrap();
347 assert_eq!(content.relates_to.event_id, "$1598361704261elfgc:localhost");
348
349 assert_matches!(content.method, AcceptMethod::SasV1(sas));
350 assert_eq!(sas.commitment.encode(), "aGVsbG8");
351 assert_eq!(sas.hash, HashAlgorithm::Sha256);
352 assert_eq!(sas.key_agreement_protocol, KeyAgreementProtocol::Curve25519);
353 assert_eq!(sas.message_authentication_code, MessageAuthenticationCode::HkdfHmacSha256V2);
354 assert_eq!(sas.short_authentication_string, vec![ShortAuthenticationString::Decimal]);
355 }
356
357 #[test]
358 fn in_room_serialization_roundtrip() {
359 let event_id = event_id!("$1598361704261elfgc:localhost");
360
361 let content = KeyVerificationAcceptEventContent {
362 relates_to: Reference { event_id: event_id.to_owned() },
363 method: AcceptMethod::SasV1(SasV1Content {
364 hash: HashAlgorithm::Sha256,
365 key_agreement_protocol: KeyAgreementProtocol::Curve25519,
366 message_authentication_code: MessageAuthenticationCode::HkdfHmacSha256V2,
367 short_authentication_string: vec![ShortAuthenticationString::Decimal],
368 commitment: Base64::new(b"hello".to_vec()),
369 }),
370 };
371
372 let json_content = Raw::new(&content).unwrap();
373 let deser_content = json_content.deserialize().unwrap();
374
375 assert_matches!(deser_content.method, AcceptMethod::SasV1(_));
376 assert_eq!(deser_content.relates_to.event_id, event_id);
377 }
378}