Skip to main content

lb_rs/model/
meta.rs

1use libsecp256k1::PublicKey;
2use serde::{Deserialize, Serialize};
3use std::mem;
4use uuid::Uuid;
5
6use crate::LbResult;
7use crate::service::keychain::Keychain;
8
9use super::access_info::{EncryptedKey, UserAccessInfo, UserAccessMode};
10use super::account::Account;
11use super::clock::get_time;
12use super::crypto::AESKey;
13use super::entropy::Entropy;
14use super::file_like::FileLike;
15use super::file_metadata::{DocumentHmac, FileType, Owner};
16use super::secret_filename::SecretFileName;
17use super::signed_meta::SignedMeta;
18use super::{pubkey, symkey};
19
20#[derive(Serialize, Deserialize, Clone, Debug)]
21pub enum Meta {
22    V1 {
23        id: Uuid,
24        file_type: FileType,
25        parent: Uuid,
26        name: SecretFileName,
27        owner: Owner,
28        is_deleted: bool,
29        doc_size: Option<usize>,
30        doc_hmac: Option<DocumentHmac>,
31        user_access_keys: Vec<UserAccessInfo>,
32        folder_access_key: EncryptedKey,
33    },
34    /// Adds optional attachment info that marks this file as an attachment of another. A single
35    /// attachment folder per note is created when the first image is added or the note is
36    /// write-shared for the first time. This folder is moved, deleted, and shared alongside the
37    /// target note. Links resolve into it under relaxed conditions. This folder is hidden from
38    /// files, recents, shared with me, and search, but shown by `lockbook list` and files
39    /// integrations.
40    V2 {
41        id: Uuid,
42        file_type: FileType,
43        parent: Uuid,
44        name: SecretFileName,
45        owner: Owner,
46        is_deleted: bool,
47        doc_size: Option<usize>,
48        doc_hmac: Option<DocumentHmac>,
49        user_access_keys: Vec<UserAccessInfo>,
50        folder_access_key: EncryptedKey,
51        attachment_of: Option<AttachmentAccessInfo>,
52    },
53}
54
55/// The information required for this folder to follow the target note's lifecycle. The server
56/// considers it part of the same tree as the target, so it hands it out to the same users from
57/// `/get-updates`, and its key is encrypted with that of the target so it is readable to anyone
58/// who can read the target.
59#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
60pub struct AttachmentAccessInfo {
61    pub id: Uuid,
62    pub key: EncryptedKey,
63}
64
65impl Meta {
66    pub fn create_root(account: &Account, entropy: &Entropy) -> LbResult<Self> {
67        let id = entropy.id();
68        let key = entropy.key();
69        let pub_key = account.public_key();
70
71        Ok(Meta::V1 {
72            id,
73            file_type: FileType::Folder,
74            parent: id,
75            name: SecretFileName::from_str(&account.username, &key, &key)?,
76            owner: Owner(pub_key),
77            is_deleted: false,
78            doc_hmac: None,
79            doc_size: None,
80            user_access_keys: vec![UserAccessInfo::encrypt(
81                account,
82                &pub_key,
83                &pub_key,
84                &key,
85                UserAccessMode::Write,
86            )?],
87            folder_access_key: symkey::encrypt(&key, &key)?,
88        })
89    }
90
91    #[allow(clippy::too_many_arguments)]
92    pub fn create(
93        id: Uuid, key: AESKey, owner: &PublicKey, parent: Uuid, parent_key: &AESKey, name: &str,
94        file_type: FileType, attachment_of: Option<AttachmentAccessInfo>,
95    ) -> LbResult<Self> {
96        let name = SecretFileName::from_str(name, &key, parent_key)?;
97        let owner = Owner(*owner);
98        let folder_access_key = symkey::encrypt(parent_key, &key)?;
99        Ok(match attachment_of {
100            None => Meta::V1 {
101                id,
102                file_type,
103                parent,
104                name,
105                owner,
106                is_deleted: false,
107                doc_hmac: None,
108                doc_size: None,
109                user_access_keys: Default::default(),
110                folder_access_key,
111            },
112            Some(_) => Meta::V2 {
113                id,
114                file_type,
115                parent,
116                name,
117                owner,
118                is_deleted: false,
119                doc_hmac: None,
120                doc_size: None,
121                user_access_keys: Default::default(),
122                folder_access_key,
123                attachment_of,
124            },
125        })
126    }
127
128    pub fn set_parent(&mut self, new_parent: Uuid) {
129        match self {
130            Meta::V1 { parent, .. } | Meta::V2 { parent, .. } => *parent = new_parent,
131        }
132    }
133
134    pub fn set_name(&mut self, new_name: SecretFileName) {
135        match self {
136            Meta::V1 { name, .. } | Meta::V2 { name, .. } => *name = new_name,
137        }
138    }
139
140    pub fn set_owner(&mut self, new_owner: Owner) {
141        match self {
142            Meta::V1 { owner, .. } | Meta::V2 { owner, .. } => *owner = new_owner,
143        }
144    }
145
146    pub fn set_deleted(&mut self, new_is_deleted: bool) {
147        match self {
148            Meta::V1 { is_deleted, .. } | Meta::V2 { is_deleted, .. } => {
149                *is_deleted = new_is_deleted
150            }
151        }
152    }
153
154    pub fn set_folder_access_keys(&mut self, new_keys: EncryptedKey) {
155        match self {
156            Meta::V1 { folder_access_key, .. } | Meta::V2 { folder_access_key, .. } => {
157                *folder_access_key = new_keys
158            }
159        }
160    }
161
162    pub fn user_access_keys_mut(&mut self) -> &mut Vec<UserAccessInfo> {
163        match self {
164            Meta::V1 { user_access_keys, .. } | Meta::V2 { user_access_keys, .. } => {
165                user_access_keys
166            }
167        }
168    }
169
170    pub fn set_hmac_and_size(&mut self, new_hmac: Option<DocumentHmac>, new_size: Option<usize>) {
171        match self {
172            Meta::V1 { doc_hmac, doc_size, .. } | Meta::V2 { doc_hmac, doc_size, .. } => {
173                *doc_hmac = new_hmac;
174                *doc_size = new_size;
175            }
176        }
177    }
178
179    pub fn set_type(&mut self, new_type: FileType) {
180        match self {
181            Meta::V1 { file_type, .. } | Meta::V2 { file_type, .. } => *file_type = new_type,
182        }
183    }
184
185    pub fn set_id(&mut self, new_id: Uuid) {
186        match self {
187            Meta::V1 { id, .. } | Meta::V2 { id, .. } => *id = new_id,
188        }
189    }
190
191    // todo move to FileLike when possible
192    pub fn doc_size(&self) -> &Option<usize> {
193        match self {
194            Meta::V1 { doc_size, .. } | Meta::V2 { doc_size, .. } => doc_size,
195        }
196    }
197
198    pub fn sign(self, keychain: &Keychain) -> LbResult<SignedMeta> {
199        pubkey::sign(&keychain.get_account()?.private_key, &keychain.get_pk()?, self, get_time)
200    }
201
202    pub fn sign_with(self, account: &Account) -> LbResult<SignedMeta> {
203        pubkey::sign(&account.private_key, &account.public_key(), self, get_time)
204    }
205}
206
207// This is impl'd to avoid comparing encrypted values
208impl PartialEq for Meta {
209    fn eq(&self, other: &Self) -> bool {
210        // todo: verify that ignoring folder_access_key is intentional
211        mem::discriminant(self) == mem::discriminant(other)
212            && self.id() == other.id()
213            && self.file_type() == other.file_type()
214            && self.parent() == other.parent()
215            && self.secret_name() == other.secret_name()
216            && self.owner() == other.owner()
217            && self.explicitly_deleted() == other.explicitly_deleted()
218            && self.document_hmac() == other.document_hmac()
219            && self.user_access_keys() == other.user_access_keys()
220            && self.doc_size() == other.doc_size()
221            && self.attachment_of() == other.attachment_of()
222    }
223}