Skip to content

Commit 7269f31

Browse files
authored
Remove duplicated Cipher and Folder client (#225)
Changes `uuid` to be represented as unknown which causes most `as` casts to fail since TS safeguards against casting "string" to unknown. Adds a new type called `OrganizationId` which is manually set to be equal to `Tagged<Uuid, "OrganizationId">` on the TS side. This prevents mixing up the type with other Uuid's. In order to create an OrganizationId in the application you now need to pass through unknown otherwise you get the following error message: `Conversion of type 'string' to type 'Tag<"OrganizationId", never>' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.` To provide some ergonomics we should add the following function in typescript: ``` export function uuid<T extends Uuid>(uuid: string): T { if (Utils.isGuid(uuid)) { return uuid as T; } throw new Error(`Invalid UUID: ${uuid}`); } ```
1 parent 82595fe commit 7269f31

File tree

27 files changed

+477
-370
lines changed

27 files changed

+477
-370
lines changed

.github/workflows/build-wasm-internal.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ jobs:
6262
6363
- name: NPM setup
6464
run: npm ci
65+
working-directory: crates/bitwarden-wasm-internal/npm
6566

6667
- name: Install rust
6768
uses: dtolnay/rust-toolchain@56f84321dbccf38fb67ce29ab63e4754056677e0 # stable

Cargo.lock

Lines changed: 33 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ bitwarden-api-identity = { path = "crates/bitwarden-api-identity", version = "=1
2424
bitwarden-cli = { path = "crates/bitwarden-cli", version = "=1.0.0" }
2525
bitwarden-core = { path = "crates/bitwarden-core", version = "=1.0.0" }
2626
bitwarden-crypto = { path = "crates/bitwarden-crypto", version = "=1.0.0" }
27+
bitwarden-error = { path = "crates/bitwarden-error", version = "=1.0.0" }
28+
bitwarden-error-macro = { path = "crates/bitwarden-error-macro", version = "=1.0.0" }
2729
bitwarden-exporters = { path = "crates/bitwarden-exporters", version = "=1.0.0" }
2830
bitwarden-fido = { path = "crates/bitwarden-fido", version = "=1.0.0" }
2931
bitwarden-generators = { path = "crates/bitwarden-generators", version = "=1.0.0" }
@@ -32,17 +34,20 @@ bitwarden-send = { path = "crates/bitwarden-send", version = "=1.0.0" }
3234
bitwarden-threading = { path = "crates/bitwarden-threading", version = "=1.0.0" }
3335
bitwarden-sm = { path = "bitwarden_license/bitwarden-sm", version = "=1.0.0" }
3436
bitwarden-ssh = { path = "crates/bitwarden-ssh", version = "=1.0.0" }
37+
bitwarden-uuid = { path = "crates/bitwarden-uuid", version = "=1.0.0" }
38+
bitwarden-uuid-macro = { path = "crates/bitwarden-uuid-macro", version = "=1.0.0" }
3539
bitwarden-vault = { path = "crates/bitwarden-vault", version = "=1.0.0" }
36-
bitwarden-error = { path = "crates/bitwarden-error", version = "=1.0.0" }
37-
bitwarden-error-macro = { path = "crates/bitwarden-error-macro", version = "=1.0.0" }
3840

3941
# External crates that are expected to maintain a consistent version across all crates
4042
chrono = { version = ">=0.4.26, <0.5", features = [
4143
"clock",
4244
"serde",
4345
"std",
4446
], default-features = false }
47+
js-sys = { version = ">=0.3.72, <0.4" }
4548
log = ">=0.4.18, <0.5"
49+
proc-macro2 = ">=1.0.89, <2"
50+
quote = ">=1.0.37, <2"
4651
reqwest = { version = ">=0.12.5, <0.13", features = [
4752
"json",
4853
"multipart",
@@ -53,6 +58,7 @@ serde = { version = ">=1.0, <2.0", features = ["derive"] }
5358
serde_json = ">=1.0.96, <2.0"
5459
serde_qs = ">=0.12.0, <0.16"
5560
serde_repr = ">=0.1.12, <0.2"
61+
syn = ">=2.0.87, <3"
5662
thiserror = ">=1.0.40, <3"
5763
tokio = { version = "1.36.0", features = ["macros"] }
5864
tsify-next = { version = ">=0.5.4, <0.6", features = [
@@ -62,7 +68,6 @@ uniffi = "=0.29.1"
6268
uuid = { version = ">=1.3.3, <2.0", features = ["serde", "v4", "js"] }
6369
validator = { version = ">=0.18.1, <0.20", features = ["derive"] }
6470
wasm-bindgen = { version = ">=0.2.91, <0.3", features = ["serde-serialize"] }
65-
js-sys = { version = ">=0.3.72, <0.4" }
6671
wasm-bindgen-futures = "0.4.41"
6772
wasm-bindgen-test = "0.3.45"
6873

crates/bitwarden-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ bitwarden-api-api = { workspace = true }
3333
bitwarden-api-identity = { workspace = true }
3434
bitwarden-crypto = { workspace = true }
3535
bitwarden-error = { workspace = true }
36+
bitwarden-uuid = { workspace = true }
3637
chrono = { workspace = true, features = ["std"] }
3738
# We don't use this directly (it's used by rand), but we need it here to enable WASM support
3839
getrandom = { version = ">=0.2.9, <0.3", features = ["js"] }

crates/bitwarden-core/src/ids.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
use bitwarden_uuid::uuid;
2+
3+
uuid!(pub OrganizationId);

crates/bitwarden-core/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@ mod util;
2525

2626
pub use bitwarden_crypto::ZeroizingAllocator;
2727
pub use client::{Client, ClientSettings, DeviceType};
28+
29+
mod ids;
30+
pub use ids::*;

crates/bitwarden-error-macro/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ wasm = []
1919

2020
[dependencies]
2121
darling = "0.20.10"
22-
proc-macro2 = "1.0.89"
23-
quote = "1.0.37"
24-
syn = "2.0.87"
22+
proc-macro2 = { workspace = true }
23+
quote = { workspace = true }
24+
syn = { workspace = true }
2525

2626
[lints]
2727
workspace = true

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use bitwarden_core::OrganizationId;
12
use bitwarden_vault::{Cipher, CipherListView, CipherView, EncryptionContext, Fido2CredentialView};
23
use uuid::Uuid;
34

@@ -41,7 +42,7 @@ impl CiphersClient {
4142
) -> Result<CipherView> {
4243
Ok(self
4344
.0
44-
.move_to_organization(cipher, organization_id)
45+
.move_to_organization(cipher, OrganizationId::new(organization_id))
4546
.map_err(Error::Cipher)?)
4647
}
4748
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[package]
2+
name = "bitwarden-uuid-macro"
3+
description = """
4+
Internal crate for the bitwarden crate. Do not use.
5+
"""
6+
7+
version.workspace = true
8+
authors.workspace = true
9+
edition.workspace = true
10+
rust-version.workspace = true
11+
readme.workspace = true
12+
homepage.workspace = true
13+
repository.workspace = true
14+
license-file.workspace = true
15+
keywords.workspace = true
16+
17+
[dependencies]
18+
proc-macro2 = { workspace = true }
19+
quote = { workspace = true }
20+
syn = { workspace = true }
21+
22+
[lints]
23+
workspace = true
24+
25+
[lib]
26+
proc-macro = true

crates/bitwarden-uuid-macro/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Bitwarden UUID Macro
2+
3+
Provides uuid macros for simplifying UUID handling when working with WebAssembly.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#![doc = include_str!("../README.md")]
2+
3+
use proc_macro::TokenStream;
4+
use quote::quote;
5+
use syn::{
6+
parse::{Parse, ParseStream},
7+
parse_macro_input, Ident, Visibility,
8+
};
9+
10+
#[proc_macro]
11+
pub fn uuid(input: TokenStream) -> TokenStream {
12+
// Parse input as: vis ident
13+
let input = parse_macro_input!(input as IdTypeInput);
14+
let ident = input.ident;
15+
let vis = input.vis;
16+
let name_str = ident.to_string();
17+
18+
let tsify_type = format!("Tagged<Uuid, \"{}\">", name_str);
19+
let doc_string = format!(" NewType wrapper for `{}`", name_str);
20+
21+
let expanded = quote! {
22+
#[doc = #doc_string]
23+
#[cfg_attr(feature = "wasm", derive(::tsify_next::Tsify), tsify(into_wasm_abi, from_wasm_abi))]
24+
#[derive(
25+
::serde::Serialize, ::serde::Deserialize,
26+
::std::cmp::PartialEq, ::std::cmp::Eq,
27+
::std::clone::Clone, ::std::marker::Copy, ::std::fmt::Debug
28+
)]
29+
#[repr(transparent)]
30+
#vis struct #ident
31+
(
32+
#[cfg_attr(feature = "wasm", tsify(type = #tsify_type))]
33+
::uuid::Uuid
34+
);
35+
36+
#[cfg(feature = "uniffi")]
37+
uniffi::custom_newtype!(#ident, uuid::Uuid);
38+
39+
impl #ident {
40+
pub fn new(value: uuid::Uuid) -> Self {
41+
Self(value)
42+
}
43+
}
44+
45+
impl ::std::str::FromStr for #ident {
46+
type Err = uuid::Error;
47+
48+
fn from_str(s: &str) -> Result<Self, Self::Err> {
49+
uuid::Uuid::from_str(s).map(Self)
50+
}
51+
}
52+
53+
impl From<#ident> for ::uuid::Uuid {
54+
fn from(value: #ident) -> Self {
55+
value.0
56+
}
57+
}
58+
};
59+
60+
TokenStream::from(expanded)
61+
}
62+
63+
// Helper struct to parse "vis ident"
64+
struct IdTypeInput {
65+
vis: Visibility,
66+
ident: Ident,
67+
}
68+
69+
impl Parse for IdTypeInput {
70+
fn parse(input: ParseStream) -> syn::Result<Self> {
71+
let vis: Visibility = input.parse()?;
72+
let ident: Ident = input.parse()?;
73+
Ok(IdTypeInput { vis, ident })
74+
}
75+
}

crates/bitwarden-uuid/Cargo.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[package]
2+
name = "bitwarden-uuid"
3+
description = """
4+
Internal crate for the bitwarden crate. Do not use.
5+
"""
6+
7+
version.workspace = true
8+
authors.workspace = true
9+
edition.workspace = true
10+
rust-version.workspace = true
11+
readme.workspace = true
12+
homepage.workspace = true
13+
repository.workspace = true
14+
license-file.workspace = true
15+
keywords.workspace = true
16+
17+
[dependencies]
18+
bitwarden-uuid-macro = { workspace = true }
19+
20+
[lints]
21+
workspace = true
22+
23+
[dev-dependencies]
24+
serde = { workspace = true }
25+
serde_json = { workspace = true }
26+
uuid = { workspace = true }

crates/bitwarden-uuid/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Bitwarden UUID
2+
3+
Provides UUID macros for simplifying UUID handling when working with WebAssembly.

crates/bitwarden-uuid/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#![doc = include_str!("../README.md")]
2+
3+
pub use bitwarden_uuid_macro::uuid;

0 commit comments

Comments
 (0)