Skip to content

Commit c3962e5

Browse files
authored
[PM-17907] Remove core::Error from bitwarden-exporters (#138)
## ๐ŸŽŸ๏ธ Tracking <!-- Paste the link to the Jira or GitHub issue or otherwise describe / point to where this change is coming from. --> https://bitwarden.atlassian.net/browse/PM-17907 ## ๐Ÿ“” Objective <!-- Describe what the purpose of this PR is, for example what bug you're fixing or new feature you're adding. --> - Extract `NotAuthenticatedError` as a separate error struct. - Depend on the new `NotAuthenticatedError` instead of `bitwarden_core::Error` in `bitwarden-exporters`. ## โฐ Reminders before review - Contributor guidelines followed - All formatters and local linters executed and passed - Written new unit and / or integration tests where applicable - Protected functional changes with optionality (feature flags) - Used internationalization (i18n) for all UI strings - CI builds passed - Communicated to DevOps any deployment requirements - Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team ## ๐Ÿฆฎ Reviewer guidelines <!-- Suggested interactions but feel free to use (or not) as you desire! --> - ๐Ÿ‘ (`:+1:`) or similar for great changes - ๐Ÿ“ (`:memo:`) or โ„น๏ธ (`:information_source:`) for notes or general info - โ“ (`:question:`) for questions - ๐Ÿค” (`:thinking:`) or ๐Ÿ’ญ (`:thought_balloon:`) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion - ๐ŸŽจ (`:art:`) for suggestions / improvements - โŒ (`:x:`) or โš ๏ธ (`:warning:`) for more significant problems or concerns needing attention - ๐ŸŒฑ (`:seedling:`) or โ™ป๏ธ (`:recycle:`) for future improvements or indications of technical debt - โ› (`:pick:`) for minor or nitpick changes
1 parent b136cf3 commit c3962e5

File tree

9 files changed

+33
-30
lines changed

9 files changed

+33
-30
lines changed

โ€Žcrates/bitwarden-core/src/auth/password/validate.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use bitwarden_crypto::{HashPurpose, MasterKey};
33
use crate::{
44
auth::determine_password_hash,
55
client::{LoginMethod, UserLoginMethod},
6-
error::{Error, Result},
6+
error::{NotAuthenticatedError, Result},
77
Client,
88
};
99

@@ -16,7 +16,7 @@ pub(crate) fn validate_password(
1616
let login_method = client
1717
.internal
1818
.get_login_method()
19-
.ok_or(Error::NotAuthenticated)?;
19+
.ok_or(NotAuthenticatedError)?;
2020

2121
#[allow(irrefutable_let_patterns)]
2222
if let LoginMethod::User(login_method) = login_method.as_ref() {
@@ -34,7 +34,7 @@ pub(crate) fn validate_password(
3434
}
3535
}
3636
} else {
37-
Err(Error::NotAuthenticated)
37+
Err(NotAuthenticatedError)?
3838
}
3939
}
4040

@@ -47,7 +47,7 @@ pub(crate) fn validate_password_user_key(
4747
let login_method = client
4848
.internal
4949
.get_login_method()
50-
.ok_or(Error::NotAuthenticated)?;
50+
.ok_or(NotAuthenticatedError)?;
5151

5252
#[allow(irrefutable_let_patterns)]
5353
if let LoginMethod::User(login_method) = login_method.as_ref() {
@@ -72,7 +72,7 @@ pub(crate) fn validate_password_user_key(
7272
}
7373
}
7474
} else {
75-
Err(Error::NotAuthenticated)
75+
Err(NotAuthenticatedError)?
7676
}
7777
}
7878

โ€Žcrates/bitwarden-core/src/auth/pin.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use bitwarden_crypto::{EncString, PinKey};
22

33
use crate::{
44
client::{LoginMethod, UserLoginMethod},
5-
error::{Error, Result},
5+
error::{NotAuthenticatedError, Result},
66
Client,
77
};
88

@@ -14,11 +14,11 @@ pub(crate) fn validate_pin(
1414
let login_method = client
1515
.internal
1616
.get_login_method()
17-
.ok_or(Error::NotAuthenticated)?;
17+
.ok_or(NotAuthenticatedError)?;
1818

1919
#[allow(irrefutable_let_patterns)]
2020
let LoginMethod::User(login_method) = login_method.as_ref() else {
21-
return Err(Error::NotAuthenticated);
21+
return Err(NotAuthenticatedError)?;
2222
};
2323

2424
match login_method {

โ€Žcrates/bitwarden-core/src/auth/renew.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
use crate::{
1010
auth::api::{request::ApiTokenRequest, response::IdentityTokenResponse},
1111
client::{internal::InternalClient, LoginMethod, UserLoginMethod},
12-
error::{Error, Result},
12+
error::{Error, NotAuthenticatedError, Result},
1313
};
1414

1515
pub(crate) async fn renew_token(client: &InternalClient) -> Result<()> {
@@ -40,7 +40,7 @@ pub(crate) async fn renew_token(client: &InternalClient) -> Result<()> {
4040
let res = match login_method.as_ref() {
4141
LoginMethod::User(u) => match u {
4242
UserLoginMethod::Username { client_id, .. } => {
43-
let refresh = tokens.refresh_token.ok_or(Error::NotAuthenticated)?;
43+
let refresh = tokens.refresh_token.ok_or(NotAuthenticatedError)?;
4444

4545
crate::auth::api::request::RenewTokenRequest::new(refresh, client_id.to_owned())
4646
.send(&config)
@@ -105,5 +105,5 @@ pub(crate) async fn renew_token(client: &InternalClient) -> Result<()> {
105105
}
106106
}
107107

108-
Err(Error::NotAuthenticated)
108+
Err(NotAuthenticatedError)?
109109
}

โ€Žcrates/bitwarden-core/src/client/internal.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
use crate::{
2020
client::encryption_settings::EncryptionSettingsError,
2121
client::{flags::Flags, login_method::UserLoginMethod},
22-
error::Error,
22+
error::NotAuthenticatedError,
2323
};
2424

2525
#[derive(Debug, Clone)]
@@ -138,7 +138,7 @@ impl InternalClient {
138138
}
139139

140140
#[cfg(feature = "internal")]
141-
pub fn get_kdf(&self) -> Result<Kdf> {
141+
pub fn get_kdf(&self) -> Result<Kdf, NotAuthenticatedError> {
142142
match self
143143
.login_method
144144
.read()
@@ -148,7 +148,7 @@ impl InternalClient {
148148
Some(LoginMethod::User(
149149
UserLoginMethod::Username { kdf, .. } | UserLoginMethod::ApiKey { kdf, .. },
150150
)) => Ok(kdf.clone()),
151-
_ => Err(Error::NotAuthenticated),
151+
_ => Err(NotAuthenticatedError),
152152
}
153153
}
154154

โ€Žcrates/bitwarden-core/src/error.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ pub enum Error {
1717
MissingFieldError(#[from] MissingFieldError),
1818
#[error(transparent)]
1919
VaultLocked(#[from] VaultLocked),
20-
21-
#[error("The client is not authenticated or the session has expired")]
22-
NotAuthenticated,
20+
#[error(transparent)]
21+
NotAuthenticated(#[from] NotAuthenticatedError),
2322

2423
#[error("Access token is not in a valid format: {0}")]
2524
AccessTokenInvalid(#[from] AccessTokenInvalidError),
@@ -138,6 +137,10 @@ impl_bitwarden_error!(ApiApisError, ApiError);
138137

139138
pub(crate) type Result<T, E = Error> = std::result::Result<T, E>;
140139

140+
#[derive(Debug, Error)]
141+
#[error("The client is not authenticated or the session has expired")]
142+
pub struct NotAuthenticatedError;
143+
141144
#[derive(Debug, Error)]
142145
#[error("The response received was missing a required field: {0}")]
143146
pub struct MissingFieldError(pub &'static str);

โ€Žcrates/bitwarden-core/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub mod admin_console;
88
pub mod auth;
99
pub mod client;
1010
mod error;
11-
pub use error::{ApiError, Error, MissingFieldError, VaultLocked};
11+
pub use error::{ApiError, Error, MissingFieldError, NotAuthenticatedError, VaultLocked};
1212
#[cfg(feature = "internal")]
1313
pub mod mobile;
1414
#[cfg(feature = "internal")]

โ€Žcrates/bitwarden-core/src/mobile/crypto.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use {tsify_next::Tsify, wasm_bindgen::prelude::*};
1212

1313
use crate::{
1414
client::{encryption_settings::EncryptionSettingsError, LoginMethod, UserLoginMethod},
15-
error::{Error, Result},
15+
error::{NotAuthenticatedError, Result},
1616
Client,
1717
};
1818

@@ -230,7 +230,7 @@ pub fn update_password(client: &Client, new_password: String) -> Result<UpdatePa
230230
let login_method = client
231231
.internal
232232
.get_login_method()
233-
.ok_or(Error::NotAuthenticated)?;
233+
.ok_or(NotAuthenticatedError)?;
234234

235235
// Derive a new master key from password
236236
let new_master_key = match login_method.as_ref() {
@@ -239,7 +239,7 @@ pub fn update_password(client: &Client, new_password: String) -> Result<UpdatePa
239239
| UserLoginMethod::ApiKey { email, kdf, .. },
240240
) => MasterKey::derive(&new_password, email, kdf)?,
241241
#[cfg(feature = "secrets")]
242-
LoginMethod::ServiceAccount(_) => return Err(Error::NotAuthenticated),
242+
LoginMethod::ServiceAccount(_) => return Err(NotAuthenticatedError)?,
243243
};
244244

245245
let new_key = new_master_key.encrypt_user_key(user_key)?;
@@ -272,7 +272,7 @@ pub fn derive_pin_key(client: &Client, pin: String) -> Result<DerivePinKeyRespon
272272
let login_method = client
273273
.internal
274274
.get_login_method()
275-
.ok_or(Error::NotAuthenticated)?;
275+
.ok_or(NotAuthenticatedError)?;
276276

277277
let pin_protected_user_key = derive_pin_protected_user_key(&pin, &login_method, user_key)?;
278278

@@ -290,7 +290,7 @@ pub fn derive_pin_user_key(client: &Client, encrypted_pin: EncString) -> Result<
290290
let login_method = client
291291
.internal
292292
.get_login_method()
293-
.ok_or(Error::NotAuthenticated)?;
293+
.ok_or(NotAuthenticatedError)?;
294294

295295
derive_pin_protected_user_key(&pin, &login_method, user_key)
296296
}
@@ -308,7 +308,7 @@ fn derive_pin_protected_user_key(
308308
| UserLoginMethod::ApiKey { email, kdf, .. },
309309
) => PinKey::derive(pin.as_bytes(), email.as_bytes(), kdf)?,
310310
#[cfg(feature = "secrets")]
311-
LoginMethod::ServiceAccount(_) => return Err(Error::NotAuthenticated),
311+
LoginMethod::ServiceAccount(_) => return Err(NotAuthenticatedError)?,
312312
};
313313

314314
Ok(derived_key.encrypt_user_key(user_key)?)

โ€Žcrates/bitwarden-core/src/platform/get_user_api_key.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize};
1212
use super::SecretVerificationRequest;
1313
use crate::{
1414
client::{LoginMethod, UserLoginMethod},
15-
error::{Error, Result},
15+
error::{NotAuthenticatedError, Result},
1616
require, Client,
1717
};
1818

@@ -32,14 +32,14 @@ pub(crate) async fn get_user_api_key(
3232
UserApiKeyResponse::process_response(response)
3333
}
3434

35-
fn get_login_method(client: &Client) -> Result<Arc<LoginMethod>> {
35+
fn get_login_method(client: &Client) -> Result<Arc<LoginMethod>, NotAuthenticatedError> {
3636
if client.internal.is_authed() {
3737
client
3838
.internal
3939
.get_login_method()
40-
.ok_or(Error::NotAuthenticated)
40+
.ok_or(NotAuthenticatedError)
4141
} else {
42-
Err(Error::NotAuthenticated)
42+
Err(NotAuthenticatedError)
4343
}
4444
}
4545

โ€Žcrates/bitwarden-exporters/src/error.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ pub enum ExportError {
55
#[error(transparent)]
66
MissingField(#[from] bitwarden_core::MissingFieldError),
77
#[error(transparent)]
8+
NotAuthenticated(#[from] bitwarden_core::NotAuthenticatedError),
9+
#[error(transparent)]
810
VaultLocked(#[from] bitwarden_core::VaultLocked),
911

1012
#[error("CSV error: {0}")]
@@ -16,8 +18,6 @@ pub enum ExportError {
1618
#[error("Encrypted JSON error: {0}")]
1719
EncryptedJsonError(#[from] crate::encrypted_json::EncryptedJsonError),
1820

19-
#[error(transparent)]
20-
BitwardenError(#[from] bitwarden_core::Error),
2121
#[error(transparent)]
2222
BitwardenCryptoError(#[from] bitwarden_crypto::CryptoError),
2323
#[error(transparent)]

0 commit comments

Comments
ย (0)