Skip to content

Lift in user_id into login method #318

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion crates/bitwarden-core/src/auth/auth_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
use bitwarden_crypto::{
CryptoError, DeviceKey, EncString, Kdf, TrustDeviceResponse, UnsignedSharedKey,
};
#[cfg(feature = "internal")]
use uuid::Uuid;

#[cfg(feature = "secrets")]
use crate::auth::login::{login_access_token, AccessTokenLoginRequest, AccessTokenLoginResponse};
Expand Down Expand Up @@ -91,8 +93,15 @@
email: String,
org_public_key: String,
remember_device: bool,
user_id: Uuid,

Check warning on line 96 in crates/bitwarden-core/src/auth/auth_client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-core/src/auth/auth_client.rs#L96

Added line #L96 was not covered by tests
) -> Result<RegisterTdeKeyResponse, EncryptionSettingsError> {
make_register_tde_keys(&self.client, email, org_public_key, remember_device)
make_register_tde_keys(
&self.client,
email,
org_public_key,
remember_device,
user_id,
)

Check warning on line 104 in crates/bitwarden-core/src/auth/auth_client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-core/src/auth/auth_client.rs#L98-L104

Added lines #L98 - L104 were not covered by tests
}

#[allow(missing_docs)]
Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden-core/src/auth/auth_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ mod tests {
new_device
.crypto()
.initialize_user_crypto(InitUserCryptoRequest {
user_id: Some(uuid::Uuid::new_v4()),
user_id: uuid::Uuid::new_v4(),
kdf_params: kdf,
email: email.to_owned(),
private_key,
Expand Down
9 changes: 5 additions & 4 deletions crates/bitwarden-core/src/auth/login/api_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@

let master_key = MasterKey::derive(&input.password, &email, &kdf)?;

client
.internal
.set_login_method(LoginMethod::User(UserLoginMethod::ApiKey {
client.internal.set_login_method(LoginMethod::User {
method: UserLoginMethod::ApiKey {

Check warning on line 43 in crates/bitwarden-core/src/auth/login/api_key.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-core/src/auth/login/api_key.rs#L42-L43

Added lines #L42 - L43 were not covered by tests
client_id: input.client_id.to_owned(),
client_secret: input.client_secret.to_owned(),
email,
kdf,
}));
},
user_id: access_token_obj.sub.parse()?,

Check warning on line 49 in crates/bitwarden-core/src/auth/login/api_key.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-core/src/auth/login/api_key.rs#L48-L49

Added lines #L48 - L49 were not covered by tests
});

let user_key: EncString = require!(r.key.as_deref()).parse()?;
let private_key: EncString = require!(r.private_key.as_deref()).parse()?;
Expand Down
14 changes: 9 additions & 5 deletions crates/bitwarden-core/src/auth/login/auth_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
auth::{
api::{request::AuthRequestTokenRequest, response::IdentityTokenResponse},
auth_request::new_auth_request,
JwtToken,
},
client::{LoginMethod, UserLoginMethod},
key_management::crypto::{AuthRequestMethod, InitUserCryptoMethod, InitUserCryptoRequest},
Expand Down Expand Up @@ -88,20 +89,23 @@
.await?;

if let IdentityTokenResponse::Authenticated(r) = response {
let access_token_obj: JwtToken = r.access_token.parse()?;

Check warning on line 92 in crates/bitwarden-core/src/auth/login/auth_request.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-core/src/auth/login/auth_request.rs#L92

Added line #L92 was not covered by tests

let kdf = Kdf::default();

client.internal.set_tokens(
r.access_token.clone(),
r.refresh_token.clone(),
r.expires_in,
);
client
.internal
.set_login_method(LoginMethod::User(UserLoginMethod::Username {
client.internal.set_login_method(LoginMethod::User {
method: UserLoginMethod::Username {

Check warning on line 102 in crates/bitwarden-core/src/auth/login/auth_request.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-core/src/auth/login/auth_request.rs#L101-L102

Added lines #L101 - L102 were not covered by tests
client_id: "web".to_owned(),
email: auth_req.email.to_owned(),
kdf: kdf.clone(),
}));
},
user_id: access_token_obj.sub.parse()?,

Check warning on line 107 in crates/bitwarden-core/src/auth/login/auth_request.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-core/src/auth/login/auth_request.rs#L106-L107

Added lines #L106 - L107 were not covered by tests
});

let method = match res.master_password_hash {
Some(_) => AuthRequestMethod::MasterKey {
Expand All @@ -116,7 +120,7 @@
client
.crypto()
.initialize_user_crypto(InitUserCryptoRequest {
user_id: None,
user_id: access_token_obj.sub.parse()?,

Check warning on line 123 in crates/bitwarden-core/src/auth/login/auth_request.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-core/src/auth/login/auth_request.rs#L123

Added line #L123 was not covered by tests
kdf_params: kdf,
email: auth_req.email,
private_key: require!(r.private_key).parse()?,
Expand Down
2 changes: 2 additions & 0 deletions crates/bitwarden-core/src/auth/login/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub enum LoginError {
Serde(#[from] serde_json::Error),
#[error(transparent)]
InvalidBase64(#[from] base64::DecodeError),
#[error(transparent)]
Uuid(#[from] uuid::Error),

#[error(transparent)]
MissingField(#[from] crate::MissingFieldError),
Expand Down
13 changes: 9 additions & 4 deletions crates/bitwarden-core/src/auth/login/password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,23 @@
let response = request_identity_tokens(client, input, &password_hash).await?;

if let IdentityTokenResponse::Authenticated(r) = &response {
use crate::auth::JwtToken;

Check warning on line 37 in crates/bitwarden-core/src/auth/login/password.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-core/src/auth/login/password.rs#L37

Added line #L37 was not covered by tests

let access_token_obj: JwtToken = r.access_token.parse()?;

Check warning on line 39 in crates/bitwarden-core/src/auth/login/password.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-core/src/auth/login/password.rs#L39

Added line #L39 was not covered by tests

client.internal.set_tokens(
r.access_token.clone(),
r.refresh_token.clone(),
r.expires_in,
);
client
.internal
.set_login_method(LoginMethod::User(UserLoginMethod::Username {
client.internal.set_login_method(LoginMethod::User {
method: UserLoginMethod::Username {

Check warning on line 47 in crates/bitwarden-core/src/auth/login/password.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-core/src/auth/login/password.rs#L46-L47

Added lines #L46 - L47 were not covered by tests
client_id: "web".to_owned(),
email: input.email.to_owned(),
kdf: input.kdf.to_owned(),
}));
},
user_id: access_token_obj.sub.parse()?,

Check warning on line 52 in crates/bitwarden-core/src/auth/login/password.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-core/src/auth/login/password.rs#L51-L52

Added lines #L51 - L52 were not covered by tests
});

let user_key: EncString = require!(r.key.as_deref()).parse()?;
let private_key: EncString = require!(r.private_key.as_deref()).parse()?;
Expand Down
39 changes: 25 additions & 14 deletions crates/bitwarden-core/src/auth/password/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ pub(crate) fn validate_password(
.ok_or(NotAuthenticatedError)?;

#[allow(irrefutable_let_patterns)]
if let LoginMethod::User(login_method) = login_method.as_ref() {
if let LoginMethod::User {
method: login_method,
..
} = login_method.as_ref()
{
match login_method {
UserLoginMethod::Username { email, kdf, .. }
| UserLoginMethod::ApiKey { email, kdf, .. } => {
Expand Down Expand Up @@ -50,7 +54,11 @@ pub(crate) fn validate_password_user_key(
.ok_or(NotAuthenticatedError)?;

#[allow(irrefutable_let_patterns)]
if let LoginMethod::User(login_method) = login_method.as_ref() {
if let LoginMethod::User {
method: login_method,
..
} = login_method.as_ref()
{
match login_method {
UserLoginMethod::Username { email, kdf, .. }
| UserLoginMethod::ApiKey { email, kdf, .. } => {
Expand Down Expand Up @@ -91,15 +99,16 @@ mod tests {
use crate::client::{Client, LoginMethod, UserLoginMethod};

let client = Client::new(None);
client
.internal
.set_login_method(LoginMethod::User(UserLoginMethod::Username {
client.internal.set_login_method(LoginMethod::User {
method: UserLoginMethod::Username {
email: "[email protected]".to_string(),
kdf: Kdf::PBKDF2 {
iterations: NonZeroU32::new(100_000).unwrap(),
},
client_id: "1".to_string(),
}));
},
user_id: uuid::Uuid::new_v4(),
});

let password = "password123".to_string();
let password_hash = "7kTqkF1pY/3JeOu73N9kR99fDDe9O1JOZaVc7KH3lsU=".to_string();
Expand All @@ -125,13 +134,14 @@ mod tests {
iterations: NonZeroU32::new(600_000).unwrap(),
};

client
.internal
.set_login_method(LoginMethod::User(UserLoginMethod::Username {
client.internal.set_login_method(LoginMethod::User {
method: UserLoginMethod::Username {
email: email.to_string(),
kdf: kdf.clone(),
client_id: "1".to_string(),
}));
},
user_id: uuid::Uuid::new_v4(),
});

let master_key = MasterKey::derive(password, email, &kdf).unwrap();

Expand Down Expand Up @@ -173,13 +183,14 @@ mod tests {
iterations: NonZeroU32::new(600_000).unwrap(),
};

client
.internal
.set_login_method(LoginMethod::User(UserLoginMethod::Username {
client.internal.set_login_method(LoginMethod::User {
method: UserLoginMethod::Username {
email: email.to_string(),
kdf: kdf.clone(),
client_id: "1".to_string(),
}));
},
user_id: uuid::Uuid::new_v4(),
});

let master_key = MasterKey::derive(password, email, &kdf).unwrap();

Expand Down
16 changes: 11 additions & 5 deletions crates/bitwarden-core/src/auth/pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ pub(crate) fn validate_pin(
.ok_or(NotAuthenticatedError)?;

#[allow(irrefutable_let_patterns)]
let LoginMethod::User(login_method) = login_method.as_ref() else {
let LoginMethod::User {
method: login_method,
..
} = login_method.as_ref()
else {
return Err(NotAuthenticatedError)?;
};

Expand Down Expand Up @@ -47,6 +51,7 @@ mod tests {
use std::num::NonZeroU32;

use bitwarden_crypto::{Kdf, MasterKey};
use uuid::Uuid;

use super::*;
use crate::client::{Client, LoginMethod, UserLoginMethod};
Expand All @@ -60,13 +65,14 @@ mod tests {
iterations: NonZeroU32::new(600_000).unwrap(),
};

client
.internal
.set_login_method(LoginMethod::User(UserLoginMethod::Username {
client.internal.set_login_method(LoginMethod::User {
method: UserLoginMethod::Username {
email: email.to_string(),
kdf: kdf.clone(),
client_id: "1".to_string(),
}));
},
user_id: Uuid::new_v4(),
});

let master_key = MasterKey::derive(password, email, &kdf).unwrap();

Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden-core/src/auth/renew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
.clone();

let res = match login_method.as_ref() {
LoginMethod::User(u) => match u {
LoginMethod::User { method: u, .. } => match u {

Check warning on line 43 in crates/bitwarden-core/src/auth/renew.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-core/src/auth/renew.rs#L43

Added line #L43 was not covered by tests
UserLoginMethod::Username { client_id, .. } => {
let refresh = tokens.refresh_token.ok_or(NotAuthenticatedError)?;

Expand Down
9 changes: 6 additions & 3 deletions crates/bitwarden-core/src/auth/tde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
AsymmetricPublicCryptoKey, DeviceKey, EncString, Kdf, SymmetricCryptoKey, TrustDeviceResponse,
UnsignedSharedKey, UserKey,
};
use uuid::Uuid;

use crate::{client::encryption_settings::EncryptionSettingsError, Client};

Expand All @@ -14,6 +15,7 @@
email: String,
org_public_key: String,
remember_device: bool,
user_id: Uuid,

Check warning on line 18 in crates/bitwarden-core/src/auth/tde.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-core/src/auth/tde.rs#L18

Added line #L18 was not covered by tests
) -> Result<RegisterTdeKeyResponse, EncryptionSettingsError> {
let public_key = AsymmetricPublicCryptoKey::from_der(&STANDARD.decode(org_public_key)?)?;

Expand All @@ -30,13 +32,14 @@

client
.internal
.set_login_method(crate::client::LoginMethod::User(
crate::client::UserLoginMethod::Username {
.set_login_method(crate::client::LoginMethod::User {
user_id,
method: crate::client::UserLoginMethod::Username {

Check warning on line 37 in crates/bitwarden-core/src/auth/tde.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-core/src/auth/tde.rs#L35-L37

Added lines #L35 - L37 were not covered by tests
client_id: "".to_owned(),
email,
kdf: Kdf::default(),
},
));
});

Check warning on line 42 in crates/bitwarden-core/src/auth/tde.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-core/src/auth/tde.rs#L42

Added line #L42 was not covered by tests
client.internal.initialize_user_crypto_decrypted_key(
user_key.0,
key_pair.private.clone(),
Expand Down
3 changes: 1 addition & 2 deletions crates/bitwarden-core/src/client/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::{Arc, OnceLock, RwLock};
use std::sync::{Arc, RwLock};

use bitwarden_crypto::KeyStore;
use reqwest::header::{self, HeaderValue};
Expand Down Expand Up @@ -76,7 +76,6 @@ impl Client {

Self {
internal: Arc::new(InternalClient {
user_id: OnceLock::new(),
tokens: RwLock::new(Tokens::default()),
login_method: RwLock::new(None),
#[cfg(feature = "internal")]
Expand Down
5 changes: 1 addition & 4 deletions crates/bitwarden-core/src/client/encryption_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use uuid::Uuid;

#[cfg(any(feature = "internal", feature = "secrets"))]
use crate::key_management::{KeyIds, SymmetricKeyId};
use crate::{error::UserIdAlreadySetError, MissingPrivateKeyError, VaultLockedError};
use crate::{MissingPrivateKeyError, VaultLockedError};

#[allow(missing_docs)]
#[bitwarden_error(flat)]
Expand All @@ -29,9 +29,6 @@ pub enum EncryptionSettingsError {

#[error(transparent)]
MissingPrivateKey(#[from] MissingPrivateKeyError),

#[error(transparent)]
UserIdAlreadySetError(#[from] UserIdAlreadySetError),
}

#[allow(missing_docs)]
Expand Down
Loading
Loading