Skip to content

Remove duplicated Cipher and Folder client #225

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 20 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from 18 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
1 change: 1 addition & 0 deletions .github/workflows/build-wasm-internal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ jobs:

- name: NPM setup
run: npm ci
working-directory: crates/bitwarden-wasm-internal/npm

- name: Install rust
uses: dtolnay/rust-toolchain@56f84321dbccf38fb67ce29ab63e4754056677e0 # stable
Expand Down
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ bitwarden-api-identity = { path = "crates/bitwarden-api-identity", version = "=1
bitwarden-cli = { path = "crates/bitwarden-cli", version = "=1.0.0" }
bitwarden-core = { path = "crates/bitwarden-core", version = "=1.0.0" }
bitwarden-crypto = { path = "crates/bitwarden-crypto", version = "=1.0.0" }
bitwarden-error = { path = "crates/bitwarden-error", version = "=1.0.0" }
bitwarden-error-macro = { path = "crates/bitwarden-error-macro", version = "=1.0.0" }
bitwarden-exporters = { path = "crates/bitwarden-exporters", version = "=1.0.0" }
bitwarden-fido = { path = "crates/bitwarden-fido", version = "=1.0.0" }
bitwarden-generators = { path = "crates/bitwarden-generators", version = "=1.0.0" }
Expand All @@ -32,17 +34,20 @@ bitwarden-send = { path = "crates/bitwarden-send", version = "=1.0.0" }
bitwarden-threading = { path = "crates/bitwarden-threading", version = "=1.0.0" }
bitwarden-sm = { path = "bitwarden_license/bitwarden-sm", version = "=1.0.0" }
bitwarden-ssh = { path = "crates/bitwarden-ssh", version = "=1.0.0" }
bitwarden-uuid = { path = "crates/bitwarden-uuid", version = "=1.0.0" }
bitwarden-uuid-macro = { path = "crates/bitwarden-uuid-macro", version = "=1.0.0" }
bitwarden-vault = { path = "crates/bitwarden-vault", version = "=1.0.0" }
bitwarden-error = { path = "crates/bitwarden-error", version = "=1.0.0" }
bitwarden-error-macro = { path = "crates/bitwarden-error-macro", version = "=1.0.0" }

# External crates that are expected to maintain a consistent version across all crates
chrono = { version = ">=0.4.26, <0.5", features = [
"clock",
"serde",
"std",
], default-features = false }
js-sys = { version = ">=0.3.72, <0.4" }
log = ">=0.4.18, <0.5"
proc-macro2 = ">=1.0.89, <2"
quote = ">=1.0.37, <2"
reqwest = { version = ">=0.12.5, <0.13", features = [
"json",
"multipart",
Expand All @@ -53,6 +58,7 @@ serde = { version = ">=1.0, <2.0", features = ["derive"] }
serde_json = ">=1.0.96, <2.0"
serde_qs = ">=0.12.0, <0.16"
serde_repr = ">=0.1.12, <0.2"
syn = ">=2.0.87, <3"
thiserror = ">=1.0.40, <3"
tokio = { version = "1.36.0", features = ["macros"] }
tsify-next = { version = ">=0.5.4, <0.6", features = [
Expand All @@ -62,7 +68,6 @@ uniffi = "=0.28.3"
uuid = { version = ">=1.3.3, <2.0", features = ["serde", "v4", "js"] }
validator = { version = ">=0.18.1, <0.20", features = ["derive"] }
wasm-bindgen = { version = ">=0.2.91, <0.3", features = ["serde-serialize"] }
js-sys = { version = ">=0.3.72, <0.4" }
wasm-bindgen-futures = "0.4.41"
wasm-bindgen-test = "0.3.45"

Expand Down
1 change: 1 addition & 0 deletions crates/bitwarden-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ bitwarden-api-api = { workspace = true }
bitwarden-api-identity = { workspace = true }
bitwarden-crypto = { workspace = true }
bitwarden-error = { workspace = true }
bitwarden-uuid = { workspace = true }
chrono = { workspace = true, features = ["std"] }
# We don't use this directly (it's used by rand), but we need it here to enable WASM support
getrandom = { version = ">=0.2.9, <0.3", features = ["js"] }
Expand Down
3 changes: 3 additions & 0 deletions crates/bitwarden-core/src/ids.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use bitwarden_uuid::uuid;

uuid!(pub OrganizationId);
3 changes: 3 additions & 0 deletions crates/bitwarden-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ mod util;

pub use bitwarden_crypto::ZeroizingAllocator;
pub use client::{Client, ClientSettings, DeviceType};

mod ids;
pub use ids::*;
6 changes: 3 additions & 3 deletions crates/bitwarden-error-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ wasm = []

[dependencies]
darling = "0.20.10"
proc-macro2 = "1.0.89"
quote = "1.0.37"
syn = "2.0.87"
proc-macro2 = { workspace = true }
quote = { workspace = true }
syn = { workspace = true }

[lints]
workspace = true
Expand Down
3 changes: 2 additions & 1 deletion crates/bitwarden-uniffi/src/vault/ciphers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bitwarden_core::OrganizationId;
use bitwarden_vault::{Cipher, CipherListView, CipherView, EncryptionContext, Fido2CredentialView};
use uuid::Uuid;

Expand Down Expand Up @@ -41,7 +42,7 @@ impl CiphersClient {
) -> Result<CipherView> {
Ok(self
.0
.move_to_organization(cipher, organization_id)
.move_to_organization(cipher, OrganizationId::new(organization_id))
.map_err(Error::Cipher)?)
}
}
26 changes: 26 additions & 0 deletions crates/bitwarden-uuid-macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "bitwarden-uuid-macro"
description = """
Internal crate for the bitwarden crate. Do not use.
"""

version.workspace = true
authors.workspace = true
edition.workspace = true
rust-version.workspace = true
readme.workspace = true
homepage.workspace = true
repository.workspace = true
license-file.workspace = true
keywords.workspace = true

[dependencies]
proc-macro2 = { workspace = true }
quote = { workspace = true }
syn = { workspace = true }

[lints]
workspace = true

[lib]
proc-macro = true
69 changes: 69 additions & 0 deletions crates/bitwarden-uuid-macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{
parse::{Parse, ParseStream},
parse_macro_input, Ident, Visibility,
};

#[proc_macro]
pub fn uuid(input: TokenStream) -> TokenStream {
// Parse input as: vis ident
let input = parse_macro_input!(input as IdTypeInput);
let ident = input.ident;
let vis = input.vis;
let name_str = ident.to_string();

let tsify_type = format!("Tagged<Uuid, \"{}\">", name_str);
let doc_string = format!(" NewType wrapper for `{}`", name_str);

let expanded = quote! {
#[doc = #doc_string]
#[cfg_attr(feature = "wasm", derive(::tsify_next::Tsify), tsify(into_wasm_abi, from_wasm_abi))]
#[derive(::serde::Serialize, ::serde::Deserialize)]
#[repr(transparent)]
#vis struct #ident
(
#[cfg_attr(feature = "wasm", tsify(type = #tsify_type))]
::uuid::Uuid
);

#[cfg(feature = "uniffi")]
uniffi::custom_newtype!(#ident, uuid::Uuid);

impl #ident {
pub fn new(value: uuid::Uuid) -> Self {
Self(value)
}
}

impl ::std::str::FromStr for #ident {
type Err = uuid::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
uuid::Uuid::from_str(s).map(Self)
}
}

impl From<#ident> for ::uuid::Uuid {
fn from(value: #ident) -> Self {
value.0
}
}
};

TokenStream::from(expanded)
}

// Helper struct to parse "vis ident"
struct IdTypeInput {
vis: Visibility,
ident: Ident,
}

impl Parse for IdTypeInput {
fn parse(input: ParseStream) -> syn::Result<Self> {
let vis: Visibility = input.parse()?;
let ident: Ident = input.parse()?;
Ok(IdTypeInput { vis, ident })
}
}
25 changes: 25 additions & 0 deletions crates/bitwarden-uuid/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "bitwarden-uuid"
description = """
Internal crate for the bitwarden crate. Do not use.
"""

version.workspace = true
authors.workspace = true
edition.workspace = true
rust-version.workspace = true
readme.workspace = true
homepage.workspace = true
repository.workspace = true
license-file.workspace = true
keywords.workspace = true

[dependencies]
bitwarden-uuid-macro = { workspace = true }

[lints]
workspace = true

[dev-dependencies]
serde = { workspace = true }
uuid = { workspace = true }
1 change: 1 addition & 0 deletions crates/bitwarden-uuid/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub use bitwarden_uuid_macro::uuid;
25 changes: 25 additions & 0 deletions crates/bitwarden-uuid/tests/uuid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#![allow(unexpected_cfgs)]

use bitwarden_uuid::uuid;

uuid!(TestId);

#[test]
fn test_parse_string() {
use uuid::Uuid;

let id: TestId = "12345678-1234-5678-1234-567812345678".parse().unwrap();
let uuid: Uuid = id.into();

assert_eq!(uuid.to_string(), "12345678-1234-5678-1234-567812345678");
}

#[test]
fn test_new() {
use uuid::Uuid;

let uuid = Uuid::new_v4();
let id = TestId::new(uuid);

assert_eq!(uuid, Into::<Uuid>::into(id));
}
11 changes: 7 additions & 4 deletions crates/bitwarden-vault/src/cipher/cipher_client.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use bitwarden_core::Client;
use bitwarden_core::{Client, OrganizationId};
use bitwarden_crypto::IdentifyKey;
use uuid::Uuid;
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;

use super::EncryptionContext;
use crate::{
Cipher, CipherError, CipherListView, CipherView, DecryptError, EncryptError, VaultClient,
};

#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub struct CiphersClient {
pub(crate) client: Client,
}

#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl CiphersClient {
pub fn encrypt(&self, mut cipher_view: CipherView) -> Result<EncryptionContext, EncryptError> {
let user_id = self
Expand Down Expand Up @@ -64,10 +67,10 @@ impl CiphersClient {
pub fn move_to_organization(
&self,
mut cipher_view: CipherView,
organization_id: Uuid,
organization_id: OrganizationId,
) -> Result<CipherView, CipherError> {
let key_store = self.client.internal.get_key_store();
cipher_view.move_to_organization(&mut key_store.context(), organization_id)?;
cipher_view.move_to_organization(&mut key_store.context(), organization_id.into())?;
Ok(cipher_view)
}

Expand Down
4 changes: 4 additions & 0 deletions crates/bitwarden-vault/src/folder_client.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use bitwarden_core::Client;
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;

use crate::{
error::{DecryptError, EncryptError},
Folder, FolderView, VaultClient,
};

#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub struct FoldersClient {
pub(crate) client: Client,
}

#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl FoldersClient {
pub fn encrypt(&self, folder_view: FolderView) -> Result<Folder, EncryptError> {
let key_store = self.client.internal.get_key_store();
Expand Down
3 changes: 2 additions & 1 deletion crates/bitwarden-wasm-internal/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ wasm2js -Os ./crates/bitwarden-wasm-internal/npm/bitwarden_wasm_internal_bg.wasm
npx terser ./crates/bitwarden-wasm-internal/npm/bitwarden_wasm_internal_bg.wasm.js -o ./crates/bitwarden-wasm-internal/npm/bitwarden_wasm_internal_bg.wasm.js

# Typecheck the generated TypeScript definitions
npx tsc --noEmit --lib es2015 crates/bitwarden-wasm-internal/npm/bitwarden_wasm_internal.d.ts
cd crates/bitwarden-wasm-internal/npm
npx tsc --noEmit --lib es2020 bitwarden_wasm_internal.d.ts
45 changes: 45 additions & 0 deletions crates/bitwarden-wasm-internal/npm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading