Skip to content

Commit

Permalink
Make client internally mutable
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-garcia committed Jun 12, 2024
1 parent c56add5 commit f61d104
Show file tree
Hide file tree
Showing 30 changed files with 219 additions and 173 deletions.
1 change: 1 addition & 0 deletions crates/bitwarden-crypto/src/keys/asymmetric_crypto_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl AsymmetricEncryptable for AsymmetricPublicCryptoKey {

/// An asymmetric encryption key. Contains both the public and private key. Can be used to both
/// encrypt and decrypt [`AsymmetricEncString`](crate::AsymmetricEncString).
#[derive(Clone)]
pub struct AsymmetricCryptoKey {
// RsaPrivateKey is not a Copy type so this isn't completely necessary, but
// to keep the compiler from making stack copies when moving this struct around,
Expand Down
8 changes: 7 additions & 1 deletion crates/bitwarden-crypto/src/keys/key_encryptable.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, hash::Hash};
use std::{collections::HashMap, hash::Hash, sync::Arc};

use rayon::prelude::*;
use uuid::Uuid;
Expand All @@ -9,6 +9,12 @@ pub trait KeyContainer: Send + Sync {
fn get_key(&self, org_id: &Option<Uuid>) -> Option<&SymmetricCryptoKey>;
}

impl<T: KeyContainer> KeyContainer for Arc<T> {
fn get_key(&self, org_id: &Option<Uuid>) -> Option<&SymmetricCryptoKey> {
self.as_ref().get_key(org_id)
}
}

pub trait LocateKey {
fn locate_key<'a>(
&self,
Expand Down
1 change: 1 addition & 0 deletions crates/bitwarden-crypto/src/keys/symmetric_crypto_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use super::key_encryptable::CryptoKey;
use crate::CryptoError;

/// A symmetric encryption key. Used to encrypt and decrypt [`EncString`](crate::EncString)
#[derive(Clone)]
pub struct SymmetricCryptoKey {
// GenericArray is equivalent to [u8; N], which is a Copy type placed on the stack.
// To keep the compiler from making stack copies when moving this struct around,
Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden/src/auth/login/access_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async fn request_access_token(
) -> Result<IdentityTokenResponse> {
let config = client.get_api_configurations().await;
AccessTokenRequest::new(input.access_token_id, &input.client_secret)
.send(config)
.send(&config)
.await
}

Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden/src/auth/login/api_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async fn request_api_identity_tokens(
) -> Result<IdentityTokenResponse> {
let config = client.get_api_configurations().await;
ApiTokenRequest::new(&input.client_id, &input.client_secret)
.send(config)
.send(&config)

Check warning on line 66 in crates/bitwarden/src/auth/login/api_key.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/login/api_key.rs#L66

Added line #L66 was not covered by tests
.await
}

Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden/src/auth/login/auth_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub(crate) async fn complete_auth_request(
config.device_type,
&auth_req.device_identifier,
)
.send(config)
.send(&config)

Check warning on line 84 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#L84

Added line #L84 was not covered by tests
.await?;

if let IdentityTokenResponse::Authenticated(r) = response {
Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden/src/auth/login/password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async fn request_identity_tokens(
"b86dd6ab-4265-4ddf-a7f1-eb28d5677f33",
&input.two_factor,
)
.send(config)
.send(&config)

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

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/login/password.rs#L79

Added line #L79 was not covered by tests
.await
}

Expand Down
14 changes: 4 additions & 10 deletions crates/bitwarden/src/auth/password/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ pub(crate) fn validate_password(
password: String,
password_hash: String,
) -> Result<bool> {
let login_method = client
.login_method
.as_ref()
.ok_or(Error::NotAuthenticated)?;
let login_method = client.get_login_method().ok_or(Error::NotAuthenticated)?;

if let LoginMethod::User(login_method) = login_method {
if let LoginMethod::User(login_method) = login_method.as_ref() {
match login_method {
UserLoginMethod::Username { email, kdf, .. }
| UserLoginMethod::ApiKey { email, kdf, .. } => {
Expand All @@ -45,12 +42,9 @@ pub(crate) fn validate_password_user_key(
) -> Result<String> {
use bitwarden_core::VaultLocked;

let login_method = client
.login_method
.as_ref()
.ok_or(Error::NotAuthenticated)?;
let login_method = client.get_login_method().ok_or(Error::NotAuthenticated)?;

if let LoginMethod::User(login_method) = login_method {
if let LoginMethod::User(login_method) = login_method.as_ref() {
match login_method {
UserLoginMethod::Username { email, kdf, .. }
| UserLoginMethod::ApiKey { email, kdf, .. } => {
Expand Down
37 changes: 26 additions & 11 deletions crates/bitwarden/src/auth/renew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,49 @@ use crate::{
pub(crate) async fn renew_token(client: &Client) -> Result<()> {
const TOKEN_RENEW_MARGIN_SECONDS: i64 = 5 * 60;

if let (Some(expires), Some(login_method)) = (&client.token_expires_on, &client.login_method) {
let expires = *client
.token_expires_on
.read()
.expect("RwLock is not poisoned");
let login_method = client
.login_method
.read()
.expect("RwLock is not poisoned")
.clone();

if let (Some(expires), Some(login_method)) = (expires, login_method) {
if Utc::now().timestamp() < expires - TOKEN_RENEW_MARGIN_SECONDS {
return Ok(());
}

let res = match login_method {
let config = client
.__api_configurations
.read()
.expect("RwLock is not poisoned")
.clone();

Check warning on line 34 in crates/bitwarden/src/auth/renew.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/renew.rs#L30-L34

Added lines #L30 - L34 were not covered by tests

let res = match login_method.as_ref() {

Check warning on line 36 in crates/bitwarden/src/auth/renew.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/renew.rs#L36

Added line #L36 was not covered by tests
#[cfg(feature = "internal")]
LoginMethod::User(u) => match u {
UserLoginMethod::Username { client_id, .. } => {
let refresh = client
.refresh_token
.as_deref()
.read()
.expect("RwLock is not poisoned")
.clone()

Check warning on line 44 in crates/bitwarden/src/auth/renew.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/renew.rs#L42-L44

Added lines #L42 - L44 were not covered by tests
.ok_or(Error::NotAuthenticated)?;

crate::auth::api::request::RenewTokenRequest::new(
refresh.to_owned(),
client_id.to_owned(),
)
.send(&client.__api_configurations)
.await?
crate::auth::api::request::RenewTokenRequest::new(refresh, client_id.to_owned())
.send(&config)
.await?

Check warning on line 49 in crates/bitwarden/src/auth/renew.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/renew.rs#L47-L49

Added lines #L47 - L49 were not covered by tests
}
UserLoginMethod::ApiKey {
client_id,
client_secret,
..
} => {
ApiTokenRequest::new(client_id, client_secret)
.send(&client.__api_configurations)
.send(&config)

Check warning on line 57 in crates/bitwarden/src/auth/renew.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/renew.rs#L57

Added line #L57 was not covered by tests
.await?
}
},
Expand All @@ -53,7 +68,7 @@ pub(crate) async fn renew_token(client: &Client) -> Result<()> {
access_token.access_token_id,
&access_token.client_secret,
)
.send(&client.__api_configurations)
.send(&config)

Check warning on line 71 in crates/bitwarden/src/auth/renew.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/renew.rs#L71

Added line #L71 was not covered by tests
.await?;

if let (IdentityTokenResponse::Payload(r), Some(state_file), Ok(enc_settings)) =
Expand Down
Loading

0 comments on commit f61d104

Please sign in to comment.