Skip to content
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

Set login_method when registering for tde #711

Merged
merged 5 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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 @@
/// 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 @@

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 @@
client::{LoginMethod, UserLoginMethod},
error::{require, Result},
mobile::crypto::{AuthRequestMethod, InitUserCryptoMethod, InitUserCryptoRequest},
util::default_kdf,
Client,
};

Expand Down Expand Up @@ -86,9 +84,7 @@
.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
Hinton marked this conversation as resolved.
Show resolved Hide resolved

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 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(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
10 changes: 9 additions & 1 deletion crates/bitwarden/src/auth/tde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
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 @@
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
8 changes: 8 additions & 0 deletions crates/bitwarden/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@
alphabet,
engine::{DecodePaddingMode, GeneralPurpose, GeneralPurposeConfig},
};
#[cfg(feature = "internal")]
use bitwarden_crypto::Kdf;

#[cfg(feature = "internal")]
pub fn default_kdf() -> Kdf {
Kdf::PBKDF2 {
iterations: default_pbkdf2_iterations(),
}
}

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

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/util.rs#L11-L15

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