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 all commits
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
7 changes: 6 additions & 1 deletion crates/bitwarden-uniffi/src/vault/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@
/// - OTP Auth URI
/// - Steam URI
pub fn generate_totp(&self, key: String, time: Option<DateTime<Utc>>) -> Result<TotpResponse> {
Ok(self.0.generate_totp(key, time).map_err(Error::Totp)?)
Ok(self
.0
.totp()
.generate_totp(key, time)
.map_err(Error::Totp)?)

Check warning on line 53 in crates/bitwarden-uniffi/src/vault/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/vault/mod.rs#L49-L53

Added lines #L49 - L53 were not covered by tests
}

/// Generate a TOTP code from a provided cipher list view.
Expand All @@ -57,6 +61,7 @@
) -> Result<TotpResponse> {
Ok(self
.0
.totp()

Check warning on line 64 in crates/bitwarden-uniffi/src/vault/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/vault/mod.rs#L64

Added line #L64 was not covered by tests
.generate_totp_cipher_view(view, time)
.map_err(Error::Totp)?)
}
Expand Down
36 changes: 22 additions & 14 deletions crates/bitwarden-vault/src/cipher/attachment_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
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,
}
Expand All @@ -34,6 +37,24 @@
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)?,
})?)

Check warning on line 54 in crates/bitwarden-vault/src/cipher/attachment_client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-vault/src/cipher/attachment_client.rs#L54

Added line #L54 was not covered by tests
}
}

impl AttachmentsClient {
pub fn encrypt_buffer(
&self,
Expand All @@ -49,6 +70,7 @@
contents: buffer,
})?)
}

pub fn encrypt_file(
&self,
cipher: Cipher,
Expand All @@ -65,20 +87,6 @@
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 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;
37 changes: 33 additions & 4 deletions crates/bitwarden-vault/src/totp_client.rs
Original file line number Diff line number Diff line change
@@ -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<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)
}

Check warning on line 33 in crates/bitwarden-vault/src/totp_client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-vault/src/totp_client.rs#L25-L33

Added lines #L25 - L33 were not covered by tests
}

impl TotpClient {
/// Generate a TOTP code from a provided key.
///
/// Key can be either:
Expand Down
43 changes: 28 additions & 15 deletions crates/bitwarden-vault/src/vault_client.rs
Original file line number Diff line number Diff line change
@@ -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,
}
Expand All @@ -16,6 +19,27 @@
Self { client }
}

pub async fn sync(&self, input: &SyncRequest) -> Result<SyncResponse, SyncError> {
sync(&self.client, input).await
}

Check warning on line 24 in crates/bitwarden-vault/src/vault_client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-vault/src/vault_client.rs#L22-L24

Added lines #L22 - L24 were not covered by tests

/// 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(),
}
}

Check warning on line 38 in crates/bitwarden-vault/src/vault_client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-vault/src/vault_client.rs#L34-L38

Added lines #L34 - L38 were not covered by tests
}

#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl VaultClient {
/// Attachment related operations.
pub fn attachments(&self) -> AttachmentsClient {
AttachmentsClient {
Expand All @@ -30,30 +54,19 @@
}
}

/// Collection related operations.
pub fn collections(&self) -> CollectionsClient {
CollectionsClient {
client: self.client.clone(),
}
}

/// Folder related operations.
pub fn folders(&self) -> FoldersClient {
FoldersClient {
client: self.client.clone(),
}
}

/// Password history related operations.
pub fn password_history(&self) -> PasswordHistoryClient {
PasswordHistoryClient {
/// TOTP related operations.
pub fn totp(&self) -> TotpClient {
TotpClient {

Check warning on line 66 in crates/bitwarden-vault/src/vault_client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-vault/src/vault_client.rs#L65-L66

Added lines #L65 - L66 were not covered by tests
client: self.client.clone(),
}
}

pub async fn sync(&self, input: &SyncRequest) -> Result<SyncResponse, SyncError> {
sync(&self.client, input).await
}
}

pub trait VaultClientExt {
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_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 @@
}

pub fn vault(&self) -> VaultClient {
VaultClient::new(self.0.vault())
self.0.vault()

Check warning on line 49 in crates/bitwarden-wasm-internal/src/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/client.rs#L49

Added line #L49 was not covered by tests
}

/// 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.