Skip to main content

bitwarden_uniffi/vault/
ciphers.rs

1use bitwarden_collections::collection::CollectionId;
2use bitwarden_core::OrganizationId;
3use bitwarden_vault::{
4    Cipher, CipherCreateRequest, CipherEditRequest, CipherId, CipherListView,
5    CipherPartialEditRequest, CipherView, DecryptCipherListResult, DecryptCipherResult,
6    EncryptionContext, FolderId,
7};
8
9use crate::Result;
10
11#[allow(missing_docs)]
12#[derive(uniffi::Object)]
13pub struct CiphersClient(pub(crate) bitwarden_vault::CiphersClient);
14
15#[uniffi::export(async_runtime = "tokio")]
16impl CiphersClient {
17    /// Encrypt cipher
18    pub async fn encrypt(&self, cipher_view: CipherView) -> Result<EncryptionContext> {
19        Ok(self.0.encrypt(cipher_view).await?)
20    }
21
22    /// Decrypt cipher
23    pub async fn decrypt(&self, cipher: Cipher) -> Result<CipherView> {
24        Ok(self.0.decrypt(cipher).await?)
25    }
26
27    /// Decrypt cipher list
28    pub async fn decrypt_list(&self, ciphers: Vec<Cipher>) -> Result<Vec<CipherListView>> {
29        Ok(self.0.decrypt_list(ciphers.into()).await?)
30    }
31
32    /// Encrypt a list of cipher views. Fails if any cipher fails to encrypt.
33    pub async fn encrypt_list(
34        &self,
35        cipher_views: Vec<CipherView>,
36    ) -> Result<Vec<EncryptionContext>> {
37        Ok(self.0.encrypt_list(cipher_views).await?)
38    }
39
40    /// Decrypt cipher list with failures
41    /// Returns both successfully decrypted ciphers and any that failed to decrypt
42    // Note that this function still needs to return a Result, as the parameter conversion can still
43    // fail
44    pub async fn decrypt_list_with_failures(
45        &self,
46        ciphers: Vec<Cipher>,
47    ) -> Result<DecryptCipherListResult> {
48        Ok(self.0.decrypt_list_with_failures(ciphers.into()).await?)
49    }
50
51    /// Move a cipher to an organization, reencrypting the cipher key if necessary
52    pub fn move_to_organization(
53        &self,
54        cipher: CipherView,
55        organization_id: OrganizationId,
56    ) -> Result<CipherView> {
57        Ok(self.0.move_to_organization(cipher, organization_id)?)
58    }
59
60    /// Prepare ciphers for bulk share to an organization
61    pub async fn prepare_ciphers_for_bulk_share(
62        &self,
63        ciphers: Vec<CipherView>,
64        organization_id: OrganizationId,
65        collection_ids: Vec<CollectionId>,
66    ) -> Result<Vec<EncryptionContext>> {
67        Ok(self
68            .0
69            .prepare_ciphers_for_bulk_share(ciphers, organization_id, collection_ids)
70            .await?)
71    }
72
73    /// Decrypt full cipher list
74    /// Returns both successfully fully decrypted ciphers and any that failed to decrypt
75    // Note that this function still needs to return a Result, as the parameter conversion can still
76    // fail
77    pub async fn decrypt_list_full_with_failures(
78        &self,
79        ciphers: Vec<Cipher>,
80    ) -> Result<DecryptCipherResult> {
81        Ok(self
82            .0
83            .decrypt_list_full_with_failures(ciphers.into())
84            .await?)
85    }
86
87    /// Create a new cipher and save it to the server and local state
88    pub async fn create(&self, request: CipherCreateRequest) -> Result<CipherView> {
89        Ok(self.0.create(request).await?)
90    }
91
92    /// Edit an existing cipher and save it to the server and local state
93    pub async fn edit(&self, request: CipherEditRequest) -> Result<CipherView> {
94        Ok(self.0.edit(request).await?)
95    }
96
97    /// Edit a PAM-gated cipher whose secrets were revealed under an active lease.
98    ///
99    /// Pass the full view obtained from the lease-authorised read as `original_cipher_view`. The
100    /// returned view is partial, since the server withholds secrets from a gated write-return.
101    pub async fn edit_gated(
102        &self,
103        request: CipherEditRequest,
104        original_cipher_view: CipherView,
105    ) -> Result<CipherView> {
106        Ok(self.0.edit_gated(request, original_cipher_view).await?)
107    }
108
109    /// Update only the folder and favorite status of a cipher, for users without edit permission
110    pub async fn edit_partial(&self, request: CipherPartialEditRequest) -> Result<CipherView> {
111        Ok(self.0.edit_partial(request).await?)
112    }
113
114    /// Update the collections a cipher belongs to
115    pub async fn update_collection(
116        &self,
117        cipher_id: CipherId,
118        collection_ids: Vec<CollectionId>,
119    ) -> Result<CipherView> {
120        Ok(self.0.update_collection(cipher_id, collection_ids).await?)
121    }
122
123    /// Get a cipher from local state and decrypt it
124    pub async fn get(&self, cipher_id: CipherId) -> Result<CipherView> {
125        Ok(self.0.get(cipher_id).await?)
126    }
127
128    /// Get all ciphers from local state and decrypt them to list views, returning both successes
129    /// and failures
130    pub async fn list(&self) -> Result<DecryptCipherListResult> {
131        Ok(self.0.list().await?)
132    }
133
134    /// Get all ciphers from local state and fully decrypt them, returning both successes and
135    /// failures
136    pub async fn get_all(&self) -> Result<DecryptCipherResult> {
137        Ok(self.0.get_all().await?)
138    }
139
140    /// Permanently delete a cipher from the server and local state
141    pub async fn delete(&self, cipher_id: CipherId) -> Result<()> {
142        Ok(self.0.delete(cipher_id).await?)
143    }
144
145    /// Permanently delete multiple ciphers from the server and local state
146    pub async fn delete_many(
147        &self,
148        cipher_ids: Vec<CipherId>,
149        organization_id: Option<OrganizationId>,
150    ) -> Result<()> {
151        Ok(self.0.delete_many(cipher_ids, organization_id).await?)
152    }
153
154    /// Move a cipher to the trash
155    pub async fn soft_delete(&self, cipher_id: CipherId) -> Result<()> {
156        Ok(self.0.soft_delete(cipher_id).await?)
157    }
158
159    /// Move multiple ciphers to the trash
160    pub async fn soft_delete_many(
161        &self,
162        cipher_ids: Vec<CipherId>,
163        organization_id: Option<OrganizationId>,
164    ) -> Result<()> {
165        Ok(self.0.soft_delete_many(cipher_ids, organization_id).await?)
166    }
167
168    /// Restore a cipher from the trash
169    pub async fn restore(&self, cipher_id: CipherId) -> Result<CipherView> {
170        Ok(self.0.restore(cipher_id).await?)
171    }
172
173    /// Restore multiple ciphers from the trash
174    pub async fn restore_many(&self, cipher_ids: Vec<CipherId>) -> Result<DecryptCipherListResult> {
175        Ok(self.0.restore_many(cipher_ids).await?)
176    }
177
178    /// Share a cipher with an organization, re-encrypting it with the organization key
179    pub async fn share_cipher(
180        &self,
181        cipher_view: CipherView,
182        organization_id: OrganizationId,
183        collection_ids: Vec<CollectionId>,
184        original_cipher_view: Option<CipherView>,
185    ) -> Result<CipherView> {
186        Ok(self
187            .0
188            .share_cipher(
189                cipher_view,
190                organization_id,
191                collection_ids,
192                original_cipher_view,
193            )
194            .await?)
195    }
196
197    /// Share multiple ciphers with an organization, re-encrypting them with the organization key
198    pub async fn share_ciphers_bulk(
199        &self,
200        cipher_views: Vec<CipherView>,
201        organization_id: OrganizationId,
202        collection_ids: Vec<CollectionId>,
203    ) -> Result<Vec<CipherView>> {
204        Ok(self
205            .0
206            .share_ciphers_bulk(cipher_views, organization_id, collection_ids)
207            .await?)
208    }
209
210    /// Move multiple ciphers to a folder, or out of any folder when `folder_id` is `None`
211    pub async fn move_many(
212        &self,
213        cipher_ids: Vec<CipherId>,
214        folder_id: Option<FolderId>,
215    ) -> Result<()> {
216        Ok(self.0.move_many(cipher_ids, folder_id).await?)
217    }
218
219    /// Add or remove collections for multiple organization ciphers
220    pub async fn bulk_update_collections(
221        &self,
222        organization_id: OrganizationId,
223        cipher_ids: Vec<CipherId>,
224        collection_ids: Vec<CollectionId>,
225        remove_collections: bool,
226    ) -> Result<()> {
227        Ok(self
228            .0
229            .bulk_update_collections(
230                organization_id,
231                cipher_ids,
232                collection_ids,
233                remove_collections,
234            )
235            .await?)
236    }
237}