Skip to content

Commit 3425208

Browse files
authored
Remove vault folder from wasm-internal (#294)
Removes the vault folder from wasm-internal and relies on the upstream implementations instead. Note this adds `wasm_bindgen` to everything that should be exposed which is a bit clunky.
1 parent 3d2e4b5 commit 3425208

File tree

10 files changed

+95
-135
lines changed

10 files changed

+95
-135
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ impl VaultClient {
4646
/// - OTP Auth URI
4747
/// - Steam URI
4848
pub fn generate_totp(&self, key: String, time: Option<DateTime<Utc>>) -> Result<TotpResponse> {
49-
Ok(self.0.generate_totp(key, time).map_err(Error::Totp)?)
49+
Ok(self
50+
.0
51+
.totp()
52+
.generate_totp(key, time)
53+
.map_err(Error::Totp)?)
5054
}
5155

5256
/// Generate a TOTP code from a provided cipher list view.
@@ -57,6 +61,7 @@ impl VaultClient {
5761
) -> Result<TotpResponse> {
5862
Ok(self
5963
.0
64+
.totp()
6065
.generate_totp_cipher_view(view, time)
6166
.map_err(Error::Totp)?)
6267
}

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ use bitwarden_core::Client;
44
use bitwarden_crypto::EncString;
55
use bitwarden_error::bitwarden_error;
66
use thiserror::Error;
7+
#[cfg(feature = "wasm")]
8+
use wasm_bindgen::prelude::*;
79

810
use crate::{
911
Attachment, AttachmentEncryptResult, AttachmentFile, AttachmentFileView, AttachmentView,
1012
Cipher, DecryptError, EncryptError,
1113
};
1214

15+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
1316
pub struct AttachmentsClient {
1417
pub(crate) client: Client,
1518
}
@@ -34,6 +37,24 @@ pub enum DecryptFileError {
3437
Io(#[from] std::io::Error),
3538
}
3639

40+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
41+
impl AttachmentsClient {
42+
pub fn decrypt_buffer(
43+
&self,
44+
cipher: Cipher,
45+
attachment: AttachmentView,
46+
encrypted_buffer: &[u8],
47+
) -> Result<Vec<u8>, DecryptError> {
48+
let key_store = self.client.internal.get_key_store();
49+
50+
Ok(key_store.decrypt(&AttachmentFile {
51+
cipher,
52+
attachment,
53+
contents: EncString::from_buffer(encrypted_buffer)?,
54+
})?)
55+
}
56+
}
57+
3758
impl AttachmentsClient {
3859
pub fn encrypt_buffer(
3960
&self,
@@ -49,6 +70,7 @@ impl AttachmentsClient {
4970
contents: buffer,
5071
})?)
5172
}
73+
5274
pub fn encrypt_file(
5375
&self,
5476
cipher: Cipher,
@@ -65,20 +87,6 @@ impl AttachmentsClient {
6587
Ok(attachment)
6688
}
6789

68-
pub fn decrypt_buffer(
69-
&self,
70-
cipher: Cipher,
71-
attachment: AttachmentView,
72-
encrypted_buffer: &[u8],
73-
) -> Result<Vec<u8>, DecryptError> {
74-
let key_store = self.client.internal.get_key_store();
75-
76-
Ok(key_store.decrypt(&AttachmentFile {
77-
cipher,
78-
attachment,
79-
contents: EncString::from_buffer(encrypted_buffer)?,
80-
})?)
81-
}
8290
pub fn decrypt_file(
8391
&self,
8492
cipher: Cipher,

crates/bitwarden-vault/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,7 @@ mod vault_client;
3131
pub use vault_client::{VaultClient, VaultClientExt};
3232

3333
mod sync;
34-
mod totp_client;
3534
pub use sync::{SyncRequest, SyncResponse};
35+
36+
mod totp_client;
37+
pub use totp_client::TotpClient;

crates/bitwarden-vault/src/totp_client.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,39 @@
1+
use bitwarden_core::Client;
12
use chrono::{DateTime, Utc};
3+
#[cfg(feature = "wasm")]
4+
use wasm_bindgen::prelude::*;
25

3-
use crate::{
4-
generate_totp, generate_totp_cipher_view, CipherListView, TotpError, TotpResponse, VaultClient,
5-
};
6+
use crate::{generate_totp, generate_totp_cipher_view, CipherListView, TotpError, TotpResponse};
67

7-
impl VaultClient {
8+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
9+
pub struct TotpClient {
10+
pub(crate) client: Client,
11+
}
12+
13+
#[cfg(feature = "wasm")]
14+
#[wasm_bindgen]
15+
impl TotpClient {
16+
/// Generates a TOTP code from a provided key
17+
///
18+
/// # Arguments
19+
/// - `key` - Can be:
20+
/// - A base32 encoded string
21+
/// - OTP Auth URI
22+
/// - Steam URI
23+
/// - `time_ms` - Optional timestamp in milliseconds
24+
#[wasm_bindgen(js_name = "generate_totp")]
25+
pub fn generate_totp_wasm(
26+
&self,
27+
key: String,
28+
time_ms: Option<f64>,
29+
) -> Result<TotpResponse, TotpError> {
30+
let datetime = time_ms.and_then(|time| DateTime::<Utc>::from_timestamp_millis(time as i64));
31+
32+
self.generate_totp(key, datetime)
33+
}
34+
}
35+
36+
impl TotpClient {
837
/// Generate a TOTP code from a provided key.
938
///
1039
/// Key can be either:

crates/bitwarden-vault/src/vault_client.rs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
use bitwarden_core::Client;
2+
#[cfg(feature = "wasm")]
3+
use wasm_bindgen::prelude::*;
24

35
use crate::{
46
sync::{sync, SyncError},
57
AttachmentsClient, CiphersClient, CollectionsClient, FoldersClient, PasswordHistoryClient,
6-
SyncRequest, SyncResponse,
8+
SyncRequest, SyncResponse, TotpClient,
79
};
810

911
#[derive(Clone)]
12+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
1013
pub struct VaultClient {
1114
pub(crate) client: Client,
1215
}
@@ -16,6 +19,27 @@ impl VaultClient {
1619
Self { client }
1720
}
1821

22+
pub async fn sync(&self, input: &SyncRequest) -> Result<SyncResponse, SyncError> {
23+
sync(&self.client, input).await
24+
}
25+
26+
/// Collection related operations.
27+
pub fn collections(&self) -> CollectionsClient {
28+
CollectionsClient {
29+
client: self.client.clone(),
30+
}
31+
}
32+
33+
/// Password history related operations.
34+
pub fn password_history(&self) -> PasswordHistoryClient {
35+
PasswordHistoryClient {
36+
client: self.client.clone(),
37+
}
38+
}
39+
}
40+
41+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
42+
impl VaultClient {
1943
/// Attachment related operations.
2044
pub fn attachments(&self) -> AttachmentsClient {
2145
AttachmentsClient {
@@ -30,30 +54,19 @@ impl VaultClient {
3054
}
3155
}
3256

33-
/// Collection related operations.
34-
pub fn collections(&self) -> CollectionsClient {
35-
CollectionsClient {
36-
client: self.client.clone(),
37-
}
38-
}
39-
4057
/// Folder related operations.
4158
pub fn folders(&self) -> FoldersClient {
4259
FoldersClient {
4360
client: self.client.clone(),
4461
}
4562
}
4663

47-
/// Password history related operations.
48-
pub fn password_history(&self) -> PasswordHistoryClient {
49-
PasswordHistoryClient {
64+
/// TOTP related operations.
65+
pub fn totp(&self) -> TotpClient {
66+
TotpClient {
5067
client: self.client.clone(),
5168
}
5269
}
53-
54-
pub async fn sync(&self, input: &SyncRequest) -> Result<SyncResponse, SyncError> {
55-
sync(&self.client, input).await
56-
}
5770
}
5871

5972
pub trait VaultClientExt {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use bitwarden_core::{Client, ClientSettings};
55
use bitwarden_error::bitwarden_error;
66
use bitwarden_exporters::ExporterClientExt;
77
use bitwarden_generators::GeneratorClientsExt;
8-
use bitwarden_vault::VaultClientExt;
8+
use bitwarden_vault::{VaultClient, VaultClientExt};
99
use wasm_bindgen::prelude::*;
1010

11-
use crate::{CryptoClient, VaultClient};
11+
use crate::CryptoClient;
1212

1313
#[wasm_bindgen]
1414
pub struct BitwardenClient(pub(crate) Client);
@@ -46,7 +46,7 @@ impl BitwardenClient {
4646
}
4747

4848
pub fn vault(&self) -> VaultClient {
49-
VaultClient::new(self.0.vault())
49+
self.0.vault()
5050
}
5151

5252
/// Constructs a specific client for generating passwords and passphrases

crates/bitwarden-wasm-internal/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ mod custom_types;
66
mod init;
77
mod pure_crypto;
88
mod ssh;
9-
mod vault;
109

1110
pub use bitwarden_ipc::wasm::*;
1211
pub use client::BitwardenClient;
1312
pub use crypto::CryptoClient;
1413
pub use init::init_sdk;
15-
pub use vault::VaultClient;

crates/bitwarden-wasm-internal/src/vault/attachments.rs

Lines changed: 0 additions & 24 deletions
This file was deleted.

crates/bitwarden-wasm-internal/src/vault/mod.rs

Lines changed: 0 additions & 35 deletions
This file was deleted.

crates/bitwarden-wasm-internal/src/vault/totp.rs

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)