Skip to content

Commit ab3c7db

Browse files
authored
[PM-13374] Add UserId, CipherId, FolderId (#293)
Continues the work on #225 by introducing `UserId`, `CipherId` and `FolderId`. It also propagates the `OrganizationId` deeper into the core crate. - Renamed `uuid` macro to `uuid_newtype`, avoids collision with the macro from `uuid` crate. - Added `Hash` to the uuids. - Added `new_v4` primarily useful in tests to generate valid UUID v4 based IDs.
1 parent a3d1c11 commit ab3c7db

File tree

38 files changed

+186
-154
lines changed

38 files changed

+186
-154
lines changed

Cargo.lock

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

bitwarden_license/bitwarden-sm/src/projects/create.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use bitwarden_api_api::models::ProjectCreateRequestModel;
2-
use bitwarden_core::{key_management::SymmetricKeyId, Client};
2+
use bitwarden_core::{key_management::SymmetricKeyId, Client, OrganizationId};
33
use bitwarden_crypto::PrimitiveEncryptable;
44
use schemars::JsonSchema;
55
use serde::{Deserialize, Serialize};
@@ -28,7 +28,7 @@ pub(crate) async fn create_project(
2828
input.validate()?;
2929

3030
let key_store = client.internal.get_key_store();
31-
let key = SymmetricKeyId::Organization(input.organization_id);
31+
let key = SymmetricKeyId::Organization(OrganizationId::new(input.organization_id));
3232

3333
let project = Some(ProjectCreateRequestModel {
3434
name: input

bitwarden_license/bitwarden-sm/src/projects/project_response.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use bitwarden_api_api::models::ProjectResponseModel;
22
use bitwarden_core::{
33
key_management::{KeyIds, SymmetricKeyId},
4-
require,
4+
require, OrganizationId,
55
};
66
use bitwarden_crypto::{Decryptable, EncString, KeyStoreContext};
77
use chrono::{DateTime, Utc};
@@ -28,7 +28,7 @@ impl ProjectResponse {
2828
ctx: &mut KeyStoreContext<KeyIds>,
2929
) -> Result<Self, SecretsManagerError> {
3030
let organization_id = require!(response.organization_id);
31-
let key = SymmetricKeyId::Organization(organization_id);
31+
let key = SymmetricKeyId::Organization(OrganizationId::new(organization_id));
3232

3333
let name = require!(response.name)
3434
.parse::<EncString>()?

bitwarden_license/bitwarden-sm/src/projects/update.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use bitwarden_api_api::models::ProjectUpdateRequestModel;
2-
use bitwarden_core::{key_management::SymmetricKeyId, Client};
2+
use bitwarden_core::{key_management::SymmetricKeyId, Client, OrganizationId};
33
use bitwarden_crypto::PrimitiveEncryptable;
44
use schemars::JsonSchema;
55
use serde::{Deserialize, Serialize};
@@ -30,7 +30,7 @@ pub(crate) async fn update_project(
3030
input.validate()?;
3131

3232
let key_store = client.internal.get_key_store();
33-
let key = SymmetricKeyId::Organization(input.organization_id);
33+
let key = SymmetricKeyId::Organization(OrganizationId::new(input.organization_id));
3434

3535
let project = Some(ProjectUpdateRequestModel {
3636
name: input

bitwarden_license/bitwarden-sm/src/secrets/create.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use bitwarden_api_api::models::SecretCreateRequestModel;
2-
use bitwarden_core::{key_management::SymmetricKeyId, Client};
2+
use bitwarden_core::{key_management::SymmetricKeyId, Client, OrganizationId};
33
use bitwarden_crypto::PrimitiveEncryptable;
44
use schemars::JsonSchema;
55
use serde::{Deserialize, Serialize};
@@ -36,7 +36,7 @@ pub(crate) async fn create_secret(
3636
input.validate()?;
3737

3838
let key_store = client.internal.get_key_store();
39-
let key = SymmetricKeyId::Organization(input.organization_id);
39+
let key = SymmetricKeyId::Organization(OrganizationId::new(input.organization_id));
4040

4141
let secret = {
4242
let mut ctx = key_store.context();

bitwarden_license/bitwarden-sm/src/secrets/list.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bitwarden_api_api::models::{
44
use bitwarden_core::{
55
client::Client,
66
key_management::{KeyIds, SymmetricKeyId},
7-
require,
7+
require, OrganizationId,
88
};
99
use bitwarden_crypto::{Decryptable, EncString, KeyStoreContext};
1010
use schemars::JsonSchema;
@@ -99,7 +99,7 @@ impl SecretIdentifierResponse {
9999
ctx: &mut KeyStoreContext<KeyIds>,
100100
) -> Result<SecretIdentifierResponse, SecretsManagerError> {
101101
let organization_id = require!(response.organization_id);
102-
let enc_key = SymmetricKeyId::Organization(organization_id);
102+
let enc_key = SymmetricKeyId::Organization(OrganizationId::new(organization_id));
103103

104104
let key = require!(response.key)
105105
.parse::<EncString>()?

bitwarden_license/bitwarden-sm/src/secrets/secret_response.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use bitwarden_api_api::models::{
33
};
44
use bitwarden_core::{
55
key_management::{KeyIds, SymmetricKeyId},
6-
require,
6+
require, OrganizationId,
77
};
88
use bitwarden_crypto::{Decryptable, EncString, KeyStoreContext};
99
use chrono::{DateTime, Utc};
@@ -52,7 +52,7 @@ impl SecretResponse {
5252
ctx: &mut KeyStoreContext<KeyIds>,
5353
) -> Result<SecretResponse, SecretsManagerError> {
5454
let organization_id = require!(response.organization_id);
55-
let enc_key = SymmetricKeyId::Organization(organization_id);
55+
let enc_key = SymmetricKeyId::Organization(OrganizationId::new(organization_id));
5656

5757
let key = require!(response.key)
5858
.parse::<EncString>()?

bitwarden_license/bitwarden-sm/src/secrets/update.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use bitwarden_api_api::models::SecretUpdateRequestModel;
2-
use bitwarden_core::{key_management::SymmetricKeyId, Client};
2+
use bitwarden_core::{key_management::SymmetricKeyId, Client, OrganizationId};
33
use bitwarden_crypto::PrimitiveEncryptable;
44
use schemars::JsonSchema;
55
use serde::{Deserialize, Serialize};
@@ -35,7 +35,7 @@ pub(crate) async fn update_secret(
3535
input.validate()?;
3636

3737
let key_store = client.internal.get_key_store();
38-
let key = SymmetricKeyId::Organization(input.organization_id);
38+
let key = SymmetricKeyId::Organization(OrganizationId::new(input.organization_id));
3939

4040
let secret = {
4141
let mut ctx = key_store.context();

crates/bitwarden-collections/Cargo.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,14 @@ uniffi = [
1616
"bitwarden-crypto/uniffi",
1717
"dep:uniffi"
1818
] # Uniffi bindings
19-
wasm = [
20-
"bitwarden-core/wasm",
21-
"dep:tsify",
22-
"dep:wasm-bindgen"
23-
] # WASM support
19+
wasm = ["bitwarden-core/wasm", "dep:tsify", "dep:wasm-bindgen"] # WASM support
2420

2521
[dependencies]
2622
bitwarden-api-api = { workspace = true }
2723
bitwarden-core = { workspace = true, features = ["internal"] }
2824
bitwarden-crypto = { workspace = true }
2925
bitwarden-error = { workspace = true }
26+
bitwarden-uuid = { workspace = true }
3027
serde = { workspace = true }
3128
serde_repr = { workspace = true }
3229
thiserror = { workspace = true }

crates/bitwarden-collections/src/collection.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use bitwarden_api_api::models::CollectionDetailsResponseModel;
22
use bitwarden_core::{
33
key_management::{KeyIds, SymmetricKeyId},
4-
require,
4+
require, OrganizationId,
55
};
66
use bitwarden_crypto::{CryptoError, Decryptable, EncString, IdentifyKey, KeyStoreContext};
7+
use bitwarden_uuid::uuid_newtype;
78
use serde::{Deserialize, Serialize};
89
use serde_repr::{Deserialize_repr, Serialize_repr};
910
use uuid::Uuid;
@@ -12,14 +13,16 @@ use {tsify::Tsify, wasm_bindgen::prelude::*};
1213

1314
use crate::{error::CollectionsParseError, tree::TreeItem};
1415

16+
uuid_newtype!(pub CollectionId);
17+
1518
#[allow(missing_docs)]
1619
#[derive(Serialize, Deserialize, Debug)]
1720
#[serde(rename_all = "camelCase", deny_unknown_fields)]
1821
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
1922
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
2023
pub struct Collection {
21-
pub id: Option<Uuid>,
22-
pub organization_id: Uuid,
24+
pub id: Option<CollectionId>,
25+
pub organization_id: OrganizationId,
2326
pub name: EncString,
2427
pub external_id: Option<String>,
2528
pub hide_passwords: bool,
@@ -35,8 +38,8 @@ pub struct Collection {
3538
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
3639
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
3740
pub struct CollectionView {
38-
pub id: Option<Uuid>,
39-
pub organization_id: Uuid,
41+
pub id: Option<CollectionId>,
42+
pub organization_id: OrganizationId,
4043
pub name: String,
4144
pub external_id: Option<String>,
4245
pub hide_passwords: bool,
@@ -94,8 +97,8 @@ impl TryFrom<CollectionDetailsResponseModel> for Collection {
9497

9598
fn try_from(collection: CollectionDetailsResponseModel) -> Result<Self, Self::Error> {
9699
Ok(Collection {
97-
id: collection.id,
98-
organization_id: require!(collection.organization_id),
100+
id: collection.id.map(CollectionId::new),
101+
organization_id: OrganizationId::new(require!(collection.organization_id)),
99102
name: require!(collection.name).parse()?,
100103
external_id: collection.external_id,
101104
hide_passwords: collection.hide_passwords.unwrap_or(false),
@@ -117,7 +120,7 @@ impl IdentifyKey<SymmetricKeyId> for Collection {
117120
#[allow(missing_docs)]
118121
impl TreeItem for CollectionView {
119122
fn id(&self) -> Uuid {
120-
self.id.unwrap_or_default()
123+
self.id.map(|id| id.0).unwrap_or_default()
121124
}
122125

123126
fn short_name(&self) -> &str {
@@ -149,7 +152,6 @@ impl From<bitwarden_api_api::models::CollectionType> for CollectionType {
149152
mod tests {
150153
use bitwarden_core::key_management::{KeyIds, SymmetricKeyId};
151154
use bitwarden_crypto::{KeyStore, PrimitiveEncryptable, SymmetricCryptoKey};
152-
use uuid::Uuid;
153155

154156
use super::*;
155157

@@ -160,7 +162,7 @@ mod tests {
160162
fn create_test_key_store() -> KeyStore<KeyIds> {
161163
let store = KeyStore::<KeyIds>::default();
162164
let key = SymmetricCryptoKey::make_aes256_cbc_hmac_key();
163-
let org_id = Uuid::parse_str(ORGANIZATION_ID).unwrap();
165+
let org_id = ORGANIZATION_ID.parse().unwrap();
164166

165167
#[allow(deprecated)]
166168
store
@@ -175,13 +177,13 @@ mod tests {
175177
fn test_decrypt_with_name_only() {
176178
let store = create_test_key_store();
177179
let mut ctx = store.context();
178-
let org_id = Uuid::parse_str(ORGANIZATION_ID).unwrap();
180+
let org_id = ORGANIZATION_ID.parse().unwrap();
179181
let key = SymmetricKeyId::Organization(org_id);
180182

181183
let collection_name: &str = "Collection Name";
182184

183185
let collection = Collection {
184-
id: Some(Uuid::parse_str(COLLECTION_ID).unwrap()),
186+
id: Some(COLLECTION_ID.parse().unwrap()),
185187
organization_id: org_id,
186188
name: collection_name.encrypt(&mut ctx, key).unwrap(),
187189
external_id: Some("external-id".to_string()),
@@ -201,14 +203,14 @@ mod tests {
201203
fn test_decrypt_with_default_user_collection_email() {
202204
let store = create_test_key_store();
203205
let mut ctx = store.context();
204-
let org_id = Uuid::parse_str(ORGANIZATION_ID).unwrap();
206+
let org_id = ORGANIZATION_ID.parse().unwrap();
205207
let key = SymmetricKeyId::Organization(org_id);
206208

207209
let collection_name: &str = "Collection Name";
208210
let default_user_collection_email = String::from("[email protected]");
209211

210212
let collection = Collection {
211-
id: Some(Uuid::parse_str(COLLECTION_ID).unwrap()),
213+
id: Some(COLLECTION_ID.parse().unwrap()),
212214
organization_id: org_id,
213215
name: collection_name.encrypt(&mut ctx, key).unwrap(),
214216
external_id: None,
@@ -229,10 +231,10 @@ mod tests {
229231
fn test_decrypt_all_fields_preserved() {
230232
let store = create_test_key_store();
231233
let mut ctx = store.context();
232-
let org_id = Uuid::parse_str(ORGANIZATION_ID).unwrap();
234+
let org_id = ORGANIZATION_ID.parse().unwrap();
233235
let key = SymmetricKeyId::Organization(org_id);
234236

235-
let collection_id = Some(Uuid::parse_str(COLLECTION_ID).unwrap());
237+
let collection_id = Some(COLLECTION_ID.parse().unwrap());
236238
let external_id = Some("external-test-id".to_string());
237239
let collection_name: &str = "Collection Name";
238240
let collection_type = CollectionType::SharedCollection;

0 commit comments

Comments
 (0)