Skip to content

[PM-22256] Add type and default_user_collection_email to collection #371

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 6 commits into from
Aug 11, 2025
Merged
Show file tree
Hide file tree
Changes from all 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 Cargo.lock

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

1 change: 1 addition & 0 deletions crates/bitwarden-collections/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ bitwarden-core = { workspace = true, features = ["internal"] }
bitwarden-crypto = { workspace = true }
bitwarden-error = { workspace = true }
serde = { workspace = true }
serde_repr = { workspace = true }
thiserror = { workspace = true }
tsify = { workspace = true, optional = true }
uniffi = { workspace = true, optional = true }
Expand Down
161 changes: 160 additions & 1 deletion crates/bitwarden-collections/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use bitwarden_core::{
};
use bitwarden_crypto::{CryptoError, Decryptable, EncString, IdentifyKey, KeyStoreContext};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use uuid::Uuid;
#[cfg(feature = "wasm")]
use {tsify::Tsify, wasm_bindgen::prelude::*};
Expand All @@ -24,6 +25,8 @@ pub struct Collection {
pub hide_passwords: bool,
pub read_only: bool,
pub manage: bool,
pub default_user_collection_email: Option<String>,
pub r#type: CollectionType,
}

#[allow(missing_docs)]
Expand All @@ -39,6 +42,24 @@ pub struct CollectionView {
pub hide_passwords: bool,
pub read_only: bool,
pub manage: bool,
pub r#type: CollectionType,
}

/// Type of collection
#[derive(Serialize_repr, Deserialize_repr, Debug, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[cfg_attr(
feature = "wasm",
derive(tsify::Tsify),
tsify(into_wasm_abi, from_wasm_abi)
)]
#[repr(u8)]
pub enum CollectionType {
/// Default collection type. Can be assigned by an organization to user(s) or group(s)
SharedCollection = 0,
/// Default collection assigned to a user for an organization that has
/// OrganizationDataOwnership (formerly PersonalOwnership) policy enabled.
DefaultUserCollection = 1,
}

#[allow(missing_docs)]
Expand All @@ -48,14 +69,21 @@ impl Decryptable<KeyIds, SymmetricKeyId, CollectionView> for Collection {
ctx: &mut KeyStoreContext<KeyIds>,
key: SymmetricKeyId,
) -> Result<CollectionView, CryptoError> {
let name = self
.default_user_collection_email
.as_ref()
.unwrap_or(&self.name.decrypt(ctx, key)?)
.clone();

Ok(CollectionView {
id: self.id,
organization_id: self.organization_id,
name: self.name.decrypt(ctx, key).ok().unwrap_or_default(),
name,
external_id: self.external_id.clone(),
hide_passwords: self.hide_passwords,
read_only: self.read_only,
manage: self.manage,
r#type: self.r#type.clone(),
})
}
}
Expand All @@ -73,6 +101,8 @@ impl TryFrom<CollectionDetailsResponseModel> for Collection {
hide_passwords: collection.hide_passwords.unwrap_or(false),
read_only: collection.read_only.unwrap_or(false),
manage: collection.manage.unwrap_or(false),
default_user_collection_email: collection.default_user_collection_email,
r#type: require!(collection.r#type).into(),
})
}
}
Expand Down Expand Up @@ -103,3 +133,132 @@ impl TreeItem for CollectionView {

const DELIMITER: char = '/';
}

impl From<bitwarden_api_api::models::CollectionType> for CollectionType {
fn from(collection_type: bitwarden_api_api::models::CollectionType) -> Self {
match collection_type {
bitwarden_api_api::models::CollectionType::SharedCollection => Self::SharedCollection,
bitwarden_api_api::models::CollectionType::DefaultUserCollection => {
Self::DefaultUserCollection
}
}
}
}

#[cfg(test)]
mod tests {
use bitwarden_core::key_management::{KeyIds, SymmetricKeyId};
use bitwarden_crypto::{KeyStore, PrimitiveEncryptable, SymmetricCryptoKey};
use uuid::Uuid;

use super::*;

const ORGANIZATION_ID: &str = "12345678-1234-1234-1234-123456789012";
const COLLECTION_ID: &str = "87654321-4321-4321-4321-210987654321";

// Helper function to create a test key store with a symmetric key
fn create_test_key_store() -> KeyStore<KeyIds> {
let store = KeyStore::<KeyIds>::default();
let key = SymmetricCryptoKey::make_aes256_cbc_hmac_key();
let org_id = Uuid::parse_str(ORGANIZATION_ID).unwrap();

#[allow(deprecated)]
store
.context_mut()
.set_symmetric_key(SymmetricKeyId::Organization(org_id), key)
.unwrap();

store
}

#[test]
fn test_decrypt_with_name_only() {
let store = create_test_key_store();
let mut ctx = store.context();
let org_id = Uuid::parse_str(ORGANIZATION_ID).unwrap();
let key = SymmetricKeyId::Organization(org_id);

let collection_name: &str = "Collection Name";

let collection = Collection {
id: Some(Uuid::parse_str(COLLECTION_ID).unwrap()),
organization_id: org_id,
name: collection_name.encrypt(&mut ctx, key).unwrap(),
external_id: Some("external-id".to_string()),
hide_passwords: true,
read_only: false,
manage: true,
default_user_collection_email: None,
r#type: CollectionType::SharedCollection,
};

let decrypted = collection.decrypt(&mut ctx, key).unwrap();

assert_eq!(decrypted.name, collection_name);
}

#[test]
fn test_decrypt_with_default_user_collection_email() {
let store = create_test_key_store();
let mut ctx = store.context();
let org_id = Uuid::parse_str(ORGANIZATION_ID).unwrap();
let key = SymmetricKeyId::Organization(org_id);

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

let collection = Collection {
id: Some(Uuid::parse_str(COLLECTION_ID).unwrap()),
organization_id: org_id,
name: collection_name.encrypt(&mut ctx, key).unwrap(),
external_id: None,
hide_passwords: false,
read_only: true,
manage: false,
default_user_collection_email: Some(default_user_collection_email.clone()),
r#type: CollectionType::SharedCollection,
};

let decrypted = collection.decrypt(&mut ctx, key).unwrap();

assert_ne!(decrypted.name, collection_name);
assert_eq!(decrypted.name, default_user_collection_email);
}

#[test]
fn test_decrypt_all_fields_preserved() {
let store = create_test_key_store();
let mut ctx = store.context();
let org_id = Uuid::parse_str(ORGANIZATION_ID).unwrap();
let key = SymmetricKeyId::Organization(org_id);

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

let collection = Collection {
id: collection_id,
organization_id: org_id,
name: collection_name.encrypt(&mut ctx, key).unwrap(),
external_id: external_id.clone(),
hide_passwords: true,
read_only: true,
manage: true,
default_user_collection_email: None,
r#type: collection_type.clone(),
};

let decrypted = collection.decrypt(&mut ctx, key).unwrap();

// Verify all fields are correctly transferred
assert_eq!(decrypted.id, collection.id);
assert_eq!(decrypted.organization_id, collection.organization_id);
assert_eq!(decrypted.name, collection_name);
assert_eq!(decrypted.external_id, external_id);
assert_eq!(decrypted.hide_passwords, collection.hide_passwords);
assert_eq!(decrypted.read_only, collection.read_only);
assert_eq!(decrypted.manage, collection.manage);
assert_eq!(decrypted.r#type, collection_type);
}
}
5 changes: 5 additions & 0 deletions crates/bitwarden-vault/src/collection_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ impl CollectionViewTree {

#[cfg(test)]
mod tests {
use bitwarden_collections::collection::CollectionType;
use bitwarden_core::client::test_accounts::test_bitwarden_com_account;

use super::*;
Expand All @@ -127,6 +128,8 @@ mod tests {
hide_passwords: false,
read_only: false,
manage: false,
default_user_collection_email: None,
r#type: CollectionType::SharedCollection,
}]).unwrap();

assert_eq!(dec[0].name, "Default collection");
Expand All @@ -144,6 +147,8 @@ mod tests {
hide_passwords: false,
read_only: false,
manage: false,
default_user_collection_email: None,
r#type: CollectionType::SharedCollection,
}).unwrap();

assert_eq!(dec.name, "Default collection");
Expand Down
Loading