Skip to content

Remove vault folder from wasm-internal #294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions crates/bitwarden-vault/src/cipher/attachment_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ use crate::{
Cipher, DecryptError, EncryptError, VaultClient,
};

#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;

#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub struct AttachmentsClient {
pub(crate) client: Client,
}
Expand All @@ -34,6 +38,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<Vec<u8>, 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,
Expand All @@ -49,6 +71,7 @@ impl AttachmentsClient {
contents: buffer,
})?)
}

pub fn encrypt_file(
&self,
cipher: Cipher,
Expand All @@ -65,20 +88,6 @@ impl AttachmentsClient {
Ok(attachment)
}

pub fn decrypt_buffer(
&self,
cipher: Cipher,
attachment: AttachmentView,
encrypted_buffer: &[u8],
) -> Result<Vec<u8>, 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,
Expand All @@ -93,6 +102,7 @@ impl AttachmentsClient {
}
}

#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl VaultClient {
pub fn attachments(&self) -> AttachmentsClient {
AttachmentsClient {
Expand Down
1 change: 1 addition & 0 deletions crates/bitwarden-vault/src/cipher/cipher_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ impl CiphersClient {
}
}

#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl VaultClient {
pub fn ciphers(&self) -> CiphersClient {
CiphersClient {
Expand Down
1 change: 1 addition & 0 deletions crates/bitwarden-vault/src/folder_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl FoldersClient {
}
}

#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl VaultClient {
pub fn folders(&self) -> FoldersClient {
FoldersClient {
Expand Down
4 changes: 3 additions & 1 deletion crates/bitwarden-vault/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
43 changes: 42 additions & 1 deletion crates/bitwarden-vault/src/totp_client.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,42 @@
use bitwarden_core::Client;
use chrono::{DateTime, Utc};

use crate::{
generate_totp, generate_totp_cipher_view, CipherListView, TotpError, TotpResponse, VaultClient,
};

impl VaultClient {
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;

#[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<f64>,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DateTimes 😢

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're having a separate WASM function to deal with the parameters, we could use js_sys::Date or js_sys::Number here and then convert, that'll probably be a breaking change for the current SDK API though, so maybe later.

) -> Result<TotpResponse, TotpError> {
let datetime = time_ms.and_then(|time| DateTime::<Utc>::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:
Expand All @@ -30,3 +62,12 @@ impl VaultClient {
generate_totp_cipher_view(&mut key_store.context(), view, time)
}
}

#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl VaultClient {
pub fn totp(&self) -> TotpClient {
TotpClient {
client: self.client.clone(),
}
}
}
4 changes: 4 additions & 0 deletions crates/bitwarden-vault/src/vault_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use crate::{
SyncRequest, SyncResponse,
};

#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;

#[derive(Clone)]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub struct VaultClient {
pub(crate) client: Client,
}
Expand Down
6 changes: 3 additions & 3 deletions crates/bitwarden-wasm-internal/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions crates/bitwarden-wasm-internal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
24 changes: 0 additions & 24 deletions crates/bitwarden-wasm-internal/src/vault/attachments.rs

This file was deleted.

35 changes: 0 additions & 35 deletions crates/bitwarden-wasm-internal/src/vault/mod.rs

This file was deleted.

36 changes: 0 additions & 36 deletions crates/bitwarden-wasm-internal/src/vault/totp.rs

This file was deleted.

Loading