Skip to content

Commit 6811df2

Browse files
committed
Add UserId, CipherId, FolderId
1 parent 7269f31 commit 6811df2

File tree

25 files changed

+150
-123
lines changed

25 files changed

+150
-123
lines changed

Cargo.lock

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

crates/bitwarden-core/src/auth/auth_request.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ mod tests {
143143
use crate::{
144144
key_management::SymmetricKeyId,
145145
mobile::crypto::{AuthRequestMethod, InitUserCryptoMethod, InitUserCryptoRequest},
146+
UserId,
146147
};
147148

148149
#[test]
@@ -243,7 +244,7 @@ mod tests {
243244
new_device
244245
.crypto()
245246
.initialize_user_crypto(InitUserCryptoRequest {
246-
user_id: Some(uuid::Uuid::new_v4()),
247+
user_id: Some(UserId::new_v4()),
247248
kdf_params: kdf,
248249
email: email.to_owned(),
249250
private_key: private_key.to_owned(),

crates/bitwarden-core/src/auth/login/access_token.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use bitwarden_crypto::{EncString, KeyDecryptable, SymmetricCryptoKey};
55
use chrono::Utc;
66
use schemars::JsonSchema;
77
use serde::{Deserialize, Serialize};
8-
use uuid::Uuid;
98

109
use super::LoginError;
1110
use crate::{
@@ -17,7 +16,7 @@ use crate::{
1716
client::{LoginMethod, ServiceAccountLoginMethod},
1817
require,
1918
secrets_manager::state::{self, ClientState},
20-
Client,
19+
Client, OrganizationId,
2120
};
2221

2322
pub(crate) async fn login_access_token(
@@ -36,7 +35,7 @@ pub(crate) async fn login_access_token(
3635
.set_login_method(LoginMethod::ServiceAccount(
3736
ServiceAccountLoginMethod::AccessToken {
3837
access_token,
39-
organization_id,
38+
organization_id: organization_id.into(),
4039
state_file: Some(state_file.to_path_buf()),
4140
},
4241
));
@@ -118,7 +117,7 @@ fn load_tokens_from_state(
118117
client: &Client,
119118
state_file: &Path,
120119
access_token: &AccessToken,
121-
) -> Result<Uuid, LoginError> {
120+
) -> Result<OrganizationId, LoginError> {
122121
let client_state = state::get(state_file, access_token)?;
123122

124123
let token: JwtToken = client_state.token.parse()?;
@@ -127,7 +126,7 @@ fn load_tokens_from_state(
127126
let time_till_expiration = (token.exp as i64) - Utc::now().timestamp();
128127

129128
if time_till_expiration > 0 {
130-
let organization_id: Uuid = organization_id
129+
let organization_id: OrganizationId = organization_id
131130
.parse()
132131
.map_err(|_| LoginError::InvalidOrganizationId)?;
133132
let encryption_key = SymmetricCryptoKey::try_from(client_state.encryption_key)?;

crates/bitwarden-core/src/client/encryption_settings.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use bitwarden_crypto::{AsymmetricCryptoKey, KeyStore, SymmetricCryptoKey};
33
use bitwarden_crypto::{EncString, UnsignedSharedKey};
44
use bitwarden_error::bitwarden_error;
55
use thiserror::Error;
6-
use uuid::Uuid;
6+
7+
#[cfg(any(feature = "secrets", feature = "internal"))]
8+
use crate::OrganizationId;
79

810
use crate::{
911
error::UserIdAlreadySetError,
@@ -85,21 +87,21 @@ impl EncryptionSettings {
8587
/// This is used only for logging in Secrets Manager with an access token
8688
#[cfg(feature = "secrets")]
8789
pub(crate) fn new_single_org_key(
88-
organization_id: Uuid,
90+
organization_id: OrganizationId,
8991
key: SymmetricCryptoKey,
9092
store: &KeyStore<KeyIds>,
9193
) {
9294
// FIXME: [PM-18098] When this is part of crypto we won't need to use deprecated methods
9395
#[allow(deprecated)]
9496
store
9597
.context_mut()
96-
.set_symmetric_key(SymmetricKeyId::Organization(organization_id), key)
98+
.set_symmetric_key(SymmetricKeyId::Organization(organization_id.into()), key)
9799
.expect("Mutable context");
98100
}
99101

100102
#[cfg(feature = "internal")]
101103
pub(crate) fn set_org_keys(
102-
org_enc_keys: Vec<(Uuid, UnsignedSharedKey)>,
104+
org_enc_keys: Vec<(OrganizationId, UnsignedSharedKey)>,
103105
store: &KeyStore<KeyIds>,
104106
) -> Result<(), EncryptionSettingsError> {
105107
let mut ctx = store.context_mut();
@@ -121,7 +123,7 @@ impl EncryptionSettings {
121123
for (org_id, org_enc_key) in org_enc_keys {
122124
ctx.decapsulate_key_unsigned(
123125
AsymmetricKeyId::UserPrivateKey,
124-
SymmetricKeyId::Organization(org_id),
126+
SymmetricKeyId::Organization(org_id.into()),
125127
&org_enc_key,
126128
)?;
127129
}

crates/bitwarden-core/src/client/internal.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use bitwarden_crypto::SymmetricCryptoKey;
66
#[cfg(feature = "internal")]
77
use bitwarden_crypto::{EncString, Kdf, MasterKey, PinKey, UnsignedSharedKey};
88
use chrono::Utc;
9-
use uuid::Uuid;
109

1110
#[cfg(feature = "secrets")]
1211
use super::login_method::ServiceAccountLoginMethod;
@@ -15,13 +14,14 @@ use crate::{
1514
client::{encryption_settings::EncryptionSettings, login_method::LoginMethod},
1615
error::UserIdAlreadySetError,
1716
key_management::KeyIds,
18-
DeviceType,
17+
DeviceType, UserId,
1918
};
2019
#[cfg(feature = "internal")]
2120
use crate::{
2221
client::encryption_settings::EncryptionSettingsError,
2322
client::{flags::Flags, login_method::UserLoginMethod},
2423
error::NotAuthenticatedError,
24+
OrganizationId,
2525
};
2626

2727
#[derive(Debug, Clone)]
@@ -45,7 +45,7 @@ pub(crate) struct Tokens {
4545

4646
#[derive(Debug)]
4747
pub struct InternalClient {
48-
pub(crate) user_id: OnceLock<Uuid>,
48+
pub(crate) user_id: OnceLock<UserId>,
4949
pub(crate) tokens: RwLock<Tokens>,
5050
pub(crate) login_method: RwLock<Option<Arc<LoginMethod>>>,
5151

@@ -83,7 +83,7 @@ impl InternalClient {
8383
.clone()
8484
}
8585

86-
pub fn get_access_token_organization(&self) -> Option<Uuid> {
86+
pub fn get_access_token_organization(&self) -> Option<OrganizationId> {
8787
match self
8888
.login_method
8989
.read()
@@ -174,11 +174,11 @@ impl InternalClient {
174174
&self.key_store
175175
}
176176

177-
pub fn init_user_id(&self, user_id: Uuid) -> Result<(), UserIdAlreadySetError> {
177+
pub fn init_user_id(&self, user_id: UserId) -> Result<(), UserIdAlreadySetError> {
178178
self.user_id.set(user_id).map_err(|_| UserIdAlreadySetError)
179179
}
180180

181-
pub fn get_user_id(&self) -> Option<Uuid> {
181+
pub fn get_user_id(&self) -> Option<UserId> {
182182
self.user_id.get().copied()
183183
}
184184

@@ -220,17 +220,23 @@ impl InternalClient {
220220
#[cfg(feature = "secrets")]
221221
pub(crate) fn initialize_crypto_single_org_key(
222222
&self,
223-
organization_id: Uuid,
223+
organization_id: OrganizationId,
224224
key: SymmetricCryptoKey,
225225
) {
226-
EncryptionSettings::new_single_org_key(organization_id, key, &self.key_store);
226+
EncryptionSettings::new_single_org_key(organization_id.into(), key, &self.key_store);
227227
}
228228

229229
#[cfg(feature = "internal")]
230230
pub fn initialize_org_crypto(
231231
&self,
232-
org_keys: Vec<(Uuid, UnsignedSharedKey)>,
232+
org_keys: Vec<(OrganizationId, UnsignedSharedKey)>,
233233
) -> Result<(), EncryptionSettingsError> {
234-
EncryptionSettings::set_org_keys(org_keys, &self.key_store)
234+
EncryptionSettings::set_org_keys(
235+
org_keys
236+
.into_iter()
237+
.map(|(id, key)| (id.into(), key))
238+
.collect(),
239+
&self.key_store,
240+
)
235241
}
236242
}

crates/bitwarden-core/src/client/login_method.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#[cfg(feature = "secrets")]
22
use std::path::PathBuf;
33

4-
use bitwarden_crypto::Kdf;
5-
#[cfg(feature = "secrets")]
6-
use uuid::Uuid;
7-
84
#[cfg(feature = "secrets")]
95
use crate::auth::AccessToken;
6+
use crate::OrganizationId;
7+
use bitwarden_crypto::Kdf;
108

119
#[derive(Debug)]
1210
pub(crate) enum LoginMethod {
@@ -40,7 +38,7 @@ pub(crate) enum UserLoginMethod {
4038
pub(crate) enum ServiceAccountLoginMethod {
4139
AccessToken {
4240
access_token: AccessToken,
43-
organization_id: Uuid,
41+
organization_id: OrganizationId,
4442
state_file: Option<PathBuf>,
4543
},
4644
}

crates/bitwarden-core/src/client/test_accounts.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
initialize_org_crypto, initialize_user_crypto, InitOrgCryptoRequest, InitUserCryptoMethod,
99
InitUserCryptoRequest,
1010
},
11-
Client,
11+
Client, UserId,
1212
};
1313

1414
impl Client {
@@ -117,7 +117,7 @@ pub struct TestAccount {
117117
pub fn test_bitwarden_com_account() -> TestAccount {
118118
TestAccount {
119119
user: InitUserCryptoRequest {
120-
user_id: Some(uuid::uuid!("060000fb-0922-4dd3-b170-6e15cb5df8c8")),
120+
user_id: Some(UserId::new(uuid::uuid!("060000fb-0922-4dd3-b170-6e15cb5df8c8"))),
121121
kdf_params: Kdf::PBKDF2 {
122122
iterations: 600_000.try_into().unwrap(),
123123
},
@@ -175,7 +175,7 @@ pub fn test_bitwarden_com_account() -> TestAccount {
175175
pub fn test_legacy_user_key_account() -> TestAccount {
176176
TestAccount {
177177
user: InitUserCryptoRequest {
178-
user_id: Some(uuid::uuid!("060000fb-0922-4dd3-b170-6e15cb5df8c8")),
178+
user_id: Some(UserId::new(uuid::uuid!("060000fb-0922-4dd3-b170-6e15cb5df8c8"))),
179179
kdf_params: Kdf::PBKDF2 {
180180
iterations: 600_000.try_into().unwrap(),
181181
},

crates/bitwarden-core/src/ids.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
use bitwarden_uuid::uuid;
1+
use bitwarden_uuid::uuid_newtype;
22

3-
uuid!(pub OrganizationId);
3+
uuid_newtype!(pub OrganizationId);
4+
uuid_newtype!(pub UserId);

crates/bitwarden-core/src/mobile/crypto.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use {tsify_next::Tsify, wasm_bindgen::prelude::*};
1818
use crate::{
1919
client::{encryption_settings::EncryptionSettingsError, LoginMethod, UserLoginMethod},
2020
key_management::SymmetricKeyId,
21-
Client, NotAuthenticatedError, VaultLockedError, WrongPasswordError,
21+
Client, NotAuthenticatedError, OrganizationId, UserId, VaultLockedError, WrongPasswordError,
2222
};
2323

2424
/// Catch all error for mobile crypto operations.
@@ -39,7 +39,7 @@ pub enum MobileCryptoError {
3939
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
4040
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
4141
pub struct InitUserCryptoRequest {
42-
pub user_id: Option<uuid::Uuid>,
42+
pub user_id: Option<UserId>,
4343
/// The user's KDF parameters, as received from the prelogin request
4444
pub kdf_params: Kdf,
4545
/// The user's email address
@@ -232,7 +232,7 @@ pub async fn initialize_user_crypto(
232232
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
233233
pub struct InitOrgCryptoRequest {
234234
/// The encryption keys for all the organizations the user is a part of
235-
pub organization_keys: HashMap<uuid::Uuid, UnsignedSharedKey>,
235+
pub organization_keys: HashMap<OrganizationId, UnsignedSharedKey>,
236236
}
237237

238238
/// Initialize the user's organizational cryptographic state.
@@ -569,7 +569,7 @@ mod tests {
569569
initialize_user_crypto(
570570
& client,
571571
InitUserCryptoRequest {
572-
user_id: Some(uuid::Uuid::new_v4()),
572+
user_id: Some(UserId::new_v4()),
573573
kdf_params: kdf.clone(),
574574
email: "[email protected]".into(),
575575
private_key: priv_key.to_owned(),
@@ -589,7 +589,7 @@ mod tests {
589589
initialize_user_crypto(
590590
&client2,
591591
InitUserCryptoRequest {
592-
user_id: Some(uuid::Uuid::new_v4()),
592+
user_id: Some(UserId::new_v4()),
593593
kdf_params: kdf.clone(),
594594
email: "[email protected]".into(),
595595
private_key: priv_key.to_owned(),
@@ -645,7 +645,7 @@ mod tests {
645645
initialize_user_crypto(
646646
& client,
647647
InitUserCryptoRequest {
648-
user_id: Some(uuid::Uuid::new_v4()),
648+
user_id: Some(UserId::new_v4()),
649649
kdf_params: Kdf::PBKDF2 {
650650
iterations: 100_000.try_into().unwrap(),
651651
},
@@ -667,7 +667,7 @@ mod tests {
667667
initialize_user_crypto(
668668
&client2,
669669
InitUserCryptoRequest {
670-
user_id: Some(uuid::Uuid::new_v4()),
670+
user_id: Some(UserId::new_v4()),
671671
kdf_params: Kdf::PBKDF2 {
672672
iterations: 100_000.try_into().unwrap(),
673673
},
@@ -710,7 +710,7 @@ mod tests {
710710
initialize_user_crypto(
711711
&client3,
712712
InitUserCryptoRequest {
713-
user_id: Some(uuid::Uuid::new_v4()),
713+
user_id: Some(UserId::new_v4()),
714714
kdf_params: Kdf::PBKDF2 {
715715
iterations: 100_000.try_into().unwrap(),
716716
},

crates/bitwarden-core/tests/register.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ async fn test_register_initialize_crypto() {
88

99
use bitwarden_core::{
1010
mobile::crypto::{InitUserCryptoMethod, InitUserCryptoRequest},
11-
Client,
11+
Client, UserId,
1212
};
1313
use bitwarden_crypto::Kdf;
1414

@@ -29,7 +29,7 @@ async fn test_register_initialize_crypto() {
2929
client
3030
.crypto()
3131
.initialize_user_crypto(InitUserCryptoRequest {
32-
user_id: Some(uuid::Uuid::new_v4()),
32+
user_id: Some(UserId::new_v4()),
3333
kdf_params: kdf,
3434
email: email.to_owned(),
3535
private_key: register_response.keys.private.to_string(),

crates/bitwarden-exporters/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt;
22

33
use bitwarden_vault::{
4-
CipherRepromptType, CipherView, Fido2CredentialFullView, LoginUriView, UriMatchType,
4+
CipherRepromptType, CipherView, Fido2CredentialFullView, FolderId, LoginUriView, UriMatchType,
55
};
66
use chrono::{DateTime, Utc};
77
use uuid::Uuid;
@@ -117,7 +117,7 @@ impl From<ImportingCipher> for CipherView {
117117
Self {
118118
id: None,
119119
organization_id: None,
120-
folder_id: value.folder_id,
120+
folder_id: value.folder_id.map(FolderId::new),
121121
collection_ids: vec![],
122122
key: None,
123123
name: value.name,

0 commit comments

Comments
 (0)