diff --git a/crates/bitwarden-uniffi/src/vault/ciphers.rs b/crates/bitwarden-uniffi/src/vault/ciphers.rs index 4a7c5e09..9fb13e60 100644 --- a/crates/bitwarden-uniffi/src/vault/ciphers.rs +++ b/crates/bitwarden-uniffi/src/vault/ciphers.rs @@ -1,4 +1,4 @@ -use bitwarden_vault::{Cipher, CipherListView, CipherView, Fido2CredentialView}; +use bitwarden_vault::{Cipher, CipherListView, CipherView, Fido2CredentialView, OrganizationId}; use uuid::Uuid; use crate::{error::Error, Result}; @@ -41,7 +41,7 @@ impl CiphersClient { ) -> Result { Ok(self .0 - .move_to_organization(cipher, organization_id) + .move_to_organization(cipher, OrganizationId(organization_id)) .map_err(Error::Cipher)?) } } diff --git a/crates/bitwarden-vault/src/cipher/cipher_client.rs b/crates/bitwarden-vault/src/cipher/cipher_client.rs index e4e2e219..bfc8699e 100644 --- a/crates/bitwarden-vault/src/cipher/cipher_client.rs +++ b/crates/bitwarden-vault/src/cipher/cipher_client.rs @@ -1,15 +1,27 @@ use bitwarden_core::Client; use bitwarden_crypto::IdentifyKey; -use uuid::Uuid; +#[cfg(feature = "wasm")] +use wasm_bindgen::prelude::*; use crate::{ Cipher, CipherError, CipherListView, CipherView, DecryptError, EncryptError, VaultClient, }; +/// NewType representing an Organization ID. This ensures type safety. +#[cfg_attr( + feature = "wasm", + derive(serde::Serialize, serde::Deserialize, tsify_next::Tsify), + tsify(into_wasm_abi, from_wasm_abi) +)] +#[repr(transparent)] +pub struct OrganizationId(pub uuid::Uuid); + +#[cfg_attr(feature = "wasm", wasm_bindgen)] pub struct CiphersClient { pub(crate) client: Client, } +#[cfg_attr(feature = "wasm", wasm_bindgen)] impl CiphersClient { pub fn encrypt(&self, mut cipher_view: CipherView) -> Result { let key_store = self.client.internal.get_key_store(); @@ -55,10 +67,10 @@ impl CiphersClient { pub fn move_to_organization( &self, mut cipher_view: CipherView, - organization_id: Uuid, + organization_id: OrganizationId, ) -> Result { let key_store = self.client.internal.get_key_store(); - cipher_view.move_to_organization(&mut key_store.context(), organization_id)?; + cipher_view.move_to_organization(&mut key_store.context(), organization_id.0)?; Ok(cipher_view) } @@ -206,7 +218,7 @@ mod tests { // Move cipher to organization let res = client.vault().ciphers().move_to_organization( view, - "1bc9ac1e-f5aa-45f2-94bf-b181009709b8".parse().unwrap(), + OrganizationId("1bc9ac1e-f5aa-45f2-94bf-b181009709b8".parse().unwrap()), ); assert!(res.is_err()); @@ -301,7 +313,7 @@ mod tests { .ciphers() .move_to_organization( view, - "1bc9ac1e-f5aa-45f2-94bf-b181009709b8".parse().unwrap(), + OrganizationId("1bc9ac1e-f5aa-45f2-94bf-b181009709b8".parse().unwrap()), ) .unwrap(); let new_cipher = client.vault().ciphers().encrypt(new_view).unwrap(); diff --git a/crates/bitwarden-vault/src/cipher/mod.rs b/crates/bitwarden-vault/src/cipher/mod.rs index 445c181b..76c0638b 100644 --- a/crates/bitwarden-vault/src/cipher/mod.rs +++ b/crates/bitwarden-vault/src/cipher/mod.rs @@ -22,7 +22,7 @@ pub use cipher::{ Cipher, CipherError, CipherListView, CipherListViewType, CipherRepromptType, CipherType, CipherView, }; -pub use cipher_client::CiphersClient; +pub use cipher_client::{CiphersClient, OrganizationId}; pub use field::FieldView; pub use identity::IdentityView; pub use login::{ diff --git a/crates/bitwarden-vault/src/folder_client.rs b/crates/bitwarden-vault/src/folder_client.rs index 6821f90f..a238f834 100644 --- a/crates/bitwarden-vault/src/folder_client.rs +++ b/crates/bitwarden-vault/src/folder_client.rs @@ -1,14 +1,18 @@ use bitwarden_core::Client; +#[cfg(feature = "wasm")] +use wasm_bindgen::prelude::*; use crate::{ error::{DecryptError, EncryptError}, Folder, FolderView, VaultClient, }; +#[cfg_attr(feature = "wasm", wasm_bindgen)] pub struct FoldersClient { pub(crate) client: Client, } +#[cfg_attr(feature = "wasm", wasm_bindgen)] impl FoldersClient { pub fn encrypt(&self, folder_view: FolderView) -> Result { let key_store = self.client.internal.get_key_store(); diff --git a/crates/bitwarden-wasm-internal/src/lib.rs b/crates/bitwarden-wasm-internal/src/lib.rs index 1e21f6ec..ec94f77a 100644 --- a/crates/bitwarden-wasm-internal/src/lib.rs +++ b/crates/bitwarden-wasm-internal/src/lib.rs @@ -14,4 +14,4 @@ pub use client::BitwardenClient; pub use crypto::CryptoClient; pub use generators::GeneratorClient; pub use init::init_sdk; -pub use vault::{folders::FoldersClient, VaultClient}; +pub use vault::VaultClient; diff --git a/crates/bitwarden-wasm-internal/src/vault/ciphers.rs b/crates/bitwarden-wasm-internal/src/vault/ciphers.rs deleted file mode 100644 index 11c91ce1..00000000 --- a/crates/bitwarden-wasm-internal/src/vault/ciphers.rs +++ /dev/null @@ -1,86 +0,0 @@ -use bitwarden_vault::{ - Cipher, CipherError, CipherListView, CipherView, DecryptError, EncryptError, - Fido2CredentialView, -}; -use wasm_bindgen::prelude::wasm_bindgen; - -#[wasm_bindgen] -pub struct CiphersClient(bitwarden_vault::CiphersClient); - -impl CiphersClient { - pub fn new(client: bitwarden_vault::CiphersClient) -> Self { - Self(client) - } -} - -#[wasm_bindgen] -impl CiphersClient { - /// 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 { - 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 { - self.0.decrypt(cipher) - } - - /// Decrypt list of ciphers - /// - /// # Arguments - /// - `ciphers` - The list of encrypted ciphers to decrypt - /// - /// # Returns - /// - `Ok(Vec)` containing the decrypted ciphers - /// - `Err(DecryptError)` if decryption fails - pub fn decrypt_list(&self, ciphers: Vec) -> Result, DecryptError> { - self.0.decrypt_list(ciphers) - } - - /// Decrypt FIDO2 credentials - /// - /// # Arguments - /// - `cipher_view` - Cipher to encrypt containing the FIDO2 credential - /// - /// # Returns - /// - `Ok(Vec)` containing the decrypted FIDO2 credentials - /// - `Err(DecryptError)` if decryption fails - pub fn decrypt_fido2_credentials( - &self, - cipher_view: CipherView, - ) -> Result, 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 { - self.0.decrypt_fido2_private_key(cipher_view) - } -} diff --git a/crates/bitwarden-wasm-internal/src/vault/folders.rs b/crates/bitwarden-wasm-internal/src/vault/folders.rs deleted file mode 100644 index 92900b03..00000000 --- a/crates/bitwarden-wasm-internal/src/vault/folders.rs +++ /dev/null @@ -1,19 +0,0 @@ -use bitwarden_vault::{DecryptError, Folder, FolderView}; -use wasm_bindgen::prelude::*; - -#[wasm_bindgen] -pub struct FoldersClient(bitwarden_vault::FoldersClient); - -impl FoldersClient { - pub fn new(client: bitwarden_vault::FoldersClient) -> Self { - Self(client) - } -} - -#[wasm_bindgen] -impl FoldersClient { - /// Decrypt folder - pub fn decrypt(&self, folder: Folder) -> Result { - self.0.decrypt(folder) - } -} diff --git a/crates/bitwarden-wasm-internal/src/vault/mod.rs b/crates/bitwarden-wasm-internal/src/vault/mod.rs index d0e4e118..bbb980cf 100644 --- a/crates/bitwarden-wasm-internal/src/vault/mod.rs +++ b/crates/bitwarden-wasm-internal/src/vault/mod.rs @@ -1,15 +1,11 @@ pub mod attachments; -pub mod ciphers; -pub mod folders; pub mod totp; use attachments::AttachmentsClient; -use ciphers::CiphersClient; +use bitwarden_vault::{CiphersClient, FoldersClient}; use totp::TotpClient; use wasm_bindgen::prelude::*; -use crate::FoldersClient; - #[wasm_bindgen] pub struct VaultClient(bitwarden_vault::VaultClient); @@ -26,11 +22,11 @@ impl VaultClient { } pub fn ciphers(&self) -> CiphersClient { - CiphersClient::new(self.0.ciphers()) + self.0.ciphers() } pub fn folders(&self) -> FoldersClient { - FoldersClient::new(self.0.folders()) + self.0.folders() } pub fn totp(&self) -> TotpClient {