bitwarden_uniffi/vault/
ciphers.rs

1use bitwarden_core::OrganizationId;
2use bitwarden_vault::{
3    Cipher, CipherListView, CipherView, DecryptCipherListResult, EncryptionContext,
4    Fido2CredentialView,
5};
6use uuid::Uuid;
7
8use crate::{error::Error, Result};
9
10#[allow(missing_docs)]
11#[derive(uniffi::Object)]
12pub struct CiphersClient(pub(crate) bitwarden_vault::CiphersClient);
13
14#[uniffi::export]
15impl CiphersClient {
16    /// Encrypt cipher
17    pub fn encrypt(&self, cipher_view: CipherView) -> Result<EncryptionContext> {
18        Ok(self.0.encrypt(cipher_view).map_err(Error::Encrypt)?)
19    }
20
21    /// Decrypt cipher
22    pub fn decrypt(&self, cipher: Cipher) -> Result<CipherView> {
23        Ok(self.0.decrypt(cipher).map_err(Error::Decrypt)?)
24    }
25
26    /// Decrypt cipher list
27    pub fn decrypt_list(&self, ciphers: Vec<Cipher>) -> Result<Vec<CipherListView>> {
28        Ok(self.0.decrypt_list(ciphers).map_err(Error::Decrypt)?)
29    }
30
31    /// Decrypt cipher list with failures
32    /// Returns both successfully decrypted ciphers and any that failed to decrypt
33    pub fn decrypt_list_with_failures(&self, ciphers: Vec<Cipher>) -> DecryptCipherListResult {
34        self.0.decrypt_list_with_failures(ciphers)
35    }
36
37    pub fn decrypt_fido2_credentials(
38        &self,
39        cipher_view: CipherView,
40    ) -> Result<Vec<Fido2CredentialView>> {
41        Ok(self
42            .0
43            .decrypt_fido2_credentials(cipher_view)
44            .map_err(Error::Decrypt)?)
45    }
46
47    /// Move a cipher to an organization, reencrypting the cipher key if necessary
48    pub fn move_to_organization(
49        &self,
50        cipher: CipherView,
51        organization_id: Uuid,
52    ) -> Result<CipherView> {
53        Ok(self
54            .0
55            .move_to_organization(cipher, OrganizationId::new(organization_id))
56            .map_err(Error::Cipher)?)
57    }
58}