bitwarden_vault/
collection_client.rs1use bitwarden_core::Client;
2
3use crate::{error::DecryptError, Collection, CollectionView, VaultClient};
4
5pub struct CollectionsClient {
6 pub(crate) client: Client,
7}
8
9impl CollectionsClient {
10 pub fn decrypt(&self, collection: Collection) -> Result<CollectionView, DecryptError> {
11 let key_store = self.client.internal.get_key_store();
12 let view = key_store.decrypt(&collection)?;
13 Ok(view)
14 }
15
16 pub fn decrypt_list(
17 &self,
18 collections: Vec<Collection>,
19 ) -> Result<Vec<CollectionView>, DecryptError> {
20 let key_store = self.client.internal.get_key_store();
21 let views = key_store.decrypt_list(&collections)?;
22 Ok(views)
23 }
24}
25
26impl VaultClient {
27 pub fn collections(&self) -> CollectionsClient {
28 CollectionsClient {
29 client: self.client.clone(),
30 }
31 }
32}
33
34#[cfg(test)]
35mod tests {
36 use bitwarden_core::client::test_accounts::test_bitwarden_com_account;
37
38 use super::*;
39 use crate::VaultClientExt;
40
41 #[tokio::test]
42 async fn test_decrypt_list() {
43 let client = Client::init_test_account(test_bitwarden_com_account()).await;
44
45 let dec = client.vault().collections().decrypt_list(vec![Collection {
46 id: Some("66c5ca57-0868-4c7e-902f-b181009709c0".parse().unwrap()),
47 organization_id: "1bc9ac1e-f5aa-45f2-94bf-b181009709b8".parse().unwrap(),
48 name: "2.EI9Km5BfrIqBa1W+WCccfA==|laWxNnx+9H3MZww4zm7cBSLisjpi81zreaQntRhegVI=|x42+qKFf5ga6DIL0OW5pxCdLrC/gm8CXJvf3UASGteI=".parse().unwrap(),
49 external_id: None,
50 hide_passwords: false,
51 read_only: false,
52 manage: false,
53 }]).unwrap();
54
55 assert_eq!(dec[0].name, "Default collection");
56 }
57
58 #[tokio::test]
59 async fn test_decrypt() {
60 let client = Client::init_test_account(test_bitwarden_com_account()).await;
61
62 let dec = client.vault().collections().decrypt(Collection {
63 id: Some("66c5ca57-0868-4c7e-902f-b181009709c0".parse().unwrap()),
64 organization_id: "1bc9ac1e-f5aa-45f2-94bf-b181009709b8".parse().unwrap(),
65 name: "2.EI9Km5BfrIqBa1W+WCccfA==|laWxNnx+9H3MZww4zm7cBSLisjpi81zreaQntRhegVI=|x42+qKFf5ga6DIL0OW5pxCdLrC/gm8CXJvf3UASGteI=".parse().unwrap(),
66 external_id: None,
67 hide_passwords: false,
68 read_only: false,
69 manage: false,
70 }).unwrap();
71
72 assert_eq!(dec.name, "Default collection");
73 }
74}