Skip to content

Commit 468bcda

Browse files
committed
Only allow setting userID once
1 parent fbda0e7 commit 468bcda

File tree

5 files changed

+18
-8
lines changed

5 files changed

+18
-8
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::{Arc, RwLock};
1+
use std::sync::{Arc, OnceLock, RwLock};
22

33
use bitwarden_crypto::KeyStore;
44
use reqwest::header::{self, HeaderValue};
@@ -75,7 +75,7 @@ impl Client {
7575

7676
Self {
7777
internal: Arc::new(InternalClient {
78-
user_id: RwLock::new(None),
78+
user_id: OnceLock::new(),
7979
tokens: RwLock::new(Tokens::default()),
8080
login_method: RwLock::new(None),
8181
#[cfg(feature = "internal")]

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use thiserror::Error;
66
use uuid::Uuid;
77

88
use crate::{
9+
error::UserIdAlreadySetError,
910
key_management::{AsymmetricKeyId, KeyIds, SymmetricKeyId},
1011
MissingPrivateKeyError, VaultLockedError,
1112
};
@@ -27,6 +28,9 @@ pub enum EncryptionSettingsError {
2728

2829
#[error(transparent)]
2930
MissingPrivateKey(#[from] MissingPrivateKeyError),
31+
32+
#[error(transparent)]
33+
UserIdAlreadySetError(#[from] UserIdAlreadySetError),
3034
}
3135

3236
pub struct EncryptionSettings {}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::{Arc, RwLock};
1+
use std::sync::{Arc, OnceLock, RwLock};
22

33
use bitwarden_crypto::KeyStore;
44
#[cfg(any(feature = "internal", feature = "secrets"))]
@@ -13,6 +13,7 @@ use super::login_method::ServiceAccountLoginMethod;
1313
use crate::{
1414
auth::renew::renew_token,
1515
client::{encryption_settings::EncryptionSettings, login_method::LoginMethod},
16+
error::UserIdAlreadySetError,
1617
key_management::KeyIds,
1718
DeviceType,
1819
};
@@ -44,7 +45,7 @@ pub(crate) struct Tokens {
4445

4546
#[derive(Debug)]
4647
pub struct InternalClient {
47-
pub(crate) user_id: RwLock<Option<Uuid>>,
48+
pub(crate) user_id: OnceLock<Uuid>,
4849
pub(crate) tokens: RwLock<Tokens>,
4950
pub(crate) login_method: RwLock<Option<Arc<LoginMethod>>>,
5051

@@ -173,12 +174,12 @@ impl InternalClient {
173174
&self.key_store
174175
}
175176

176-
pub fn set_user_id(&self, user_id: Uuid) {
177-
*self.user_id.write().expect("RwLock is not poisoned") = Some(user_id);
177+
pub fn init_user_id(&self, user_id: Uuid) -> Result<(), UserIdAlreadySetError> {
178+
self.user_id.set(user_id).map_err(|_| UserIdAlreadySetError)
178179
}
179180

180181
pub fn get_user_id(&self) -> Option<Uuid> {
181-
*self.user_id.read().expect("RwLock is not poisoned")
182+
self.user_id.get().copied()
182183
}
183184

184185
#[cfg(feature = "internal")]

crates/bitwarden-core/src/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ impl_bitwarden_error!(IdentityError, ApiError);
4848
#[error("The client is not authenticated or the session has expired")]
4949
pub struct NotAuthenticatedError;
5050

51+
/// Client's user ID is already set.
52+
#[derive(Debug, Error)]
53+
#[error("The client user ID is already set")]
54+
pub struct UserIdAlreadySetError;
55+
5156
/// Missing required field.
5257
#[derive(Debug, Error)]
5358
#[error("The response received was missing a required field: {0}")]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub async fn initialize_user_crypto(
133133
let private_key: EncString = req.private_key.parse()?;
134134

135135
if let Some(user_id) = req.user_id {
136-
client.internal.set_user_id(user_id);
136+
client.internal.init_user_id(user_id)?;
137137
}
138138

139139
match req.method {

0 commit comments

Comments
 (0)