bitwarden_vault/cipher/
secure_note.rs

1use bitwarden_api_api::models::CipherSecureNoteModel;
2use bitwarden_core::{
3    key_management::{KeyIds, SymmetricKeyId},
4    require,
5};
6use bitwarden_crypto::{CompositeEncryptable, CryptoError, Decryptable, KeyStoreContext};
7use serde::{Deserialize, Deserializer, Serialize};
8use serde_repr::Serialize_repr;
9#[cfg(feature = "wasm")]
10use tsify::Tsify;
11#[cfg(feature = "wasm")]
12use wasm_bindgen::prelude::wasm_bindgen;
13
14use crate::{
15    Cipher, VaultParseError,
16    cipher::cipher::{CipherKind, CopyableCipherFields},
17};
18
19#[allow(missing_docs)]
20#[derive(Clone, Copy, Serialize_repr, Debug)]
21#[repr(u8)]
22#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
23#[cfg_attr(feature = "wasm", wasm_bindgen)]
24pub enum SecureNoteType {
25    Generic = 0,
26}
27
28#[derive(Clone, Serialize, Deserialize, Debug)]
29#[serde(rename_all = "camelCase")]
30#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
31#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
32pub struct SecureNote {
33    r#type: SecureNoteType,
34}
35
36/// Use the custom deserializer for SecureNoteType
37/// Older notes may have a Type greater than 0. If so set them to Generic
38impl<'de> Deserialize<'de> for SecureNoteType {
39    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
40    where
41        D: Deserializer<'de>,
42    {
43        match u8::deserialize(deserializer) {
44            Ok(0) => Ok(SecureNoteType::Generic),
45            Ok(_) => {
46                // Any unknown type (like type 1) defaults to Generic
47                Ok(SecureNoteType::Generic)
48            }
49            Err(_) => Ok(SecureNoteType::Generic),
50        }
51    }
52}
53
54#[allow(missing_docs)]
55#[derive(Clone, Serialize, Deserialize, Debug)]
56#[serde(rename_all = "camelCase", deny_unknown_fields)]
57#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
58#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
59pub struct SecureNoteView {
60    pub r#type: SecureNoteType,
61}
62
63impl CompositeEncryptable<KeyIds, SymmetricKeyId, SecureNote> for SecureNoteView {
64    fn encrypt_composite(
65        &self,
66        _ctx: &mut KeyStoreContext<KeyIds>,
67        _key: SymmetricKeyId,
68    ) -> Result<SecureNote, CryptoError> {
69        Ok(SecureNote {
70            r#type: self.r#type,
71        })
72    }
73}
74
75impl Decryptable<KeyIds, SymmetricKeyId, SecureNoteView> for SecureNote {
76    fn decrypt(
77        &self,
78        _ctx: &mut KeyStoreContext<KeyIds>,
79        _key: SymmetricKeyId,
80    ) -> Result<SecureNoteView, CryptoError> {
81        Ok(SecureNoteView {
82            r#type: self.r#type,
83        })
84    }
85}
86
87impl TryFrom<CipherSecureNoteModel> for SecureNote {
88    type Error = VaultParseError;
89
90    fn try_from(model: CipherSecureNoteModel) -> Result<Self, Self::Error> {
91        Ok(Self {
92            r#type: require!(model.r#type).into(),
93        })
94    }
95}
96
97impl From<bitwarden_api_api::models::SecureNoteType> for SecureNoteType {
98    fn from(model: bitwarden_api_api::models::SecureNoteType) -> Self {
99        match model {
100            bitwarden_api_api::models::SecureNoteType::Generic => SecureNoteType::Generic,
101        }
102    }
103}
104
105impl From<SecureNoteType> for bitwarden_api_api::models::SecureNoteType {
106    fn from(model: SecureNoteType) -> Self {
107        match model {
108            SecureNoteType::Generic => bitwarden_api_api::models::SecureNoteType::Generic,
109        }
110    }
111}
112
113impl From<SecureNote> for CipherSecureNoteModel {
114    fn from(model: SecureNote) -> Self {
115        Self {
116            r#type: Some(model.r#type.into()),
117        }
118    }
119}
120
121impl CipherKind for SecureNote {
122    fn get_copyable_fields(&self, cipher: Option<&Cipher>) -> Vec<CopyableCipherFields> {
123        [cipher
124            .and_then(|c| c.notes.as_ref())
125            .map(|_| CopyableCipherFields::SecureNotes)]
126        .into_iter()
127        .flatten()
128        .collect()
129    }
130
131    fn decrypt_subtitle(
132        &self,
133        _ctx: &mut KeyStoreContext<KeyIds>,
134        _key: SymmetricKeyId,
135    ) -> Result<String, CryptoError> {
136        Ok(String::new())
137    }
138}
139
140#[cfg(test)]
141mod tests {
142    use crate::{
143        CipherRepromptType, CipherType, SecureNoteType,
144        cipher::cipher::{Cipher, CipherKind, CopyableCipherFields},
145        secure_note::SecureNote,
146    };
147
148    fn create_cipher_for_note(note: SecureNote) -> Cipher {
149        Cipher {
150            id: Some("090c19ea-a61a-4df6-8963-262b97bc6266".parse().unwrap()),
151            organization_id: None,
152            folder_id: None,
153            collection_ids: vec![],
154            r#type: CipherType::Login,
155            key: None,
156            name: "2.iovOJUb186UXu+0AlQggjw==|LeWZhrT0B7rqFtDufOJMlJsftwmMGuaoBxf/Cig4D4A9XHhUqacd8uOYP7M5bd/k|++gmrHIyt8hvvPP9dwFS/CGd+POfzmeXzKOsuyJpDDc=".parse().unwrap(),
157            notes: None,
158            login: None,
159            identity: None,
160            card: None,
161            secure_note: Some(note),
162            ssh_key: None,
163            favorite: false,
164            reprompt: CipherRepromptType::None,
165            organization_use_totp: false,
166            edit: true,
167            permissions: None,
168            view_password: true,
169            local_data: None,
170            attachments: None,
171            fields: None,
172            password_history: None,
173            creation_date: "2024-01-01T00:00:00.000Z".parse().unwrap(),
174            deleted_date: None,
175            revision_date: "2024-01-01T00:00:00.000Z".parse().unwrap(),
176            archived_date: None,
177            data: None,
178        }
179    }
180
181    #[test]
182    fn test_get_copyable_fields_secure_note_empty() {
183        let secure_note = SecureNote {
184            r#type: SecureNoteType::Generic,
185        };
186
187        let cipher = create_cipher_for_note(secure_note.clone());
188
189        let copyable_fields = secure_note.get_copyable_fields(Some(&cipher));
190        assert_eq!(copyable_fields, vec![]);
191    }
192
193    #[test]
194    fn test_get_copyable_fields_secure_note_has_notes() {
195        let secure_note = SecureNote {
196            r#type: SecureNoteType::Generic,
197        };
198
199        let mut cipher = create_cipher_for_note(secure_note.clone());
200        cipher.notes = Some("2.iovOJUb186UXu+0AlQggjw==|LeWZhrT0B7rqFtDufOJMlJsftwmMGuaoBxf/Cig4D4A9XHhUqacd8uOYP7M5bd/k|++gmrHIyt8hvvPP9dwFS/CGd+POfzmeXzKOsuyJpDDc=".parse().unwrap());
201
202        let copyable_fields = secure_note.get_copyable_fields(Some(&cipher));
203        assert_eq!(copyable_fields, vec![CopyableCipherFields::SecureNotes]);
204    }
205
206    #[test]
207    fn test_get_copyable_fields_secure_no_cipher() {
208        let secure_note = SecureNote {
209            r#type: SecureNoteType::Generic,
210        };
211
212        let copyable_fields = secure_note.get_copyable_fields(None);
213        assert_eq!(copyable_fields, vec![]);
214    }
215}