From a1201f1ee0b9051c916ebdbc49629f1690b81762 Mon Sep 17 00:00:00 2001 From: Andreas Coroiu Date: Wed, 9 Oct 2024 11:25:06 +0200 Subject: [PATCH] feat: add support for decrypting folders --- Cargo.lock | 3 +++ crates/bitwarden-vault/Cargo.toml | 3 +++ crates/bitwarden-vault/src/folder.rs | 5 +++++ crates/bitwarden-wasm-internal/Cargo.toml | 1 + crates/bitwarden-wasm-internal/src/client.rs | 6 +++++- crates/bitwarden-wasm-internal/src/lib.rs | 4 ++++ .../src/vault/folders.rs | 20 +++++++++++++++++++ .../bitwarden-wasm-internal/src/vault/mod.rs | 18 +++++++++++++++++ 8 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 crates/bitwarden-wasm-internal/src/vault/folders.rs create mode 100644 crates/bitwarden-wasm-internal/src/vault/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 07b8fed77..397a34230 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -666,8 +666,10 @@ dependencies = [ "sha2", "thiserror", "tokio", + "tsify-next", "uniffi", "uuid", + "wasm-bindgen", ] [[package]] @@ -694,6 +696,7 @@ dependencies = [ "bitwarden", "bitwarden-core", "bitwarden-crypto", + "bitwarden-vault", "console_error_panic_hook", "console_log", "js-sys", diff --git a/crates/bitwarden-vault/Cargo.toml b/crates/bitwarden-vault/Cargo.toml index 4c41e139c..3c7757e2f 100644 --- a/crates/bitwarden-vault/Cargo.toml +++ b/crates/bitwarden-vault/Cargo.toml @@ -19,6 +19,7 @@ uniffi = [ "bitwarden-crypto/uniffi", "dep:uniffi", ] # Uniffi bindings +wasm = ["dep:tsify-next", "dep:wasm-bindgen"] # WASM support [dependencies] base64 = ">=0.22.1, <0.23" @@ -38,6 +39,8 @@ sha2 = ">=0.10.6, <0.11" thiserror = { workspace = true } uniffi = { version = "=0.28.1", optional = true } uuid = { workspace = true } +tsify-next = { workspace = true, optional = true } +wasm-bindgen = { workspace = true, optional = true } [dev-dependencies] tokio = { workspace = true, features = ["rt"] } diff --git a/crates/bitwarden-vault/src/folder.rs b/crates/bitwarden-vault/src/folder.rs index c5ab1401b..a1ef65601 100644 --- a/crates/bitwarden-vault/src/folder.rs +++ b/crates/bitwarden-vault/src/folder.rs @@ -10,9 +10,13 @@ use uuid::Uuid; use crate::VaultParseError; +#[cfg(feature = "wasm")] +use {tsify_next::Tsify, wasm_bindgen::prelude::*}; + #[derive(Serialize, Deserialize, Debug, JsonSchema)] #[serde(rename_all = "camelCase")] #[cfg_attr(feature = "uniffi", derive(uniffi::Record))] +#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))] pub struct Folder { id: Option, name: EncString, @@ -22,6 +26,7 @@ pub struct Folder { #[derive(Serialize, Deserialize, Debug, JsonSchema)] #[serde(rename_all = "camelCase")] #[cfg_attr(feature = "uniffi", derive(uniffi::Record))] +#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))] pub struct FolderView { pub id: Option, pub name: String, diff --git a/crates/bitwarden-wasm-internal/Cargo.toml b/crates/bitwarden-wasm-internal/Cargo.toml index 3ead75b1b..522526984 100644 --- a/crates/bitwarden-wasm-internal/Cargo.toml +++ b/crates/bitwarden-wasm-internal/Cargo.toml @@ -18,6 +18,7 @@ crate-type = ["cdylib"] bitwarden = { workspace = true, features = ["internal", "wasm"] } bitwarden-core = { workspace = true, features = ["wasm"] } bitwarden-crypto = { workspace = true, features = ["wasm"] } +bitwarden-vault = { workspace = true, features = ["wasm"] } console_error_panic_hook = "0.1.7" console_log = { version = "1.0.0", features = ["color"] } js-sys = "0.3.68" diff --git a/crates/bitwarden-wasm-internal/src/client.rs b/crates/bitwarden-wasm-internal/src/client.rs index e6fb51cec..ea3210c68 100644 --- a/crates/bitwarden-wasm-internal/src/client.rs +++ b/crates/bitwarden-wasm-internal/src/client.rs @@ -5,7 +5,7 @@ use bitwarden::{Client, ClientSettings}; use log::{set_max_level, Level}; use wasm_bindgen::prelude::*; -use crate::ClientCrypto; +use crate::{vault::ClientVault, ClientCrypto}; #[wasm_bindgen] pub enum LogLevel { @@ -60,4 +60,8 @@ impl BitwardenClient { pub fn crypto(&self) -> ClientCrypto { ClientCrypto(self.0.clone()) } + + pub fn vault(&self) -> ClientVault { + ClientVault(self.0.clone()) + } } diff --git a/crates/bitwarden-wasm-internal/src/lib.rs b/crates/bitwarden-wasm-internal/src/lib.rs index a3da0bdc3..e055b9373 100644 --- a/crates/bitwarden-wasm-internal/src/lib.rs +++ b/crates/bitwarden-wasm-internal/src/lib.rs @@ -1,6 +1,10 @@ mod client; mod crypto; +mod custom_types; mod error; +mod vault; pub use client::BitwardenClient; pub use crypto::ClientCrypto; +pub use vault::folders::ClientFolders; +pub use vault::ClientVault; diff --git a/crates/bitwarden-wasm-internal/src/vault/folders.rs b/crates/bitwarden-wasm-internal/src/vault/folders.rs new file mode 100644 index 000000000..a85709e0f --- /dev/null +++ b/crates/bitwarden-wasm-internal/src/vault/folders.rs @@ -0,0 +1,20 @@ +use std::rc::Rc; + +use bitwarden::{ + vault::{ClientVaultExt, Folder, FolderView}, + Client, +}; +use wasm_bindgen::prelude::*; + +use crate::error::Result; + +#[wasm_bindgen] +pub struct ClientFolders(pub(crate) Rc); + +#[wasm_bindgen] +impl ClientFolders { + /// Decrypt folder + pub fn decrypt(&self, folder: Folder) -> Result { + Ok(self.0.vault().folders().decrypt(folder)?) + } +} diff --git a/crates/bitwarden-wasm-internal/src/vault/mod.rs b/crates/bitwarden-wasm-internal/src/vault/mod.rs new file mode 100644 index 000000000..0619ab8c2 --- /dev/null +++ b/crates/bitwarden-wasm-internal/src/vault/mod.rs @@ -0,0 +1,18 @@ +pub mod folders; + +use std::rc::Rc; + +use bitwarden::Client; +use wasm_bindgen::prelude::*; + +use crate::ClientFolders; + +#[wasm_bindgen] +pub struct ClientVault(pub(crate) Rc); + +#[wasm_bindgen] +impl ClientVault { + pub fn folders(&self) -> ClientFolders { + ClientFolders(self.0.clone()) + } +}