Skip to content

Commit

Permalink
Set login_method when registering for tde
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton committed Apr 12, 2024
1 parent b611974 commit 94cff77
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
13 changes: 6 additions & 7 deletions crates/bitwarden-uniffi/src/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,15 @@ impl ClientAuth {
/// Generate keys needed for TDE process
pub async fn make_register_tde_keys(
&self,
email: String,

Check warning on line 85 in crates/bitwarden-uniffi/src/auth/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/auth/mod.rs#L85

Added line #L85 was not covered by tests
org_public_key: String,
remember_device: bool,
) -> Result<RegisterTdeKeyResponse> {
Ok(self
.0
.0
.write()
.await
.auth()
.make_register_tde_keys(org_public_key, remember_device)?)
Ok(self.0 .0.write().await.auth().make_register_tde_keys(
email,
org_public_key,
remember_device,
)?)

Check warning on line 93 in crates/bitwarden-uniffi/src/auth/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/auth/mod.rs#L89-L93

Added lines #L89 - L93 were not covered by tests
}

/// Validate the user password
Expand Down
3 changes: 2 additions & 1 deletion crates/bitwarden/src/auth/client_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ impl<'a> ClientAuth<'a> {

pub fn make_register_tde_keys(
&mut self,
email: String,

Check warning on line 79 in crates/bitwarden/src/auth/client_auth.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/client_auth.rs#L79

Added line #L79 was not covered by tests
org_public_key: String,
remember_device: bool,
) -> Result<RegisterTdeKeyResponse> {
make_register_tde_keys(self.client, org_public_key, remember_device)
make_register_tde_keys(self.client, email, org_public_key, remember_device)

Check warning on line 83 in crates/bitwarden/src/auth/client_auth.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/client_auth.rs#L83

Added line #L83 was not covered by tests
}

pub async fn register(&mut self, input: &RegisterRequest) -> Result<()> {
Expand Down
8 changes: 2 additions & 6 deletions crates/bitwarden/src/auth/login/auth_request.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use std::num::NonZeroU32;

use bitwarden_api_api::{
apis::auth_requests_api::{auth_requests_id_response_get, auth_requests_post},
models::{AuthRequestCreateRequestModel, AuthRequestType},
};
use bitwarden_crypto::Kdf;
use uuid::Uuid;

use crate::{
Expand All @@ -15,6 +12,7 @@ use crate::{
client::{LoginMethod, UserLoginMethod},
error::{require, Result},
mobile::crypto::{AuthRequestMethod, InitUserCryptoMethod, InitUserCryptoRequest},
util::default_kdf,
Client,
};

Expand Down Expand Up @@ -86,9 +84,7 @@ pub(crate) async fn complete_auth_request(
.await?;

if let IdentityTokenResponse::Authenticated(r) = response {
let kdf = Kdf::PBKDF2 {
iterations: NonZeroU32::new(600_000).expect("Non-zero number"),
};
let kdf = default_kdf();

Check warning on line 87 in crates/bitwarden/src/auth/login/auth_request.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/login/auth_request.rs#L87

Added line #L87 was not covered by tests

client.set_tokens(
r.access_token.clone(),
Expand Down
11 changes: 7 additions & 4 deletions crates/bitwarden/src/auth/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ use bitwarden_crypto::{HashPurpose, MasterKey, RsaKeyPair};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::{client::Kdf, error::Result, util::default_pbkdf2_iterations, Client};
use crate::{
client::Kdf,
error::Result,
util::{default_kdf, default_pbkdf2_iterations},
Client,
};

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
Expand All @@ -21,9 +26,7 @@ pub struct RegisterRequest {
pub(super) async fn register(client: &mut Client, req: &RegisterRequest) -> Result<()> {
let config = client.get_api_configurations().await;

let kdf = Kdf::PBKDF2 {
iterations: default_pbkdf2_iterations(),
};
let kdf = default_kdf();

Check warning on line 29 in crates/bitwarden/src/auth/register.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/register.rs#L29

Added line #L29 was not covered by tests

let keys = make_register_keys(req.email.to_owned(), req.password.to_owned(), kdf)?;

Expand Down
12 changes: 10 additions & 2 deletions crates/bitwarden/src/auth/tde.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use base64::{engine::general_purpose::STANDARD, Engine};
use bitwarden_crypto::{
AsymmetricEncString, AsymmetricPublicCryptoKey, DeviceKey, EncString, SymmetricCryptoKey,
AsymmetricEncString, AsymmetricPublicCryptoKey, DeviceKey, EncString, Kdf, SymmetricCryptoKey,

Check failure

Code scanning / clippy

unused import: Kdf Error

unused import: Kdf
TrustDeviceResponse, UserKey,
};

use crate::{error::Result, Client};
use crate::{error::Result, util::default_kdf, Client};

/// This function generates a new user key and key pair, initializes the client's crypto with the
/// generated user key, and encrypts the user key with the organization public key for admin
/// password reset. If remember_device is true, it also generates a device key.
pub(super) fn make_register_tde_keys(
client: &mut Client,
email: String,

Check warning on line 14 in crates/bitwarden/src/auth/tde.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/tde.rs#L14

Added line #L14 was not covered by tests
org_public_key: String,
remember_device: bool,
) -> Result<RegisterTdeKeyResponse> {
Expand All @@ -30,6 +31,13 @@ pub(super) fn make_register_tde_keys(
None
};

client.set_login_method(crate::client::LoginMethod::User(
crate::client::UserLoginMethod::Username {
client_id: "".to_owned(),
email,
kdf: default_kdf(),
},
));

Check warning on line 40 in crates/bitwarden/src/auth/tde.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/tde.rs#L34-L40

Added lines #L34 - L40 were not covered by tests
client.initialize_user_crypto_decrypted_key(user_key.0, key_pair.private.clone())?;

Ok(RegisterTdeKeyResponse {
Expand Down
6 changes: 6 additions & 0 deletions crates/bitwarden/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ use base64::{
alphabet,
engine::{DecodePaddingMode, GeneralPurpose, GeneralPurposeConfig},
};
use bitwarden_crypto::Kdf;

pub fn default_kdf() -> Kdf {
Kdf::PBKDF2 {
iterations: default_pbkdf2_iterations(),
}
}

Check warning on line 13 in crates/bitwarden/src/util.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/util.rs#L9-L13

Added lines #L9 - L13 were not covered by tests
pub fn default_pbkdf2_iterations() -> NonZeroU32 {
NonZeroU32::new(600_000).expect("Non-zero number")
}
Expand Down

0 comments on commit 94cff77

Please sign in to comment.