diff --git a/crates/bitwarden-uniffi/src/vault/mod.rs b/crates/bitwarden-uniffi/src/vault/mod.rs index d83c51a61..1254bfba2 100644 --- a/crates/bitwarden-uniffi/src/vault/mod.rs +++ b/crates/bitwarden-uniffi/src/vault/mod.rs @@ -46,7 +46,11 @@ impl VaultClient { /// - OTP Auth URI /// - Steam URI pub fn generate_totp(&self, key: String, time: Option>) -> Result { - Ok(self.0.generate_totp(key, time).map_err(Error::Totp)?) + Ok(self + .0 + .totp() + .generate_totp(key, time) + .map_err(Error::Totp)?) } /// Generate a TOTP code from a provided cipher list view. @@ -57,6 +61,7 @@ impl VaultClient { ) -> Result { Ok(self .0 + .totp() .generate_totp_cipher_view(view, time) .map_err(Error::Totp)?) } diff --git a/crates/bitwarden-vault/src/cipher/attachment_client.rs b/crates/bitwarden-vault/src/cipher/attachment_client.rs index ce4c8002a..1b4bb979e 100644 --- a/crates/bitwarden-vault/src/cipher/attachment_client.rs +++ b/crates/bitwarden-vault/src/cipher/attachment_client.rs @@ -4,12 +4,15 @@ use bitwarden_core::Client; use bitwarden_crypto::EncString; use bitwarden_error::bitwarden_error; use thiserror::Error; +#[cfg(feature = "wasm")] +use wasm_bindgen::prelude::*; use crate::{ Attachment, AttachmentEncryptResult, AttachmentFile, AttachmentFileView, AttachmentView, Cipher, DecryptError, EncryptError, }; +#[cfg_attr(feature = "wasm", wasm_bindgen)] pub struct AttachmentsClient { pub(crate) client: Client, } @@ -34,6 +37,24 @@ pub enum DecryptFileError { Io(#[from] std::io::Error), } +#[cfg_attr(feature = "wasm", wasm_bindgen)] +impl AttachmentsClient { + pub fn decrypt_buffer( + &self, + cipher: Cipher, + attachment: AttachmentView, + encrypted_buffer: &[u8], + ) -> Result, DecryptError> { + let key_store = self.client.internal.get_key_store(); + + Ok(key_store.decrypt(&AttachmentFile { + cipher, + attachment, + contents: EncString::from_buffer(encrypted_buffer)?, + })?) + } +} + impl AttachmentsClient { pub fn encrypt_buffer( &self, @@ -49,6 +70,7 @@ impl AttachmentsClient { contents: buffer, })?) } + pub fn encrypt_file( &self, cipher: Cipher, @@ -65,20 +87,6 @@ impl AttachmentsClient { Ok(attachment) } - pub fn decrypt_buffer( - &self, - cipher: Cipher, - attachment: AttachmentView, - encrypted_buffer: &[u8], - ) -> Result, DecryptError> { - let key_store = self.client.internal.get_key_store(); - - Ok(key_store.decrypt(&AttachmentFile { - cipher, - attachment, - contents: EncString::from_buffer(encrypted_buffer)?, - })?) - } pub fn decrypt_file( &self, cipher: Cipher, diff --git a/crates/bitwarden-vault/src/lib.rs b/crates/bitwarden-vault/src/lib.rs index 15ac70a1b..e1d893cc2 100644 --- a/crates/bitwarden-vault/src/lib.rs +++ b/crates/bitwarden-vault/src/lib.rs @@ -31,5 +31,7 @@ mod vault_client; pub use vault_client::{VaultClient, VaultClientExt}; mod sync; -mod totp_client; pub use sync::{SyncRequest, SyncResponse}; + +mod totp_client; +pub use totp_client::TotpClient; diff --git a/crates/bitwarden-vault/src/totp_client.rs b/crates/bitwarden-vault/src/totp_client.rs index 9c39ad220..812ae8210 100644 --- a/crates/bitwarden-vault/src/totp_client.rs +++ b/crates/bitwarden-vault/src/totp_client.rs @@ -1,10 +1,39 @@ +use bitwarden_core::Client; use chrono::{DateTime, Utc}; +#[cfg(feature = "wasm")] +use wasm_bindgen::prelude::*; -use crate::{ - generate_totp, generate_totp_cipher_view, CipherListView, TotpError, TotpResponse, VaultClient, -}; +use crate::{generate_totp, generate_totp_cipher_view, CipherListView, TotpError, TotpResponse}; -impl VaultClient { +#[cfg_attr(feature = "wasm", wasm_bindgen)] +pub struct TotpClient { + pub(crate) client: Client, +} + +#[cfg(feature = "wasm")] +#[wasm_bindgen] +impl TotpClient { + /// Generates a TOTP code from a provided key + /// + /// # Arguments + /// - `key` - Can be: + /// - A base32 encoded string + /// - OTP Auth URI + /// - Steam URI + /// - `time_ms` - Optional timestamp in milliseconds + #[wasm_bindgen(js_name = "generate_totp")] + pub fn generate_totp_wasm( + &self, + key: String, + time_ms: Option, + ) -> Result { + let datetime = time_ms.and_then(|time| DateTime::::from_timestamp_millis(time as i64)); + + self.generate_totp(key, datetime) + } +} + +impl TotpClient { /// Generate a TOTP code from a provided key. /// /// Key can be either: diff --git a/crates/bitwarden-vault/src/vault_client.rs b/crates/bitwarden-vault/src/vault_client.rs index abd71dfed..527d35a71 100644 --- a/crates/bitwarden-vault/src/vault_client.rs +++ b/crates/bitwarden-vault/src/vault_client.rs @@ -1,12 +1,15 @@ use bitwarden_core::Client; +#[cfg(feature = "wasm")] +use wasm_bindgen::prelude::*; use crate::{ sync::{sync, SyncError}, AttachmentsClient, CiphersClient, CollectionsClient, FoldersClient, PasswordHistoryClient, - SyncRequest, SyncResponse, + SyncRequest, SyncResponse, TotpClient, }; #[derive(Clone)] +#[cfg_attr(feature = "wasm", wasm_bindgen)] pub struct VaultClient { pub(crate) client: Client, } @@ -16,6 +19,27 @@ impl VaultClient { Self { client } } + pub async fn sync(&self, input: &SyncRequest) -> Result { + sync(&self.client, input).await + } + + /// Collection related operations. + pub fn collections(&self) -> CollectionsClient { + CollectionsClient { + client: self.client.clone(), + } + } + + /// Password history related operations. + pub fn password_history(&self) -> PasswordHistoryClient { + PasswordHistoryClient { + client: self.client.clone(), + } + } +} + +#[cfg_attr(feature = "wasm", wasm_bindgen)] +impl VaultClient { /// Attachment related operations. pub fn attachments(&self) -> AttachmentsClient { AttachmentsClient { @@ -30,13 +54,6 @@ impl VaultClient { } } - /// Collection related operations. - pub fn collections(&self) -> CollectionsClient { - CollectionsClient { - client: self.client.clone(), - } - } - /// Folder related operations. pub fn folders(&self) -> FoldersClient { FoldersClient { @@ -44,16 +61,12 @@ impl VaultClient { } } - /// Password history related operations. - pub fn password_history(&self) -> PasswordHistoryClient { - PasswordHistoryClient { + /// TOTP related operations. + pub fn totp(&self) -> TotpClient { + TotpClient { client: self.client.clone(), } } - - pub async fn sync(&self, input: &SyncRequest) -> Result { - sync(&self.client, input).await - } } pub trait VaultClientExt { diff --git a/crates/bitwarden-wasm-internal/src/client.rs b/crates/bitwarden-wasm-internal/src/client.rs index 4cde263af..a84da4aa1 100644 --- a/crates/bitwarden-wasm-internal/src/client.rs +++ b/crates/bitwarden-wasm-internal/src/client.rs @@ -5,10 +5,10 @@ use bitwarden_core::{Client, ClientSettings}; use bitwarden_error::bitwarden_error; use bitwarden_exporters::ExporterClientExt; use bitwarden_generators::GeneratorClientsExt; -use bitwarden_vault::VaultClientExt; +use bitwarden_vault::{VaultClient, VaultClientExt}; use wasm_bindgen::prelude::*; -use crate::{CryptoClient, VaultClient}; +use crate::CryptoClient; #[wasm_bindgen] pub struct BitwardenClient(pub(crate) Client); @@ -46,7 +46,7 @@ impl BitwardenClient { } pub fn vault(&self) -> VaultClient { - VaultClient::new(self.0.vault()) + self.0.vault() } /// Constructs a specific client for generating passwords and passphrases diff --git a/crates/bitwarden-wasm-internal/src/lib.rs b/crates/bitwarden-wasm-internal/src/lib.rs index 0b4b285ed..81ff14cdd 100644 --- a/crates/bitwarden-wasm-internal/src/lib.rs +++ b/crates/bitwarden-wasm-internal/src/lib.rs @@ -6,10 +6,8 @@ mod custom_types; mod init; mod pure_crypto; mod ssh; -mod vault; pub use bitwarden_ipc::wasm::*; pub use client::BitwardenClient; pub use crypto::CryptoClient; pub use init::init_sdk; -pub use vault::VaultClient; diff --git a/crates/bitwarden-wasm-internal/src/vault/attachments.rs b/crates/bitwarden-wasm-internal/src/vault/attachments.rs deleted file mode 100644 index 1f0acae64..000000000 --- a/crates/bitwarden-wasm-internal/src/vault/attachments.rs +++ /dev/null @@ -1,24 +0,0 @@ -use bitwarden_vault::{AttachmentView, Cipher, DecryptError}; -use wasm_bindgen::prelude::wasm_bindgen; - -#[wasm_bindgen] -pub struct AttachmentsClient(bitwarden_vault::AttachmentsClient); - -impl AttachmentsClient { - pub fn new(client: bitwarden_vault::AttachmentsClient) -> Self { - Self(client) - } -} - -#[wasm_bindgen] -impl AttachmentsClient { - /// Decrypts an attachment's encrypted content - pub fn decrypt_buffer( - &self, - cipher: Cipher, - attachment: AttachmentView, - encrypted_buffer: &[u8], - ) -> Result, DecryptError> { - self.0.decrypt_buffer(cipher, attachment, encrypted_buffer) - } -} diff --git a/crates/bitwarden-wasm-internal/src/vault/mod.rs b/crates/bitwarden-wasm-internal/src/vault/mod.rs deleted file mode 100644 index bbb980cfc..000000000 --- a/crates/bitwarden-wasm-internal/src/vault/mod.rs +++ /dev/null @@ -1,35 +0,0 @@ -pub mod attachments; -pub mod totp; - -use attachments::AttachmentsClient; -use bitwarden_vault::{CiphersClient, FoldersClient}; -use totp::TotpClient; -use wasm_bindgen::prelude::*; - -#[wasm_bindgen] -pub struct VaultClient(bitwarden_vault::VaultClient); - -impl VaultClient { - pub fn new(client: bitwarden_vault::VaultClient) -> Self { - Self(client) - } -} - -#[wasm_bindgen] -impl VaultClient { - pub fn attachments(&self) -> AttachmentsClient { - AttachmentsClient::new(self.0.attachments()) - } - - pub fn ciphers(&self) -> CiphersClient { - self.0.ciphers() - } - - pub fn folders(&self) -> FoldersClient { - self.0.folders() - } - - pub fn totp(&self) -> TotpClient { - TotpClient::new(self.0.clone()) - } -} diff --git a/crates/bitwarden-wasm-internal/src/vault/totp.rs b/crates/bitwarden-wasm-internal/src/vault/totp.rs deleted file mode 100644 index 5c77e7eb2..000000000 --- a/crates/bitwarden-wasm-internal/src/vault/totp.rs +++ /dev/null @@ -1,36 +0,0 @@ -use chrono::{DateTime, Utc}; -use wasm_bindgen::prelude::*; - -#[wasm_bindgen] -pub struct TotpClient(bitwarden_vault::VaultClient); - -impl TotpClient { - pub fn new(client: bitwarden_vault::VaultClient) -> Self { - Self(client) - } -} - -#[wasm_bindgen] -impl TotpClient { - /// Generates a TOTP code from a provided key - /// - /// # Arguments - /// - `key` - Can be: - /// - A base32 encoded string - /// - OTP Auth URI - /// - Steam URI - /// - `time_ms` - Optional timestamp in milliseconds - /// - /// # Returns - /// - `Ok(TotpResponse)` containing the generated code and period - /// - `Err(TotpError)` if code generation fails - pub fn generate_totp( - &self, - key: String, - time_ms: Option, - ) -> Result { - let datetime = time_ms.and_then(|time| DateTime::::from_timestamp_millis(time as i64)); - - self.0.generate_totp(key, datetime) - } -}