Skip to content

Commit 3413a56

Browse files
committed
Only allow setting userID once
1 parent fbda0e7 commit 3413a56

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

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

Lines changed: 6 additions & 9 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"))]
@@ -11,10 +11,7 @@ use uuid::Uuid;
1111
#[cfg(feature = "secrets")]
1212
use super::login_method::ServiceAccountLoginMethod;
1313
use crate::{
14-
auth::renew::renew_token,
15-
client::{encryption_settings::EncryptionSettings, login_method::LoginMethod},
16-
key_management::KeyIds,
17-
DeviceType,
14+
auth::renew::renew_token, client::{encryption_settings::EncryptionSettings, login_method::LoginMethod}, error::UserIdAlreadySetError, key_management::KeyIds, DeviceType
1815
};
1916
#[cfg(feature = "internal")]
2017
use crate::{
@@ -44,7 +41,7 @@ pub(crate) struct Tokens {
4441

4542
#[derive(Debug)]
4643
pub struct InternalClient {
47-
pub(crate) user_id: RwLock<Option<Uuid>>,
44+
pub(crate) user_id: OnceLock<Uuid>,
4845
pub(crate) tokens: RwLock<Tokens>,
4946
pub(crate) login_method: RwLock<Option<Arc<LoginMethod>>>,
5047

@@ -173,12 +170,12 @@ impl InternalClient {
173170
&self.key_store
174171
}
175172

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

180177
pub fn get_user_id(&self) -> Option<Uuid> {
181-
*self.user_id.read().expect("RwLock is not poisoned")
178+
self.user_id.get().map(|id| *id)
182179
}
183180

184181
#[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)