From b0243ce4d51a2119228a64530f560b5c7229b7f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa?= Date: Tue, 12 Dec 2023 17:57:23 +0100 Subject: [PATCH] Split set_login_method from set_tokens (#425) ## Type of change ``` - [ ] Bug fix - [ ] New feature development - [x] Tech debt (refactoring, code cleanup, dependency upgrades, etc) - [ ] Build/deploy pipeline (DevOps) - [ ] Other ``` ## Objective There's no reason for these two to be combined, and it forces `renew_tokens` to clone login_method every time --- crates/bitwarden/src/auth/login/access_token.rs | 8 +++++--- crates/bitwarden/src/auth/login/api_key.rs | 12 ++++++------ crates/bitwarden/src/auth/login/password.rs | 10 +++++----- crates/bitwarden/src/auth/renew.rs | 6 ++---- crates/bitwarden/src/client/client.rs | 9 +++------ 5 files changed, 21 insertions(+), 24 deletions(-) diff --git a/crates/bitwarden/src/auth/login/access_token.rs b/crates/bitwarden/src/auth/login/access_token.rs index 8d5996119..147480576 100644 --- a/crates/bitwarden/src/auth/login/access_token.rs +++ b/crates/bitwarden/src/auth/login/access_token.rs @@ -58,12 +58,14 @@ pub(crate) async fn login_access_token( r.access_token.clone(), r.refresh_token.clone(), r.expires_in, - LoginMethod::ServiceAccount(ServiceAccountLoginMethod::AccessToken { + ); + client.set_login_method(LoginMethod::ServiceAccount( + ServiceAccountLoginMethod::AccessToken { access_token_id: access_token.access_token_id, client_secret: access_token.client_secret, organization_id, - }), - ); + }, + )); client.initialize_crypto_single_key(encryption_key); } diff --git a/crates/bitwarden/src/auth/login/api_key.rs b/crates/bitwarden/src/auth/login/api_key.rs index db5cbe01d..6f75b0033 100644 --- a/crates/bitwarden/src/auth/login/api_key.rs +++ b/crates/bitwarden/src/auth/login/api_key.rs @@ -36,13 +36,13 @@ pub(crate) async fn login_api_key( r.access_token.clone(), r.refresh_token.clone(), r.expires_in, - LoginMethod::User(UserLoginMethod::ApiKey { - client_id: input.client_id.to_owned(), - client_secret: input.client_secret.to_owned(), - email, - kdf, - }), ); + client.set_login_method(LoginMethod::User(UserLoginMethod::ApiKey { + client_id: input.client_id.to_owned(), + client_secret: input.client_secret.to_owned(), + email, + kdf, + })); let user_key: EncString = r.key.as_deref().unwrap().parse().unwrap(); let private_key: EncString = r.private_key.as_deref().unwrap().parse().unwrap(); diff --git a/crates/bitwarden/src/auth/login/password.rs b/crates/bitwarden/src/auth/login/password.rs index 08d7d2054..7a93c68e0 100644 --- a/crates/bitwarden/src/auth/login/password.rs +++ b/crates/bitwarden/src/auth/login/password.rs @@ -42,12 +42,12 @@ pub(crate) async fn login_password( r.access_token.clone(), r.refresh_token.clone(), r.expires_in, - LoginMethod::User(UserLoginMethod::Username { - client_id: "web".to_owned(), - email: input.email.to_owned(), - kdf: input.kdf.to_owned(), - }), ); + client.set_login_method(LoginMethod::User(UserLoginMethod::Username { + client_id: "web".to_owned(), + email: input.email.to_owned(), + kdf: input.kdf.to_owned(), + })); let user_key: EncString = r.key.as_deref().unwrap().parse().unwrap(); let private_key: EncString = r.private_key.as_deref().unwrap().parse().unwrap(); diff --git a/crates/bitwarden/src/auth/renew.rs b/crates/bitwarden/src/auth/renew.rs index 7973947ab..f215024d2 100644 --- a/crates/bitwarden/src/auth/renew.rs +++ b/crates/bitwarden/src/auth/renew.rs @@ -57,13 +57,11 @@ pub(crate) async fn renew_token(client: &mut Client) -> Result<()> { match res { IdentityTokenResponse::Refreshed(r) => { - let login_method = login_method.to_owned(); - client.set_tokens(r.access_token, r.refresh_token, r.expires_in, login_method); + client.set_tokens(r.access_token, r.refresh_token, r.expires_in); return Ok(()); } IdentityTokenResponse::Authenticated(r) => { - let login_method = login_method.to_owned(); - client.set_tokens(r.access_token, r.refresh_token, r.expires_in, login_method); + client.set_tokens(r.access_token, r.refresh_token, r.expires_in); return Ok(()); } _ => { diff --git a/crates/bitwarden/src/client/client.rs b/crates/bitwarden/src/client/client.rs index 2165b8dc7..fbbfb8798 100644 --- a/crates/bitwarden/src/client/client.rs +++ b/crates/bitwarden/src/client/client.rs @@ -29,7 +29,7 @@ pub(crate) struct ApiConfigurations { pub device_type: DeviceType, } -#[derive(Debug, Clone)] +#[derive(Debug)] pub(crate) enum LoginMethod { #[cfg(feature = "internal")] User(UserLoginMethod), @@ -38,7 +38,7 @@ pub(crate) enum LoginMethod { ServiceAccount(ServiceAccountLoginMethod), } -#[derive(Debug, Clone)] +#[derive(Debug)] #[cfg(feature = "internal")] pub(crate) enum UserLoginMethod { Username { @@ -55,7 +55,7 @@ pub(crate) enum UserLoginMethod { }, } -#[derive(Debug, Clone)] +#[derive(Debug)] pub(crate) enum ServiceAccountLoginMethod { AccessToken { access_token_id: Uuid, @@ -172,7 +172,6 @@ impl Client { self.encryption_settings.as_ref().ok_or(Error::VaultLocked) } - #[cfg(feature = "mobile")] pub(crate) fn set_login_method(&mut self, login_method: LoginMethod) { use log::debug; @@ -185,12 +184,10 @@ impl Client { token: String, refresh_token: Option, expires_in: u64, - login_method: LoginMethod, ) { self.token = Some(token.clone()); self.refresh_token = refresh_token; self.token_expires_in = Some(Utc::now().timestamp() + expires_in as i64); - self.login_method = Some(login_method); self.__api_configurations.identity.oauth_access_token = Some(token.clone()); self.__api_configurations.api.oauth_access_token = Some(token); }