Skip to content

Commit

Permalink
Move login methods to auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton committed Nov 2, 2023
1 parent 0dd1d95 commit d878a97
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 55 deletions.
8 changes: 5 additions & 3 deletions crates/bitwarden-json/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ impl Client {

match cmd {
#[cfg(feature = "internal")]
Command::PasswordLogin(req) => self.0.password_login(&req).await.into_string(),
Command::PasswordLogin(req) => self.0.auth().password_login(&req).await.into_string(),
#[cfg(feature = "secrets")]
Command::AccessTokenLogin(req) => self.0.access_token_login(&req).await.into_string(),
Command::AccessTokenLogin(req) => {
self.0.auth().access_token_login(&req).await.into_string()
}
#[cfg(feature = "internal")]
Command::GetUserApiKey(req) => self.0.get_user_api_key(&req).await.into_string(),
#[cfg(feature = "internal")]
Command::ApiKeyLogin(req) => self.0.api_key_login(&req).await.into_string(),
Command::ApiKeyLogin(req) => self.0.auth().api_key_login(&req).await.into_string(),
#[cfg(feature = "internal")]
Command::Sync(req) => self.0.sync(&req).await.into_string(),
#[cfg(feature = "internal")]
Expand Down
4 changes: 4 additions & 0 deletions crates/bitwarden/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Changed

- `client.access_token_login()` moved to `client.auth().access_token_login()`

## [0.3.1] - 2023-10-13

### Changed
Expand Down
51 changes: 50 additions & 1 deletion crates/bitwarden/src/auth/client_auth.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
use super::{
password::{password_strength, satisfies_policy, MasterPasswordPolicyOptions},
register::{make_register_keys, register},
RegisterKeyResponse, RegisterRequest,
RegisterKeyResponse, RegisterRequest, renew::renew_token,
};
use crate::{client::kdf::Kdf, error::Result, Client};

#[cfg(feature = "secrets")]
use crate::auth::login::{access_token_login, AccessTokenLoginRequest, AccessTokenLoginResponse};

#[cfg(feature = "internal")]
use crate::auth::login::{
api_key_login, password_login, send_two_factor_email, ApiKeyLoginRequest, ApiKeyLoginResponse,
PasswordLoginRequest, PasswordLoginResponse, TwoFactorEmailRequest,
};

pub struct ClientAuth<'a> {
pub(crate) client: &'a mut crate::Client,
}
Expand Down Expand Up @@ -37,10 +46,50 @@ impl<'a> ClientAuth<'a> {
make_register_keys(email, password, kdf)
}

pub async fn renew_token(&mut self) -> Result<()> {
renew_token(self.client).await
}

#[cfg(feature = "internal")]
pub async fn register(&mut self, input: &RegisterRequest) -> Result<()> {
register(self.client, input).await
}

#[cfg(feature = "internal")]
pub async fn prelogin(&mut self, email: String) -> Result<Kdf> {
use crate::auth::login::request_prelogin;

request_prelogin(self.client, email).await?.try_into()
}

#[cfg(feature = "internal")]
pub async fn password_login(
&mut self,
input: &PasswordLoginRequest,
) -> Result<PasswordLoginResponse> {
password_login(self.client, input).await
}

#[cfg(feature = "internal")]
pub async fn api_key_login(
&mut self,
input: &ApiKeyLoginRequest,
) -> Result<ApiKeyLoginResponse> {
api_key_login(self.client, input).await
}

#[cfg(feature = "secrets")]
pub async fn access_token_login(
&mut self,
input: &AccessTokenLoginRequest,
) -> Result<AccessTokenLoginResponse> {
access_token_login(self.client, input).await
}

#[cfg(feature = "internal")]
pub async fn send_two_factor_email(&mut self, tf: &TwoFactorEmailRequest) -> Result<()> {
send_two_factor_email(self.client, tf).await
}
}

impl<'a> Client {
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 @@ -30,7 +30,7 @@ pub(crate) async fn api_key_login(
.email
.ok_or(Error::Internal("Access token doesn't contain email"))?;

let kdf = client.prelogin(email.clone()).await?;
let kdf = client.auth().prelogin(email.clone()).await?;

client.set_tokens(
r.access_token.clone(),
Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden/src/auth/login/two_factor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub(crate) async fn send_two_factor_email(
input: &TwoFactorEmailRequest,
) -> Result<()> {
// TODO: This should be resolved from the client
let kdf = client.prelogin(input.email.clone()).await?;
let kdf = client.auth().prelogin(input.email.clone()).await?;

let password_hash = determine_password_hash(&input.email, &kdf, &input.password).await?;

Expand Down
50 changes: 2 additions & 48 deletions crates/bitwarden/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@ use std::time::{Duration, Instant};
use reqwest::header::{self};
use uuid::Uuid;

#[cfg(feature = "secrets")]
use crate::auth::login::{access_token_login, AccessTokenLoginRequest, AccessTokenLoginResponse};
#[cfg(feature = "internal")]
use crate::{
auth::login::{
api_key_login, password_login, send_two_factor_email, ApiKeyLoginRequest,
ApiKeyLoginResponse, PasswordLoginRequest, PasswordLoginResponse, TwoFactorEmailRequest,
},
client::kdf::Kdf,
crypto::EncString,
platform::{
Expand All @@ -19,7 +13,6 @@ use crate::{
},
};
use crate::{
auth::renew::renew_token,
client::{
client_settings::{ClientSettings, DeviceType},
encryption_settings::EncryptionSettings,
Expand Down Expand Up @@ -133,41 +126,10 @@ impl Client {
pub(crate) async fn get_api_configurations(&mut self) -> &ApiConfigurations {
// At the moment we ignore the error result from the token renewal, if it fails,
// the token will end up expiring and the next operation is going to fail anyway.
self.renew_token().await.ok();
self.auth().renew_token().await.ok();
&self.__api_configurations
}

#[cfg(feature = "internal")]
pub async fn prelogin(&mut self, email: String) -> Result<Kdf> {
use crate::auth::login::request_prelogin;

request_prelogin(self, email).await?.try_into()
}

#[cfg(feature = "internal")]
pub async fn password_login(
&mut self,
input: &PasswordLoginRequest,
) -> Result<PasswordLoginResponse> {
password_login(self, input).await
}

#[cfg(feature = "internal")]
pub async fn api_key_login(
&mut self,
input: &ApiKeyLoginRequest,
) -> Result<ApiKeyLoginResponse> {
api_key_login(self, input).await
}

#[cfg(feature = "secrets")]
pub async fn access_token_login(
&mut self,
input: &AccessTokenLoginRequest,
) -> Result<AccessTokenLoginResponse> {
access_token_login(self, input).await
}

#[cfg(feature = "internal")]
pub async fn sync(&mut self, input: &SyncRequest) -> Result<SyncResponse> {
sync(self, input).await
Expand Down Expand Up @@ -223,10 +185,6 @@ impl Client {
self.__api_configurations.api.oauth_access_token = Some(token);
}

pub async fn renew_token(&mut self) -> Result<()> {
renew_token(self).await
}

#[cfg(feature = "internal")]
pub fn is_authed(&self) -> bool {
self.token.is_some() || self.login_method.is_some()
Expand Down Expand Up @@ -279,11 +237,6 @@ impl Client {
pub fn fingerprint(&mut self, input: &FingerprintRequest) -> Result<FingerprintResponse> {
generate_fingerprint(input)
}

#[cfg(feature = "internal")]
pub async fn send_two_factor_email(&mut self, tf: &TwoFactorEmailRequest) -> Result<()> {
send_two_factor_email(self, tf).await
}
}

#[cfg(test)]
Expand Down Expand Up @@ -347,6 +300,7 @@ mod tests {

// Test the login is correct and we store the returned organization ID correctly
let res = client
.auth()
.access_token_login(&AccessTokenLoginRequest {
access_token: "0.ec2c1d46-6a4b-4751-a310-af9601317f2d.C2IgxjjLF7qSshsbwe8JGcbM075YXw:X8vbvA0bduihIDe/qrzIQQ==".into(),
})
Expand Down
6 changes: 5 additions & 1 deletion crates/bw/src/auth/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ pub(crate) async fn password_login(mut client: Client, email: Option<String>) ->

let password = Password::new("Password").without_confirmation().prompt()?;

let kdf = client.prelogin(email.clone()).await?;
let kdf = client.auth().prelogin(email.clone()).await?;

let result = client
.auth()
.password_login(&PasswordLoginRequest {
email: email.clone(),
password: password.clone(),
Expand Down Expand Up @@ -45,6 +46,7 @@ pub(crate) async fn password_login(mut client: Client, email: Option<String>) ->
} else if let Some(tf) = two_factor.email {
// Send token
client
.auth()
.send_two_factor_email(&TwoFactorEmailRequest {
email: email.clone(),
password: password.clone(),
Expand All @@ -64,6 +66,7 @@ pub(crate) async fn password_login(mut client: Client, email: Option<String>) ->
};

let result = client
.auth()
.password_login(&PasswordLoginRequest {
email,
password,
Expand Down Expand Up @@ -91,6 +94,7 @@ pub(crate) async fn api_key_login(
let password = Password::new("Password").without_confirmation().prompt()?;

let result = client
.auth()
.api_key_login(&ApiKeyLoginRequest {
client_id,
client_secret,
Expand Down
1 change: 1 addition & 0 deletions crates/bws/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ async fn process_commands() -> Result<()> {

// Load session or return if no session exists
let _ = client
.auth()
.access_token_login(&AccessTokenLoginRequest { access_token })
.await?;

Expand Down

0 comments on commit d878a97

Please sign in to comment.