matrix_sdk_crypto/types/events/
secret_send.rs

1// Copyright 2022 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Types for `m.secret.send` to-device events.
16
17use std::collections::BTreeMap;
18
19use ruma::{events::secret::request::SecretName, OwnedTransactionId};
20use serde::{Deserialize, Serialize};
21use serde_json::Value;
22use zeroize::Zeroize;
23
24use super::{EventType, ToDeviceEvent};
25
26/// The `m.secret.send` to-device event.
27pub type SecretSendEvent = ToDeviceEvent<SecretSendContent>;
28
29/// The `m.secret.send` event content.
30///
31/// Sent by a client to share a secret with another device, in response to an
32/// `m.secret.request` event. It must be encrypted as an `m.room.encrypted`
33/// event, then sent as a to-device event.
34#[derive(Clone, Serialize, Deserialize)]
35pub struct SecretSendContent {
36    /// The ID of the request that this a response to.
37    pub request_id: OwnedTransactionId,
38    /// The contents of the secret.
39    pub secret: String,
40    /// The name of the secret, typically not part of the event but can be
41    /// inserted when processing `m.secret.send` events so other event consumers
42    /// know which secret this event contains.
43    #[serde(rename = "name", skip_serializing_if = "Option::is_none")]
44    pub secret_name: Option<SecretName>,
45    /// Any other, custom and non-specced fields of the content.
46    #[serde(flatten)]
47    other: BTreeMap<String, Value>,
48}
49
50impl SecretSendContent {
51    /// Create a new `m.secret.send` content.
52    pub fn new(request_id: OwnedTransactionId, secret: String) -> Self {
53        Self { request_id, secret, secret_name: None, other: Default::default() }
54    }
55}
56
57impl Zeroize for SecretSendContent {
58    fn zeroize(&mut self) {
59        self.secret.zeroize();
60    }
61}
62
63impl Drop for SecretSendContent {
64    fn drop(&mut self) {
65        self.zeroize()
66    }
67}
68
69#[cfg(not(tarpaulin_include))]
70impl std::fmt::Debug for SecretSendContent {
71    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72        f.debug_struct("SecretSendContent")
73            .field("request_id", &self.request_id)
74            .finish_non_exhaustive()
75    }
76}
77
78impl EventType for SecretSendContent {
79    const EVENT_TYPE: &'static str = "m.secret.send";
80}
81
82#[cfg(test)]
83pub(crate) mod tests {
84    use serde_json::{json, Value};
85
86    use super::SecretSendEvent;
87
88    pub(crate) fn json() -> Value {
89        json!({
90            "sender": "@alice:example.org",
91            "content": {
92                "request_id": "randomly_generated_id_9573",
93                "secret": "ThisIsASecretDon'tTellAnyone"
94            },
95            "type": "m.secret.send",
96        })
97    }
98
99    #[test]
100    fn deserialization() -> Result<(), serde_json::Error> {
101        let json = json();
102        let event: SecretSendEvent = serde_json::from_value(json.clone())?;
103
104        assert_eq!(&event.content.secret, "ThisIsASecretDon'tTellAnyone");
105
106        let serialized = serde_json::to_value(event)?;
107        assert_eq!(json, serialized);
108
109        Ok(())
110    }
111}