bitwarden_wasm_internal/vault/ciphers.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
use bitwarden_vault::{
Cipher, CipherError, CipherListView, CipherView, DecryptError, EncryptError,
Fido2CredentialView,
};
use wasm_bindgen::prelude::wasm_bindgen;
#[wasm_bindgen]
pub struct ClientCiphers(bitwarden_vault::ClientCiphers);
impl ClientCiphers {
pub fn new(client: bitwarden_vault::ClientCiphers) -> Self {
Self(client)
}
}
#[wasm_bindgen]
impl ClientCiphers {
/// Encrypt cipher
///
/// # Arguments
/// - `cipher_view` - The decrypted cipher to encrypt
///
/// # Returns
/// - `Ok(Cipher)` containing the encrypted cipher
/// - `Err(EncryptError)` if encryption fails
pub fn encrypt(&self, cipher_view: CipherView) -> Result<Cipher, EncryptError> {
self.0.encrypt(cipher_view)
}
/// Decrypt cipher
///
/// # Arguments
/// - `cipher` - The encrypted cipher to decrypt
///
/// # Returns
/// - `Ok(CipherView)` containing the decrypted cipher
/// - `Err(DecryptError)` if decryption fails
pub fn decrypt(&self, cipher: Cipher) -> Result<CipherView, DecryptError> {
self.0.decrypt(cipher)
}
/// Decrypt list of ciphers
///
/// # Arguments
/// - `ciphers` - The list of encrypted ciphers to decrypt
///
/// # Returns
/// - `Ok(Vec<CipherListView>)` containing the decrypted ciphers
/// - `Err(DecryptError)` if decryption fails
pub fn decrypt_list(&self, ciphers: Vec<Cipher>) -> Result<Vec<CipherListView>, DecryptError> {
self.0.decrypt_list(ciphers)
}
/// Decrypt FIDO2 credentials
///
/// # Arguments
/// - `cipher_view` - Cipher to encrypt containing the FIDO2 credential
///
/// # Returns
/// - `Ok(Vec<Fido2CredentialView>)` containing the decrypted FIDO2 credentials
/// - `Err(DecryptError)` if decryption fails
pub fn decrypt_fido2_credentials(
&self,
cipher_view: CipherView,
) -> Result<Vec<Fido2CredentialView>, DecryptError> {
self.0.decrypt_fido2_credentials(cipher_view)
}
/// Decrypt key
///
/// This method is a temporary solution to allow typescript client access to decrypted key
/// values, particularly for FIDO2 credentials.
///
/// # Arguments
/// - `cipher_view` - Decrypted cipher containing the key
///
/// # Returns
/// - `Ok(String)` containing the decrypted key
/// - `Err(CipherError)`
pub fn decrypt_fido2_private_key(
&self,
cipher_view: CipherView,
) -> Result<String, CipherError> {
self.0.decrypt_fido2_private_key(cipher_view)
}
}