Skip to content

Commit 032e7ee

Browse files
committed
Tidy handling of NotAuthenticated error variant.
1 parent 4febbe3 commit 032e7ee

File tree

15 files changed

+85
-49
lines changed

15 files changed

+85
-49
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/account/src/error.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ pub enum Error {
1010
#[error("could not find folder password for '{0}'")]
1111
NoFolderPassword(VaultId),
1212

13-
/// Error generated accessing an account that is not
14-
/// authenticated.
15-
#[error("account not authenticated, sign in required")]
16-
NotAuthenticated,
17-
1813
/// Error generated when a database is required.
1914
#[error("database client is required")]
2015
NoDatabase,
@@ -77,6 +72,10 @@ pub enum Error {
7772
#[error(transparent)]
7873
Core(#[from] sos_core::Error),
7974

75+
/// Authentication errors.
76+
#[error(transparent)]
77+
Authentication(#[from] sos_core::AuthenticationError),
78+
8079
#[cfg(feature = "search")]
8180
/// Error generated by the search library.
8281
#[error(transparent)]

crates/account/src/local_account.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ use sos_core::{
2828
AccountEvent, DeviceEvent, Event, EventKind, EventLog, EventRecord,
2929
ReadEvent, WriteEvent,
3030
},
31-
AccountId, AccountRef, FolderRef, Paths, SecretId, UtcDateTime,
32-
VaultCommit, VaultId,
31+
AccountId, AccountRef, AuthenticationError, FolderRef, Paths, SecretId,
32+
UtcDateTime, VaultCommit, VaultId,
3333
};
3434
use sos_login::{
3535
device::{DeviceManager, DeviceSigner},
@@ -110,7 +110,7 @@ impl LocalAccount {
110110
fn ensure_authenticated(&self) -> Result<()> {
111111
let is_authenticated = self.storage.is_authenticated();
112112
if !is_authenticated {
113-
return Err(Error::NotAuthenticated);
113+
return Err(AuthenticationError::NotAuthenticated.into());
114114
}
115115
Ok(())
116116
}

crates/backend/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ futures.workspace = true
5454
tokio.workspace = true
5555
tokio-util.workspace = true
5656
binary-stream.workspace = true
57-
uuid.workspace = true
5857
tracing.workspace = true
5958
indexmap.workspace = true
6059
tempfile.workspace = true

crates/backend/src/error.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use thiserror::Error;
2-
use uuid::Uuid;
32

43
/// Errors generated by the library.
54
#[derive(Debug, Error)]
@@ -44,10 +43,5 @@ pub enum Error {
4443
SystemMessages(#[from] sos_system_messages::Error),
4544
}
4645

47-
/// Generic storage error shared between the client and server.
48-
#[derive(Debug, Error)]
49-
pub enum StorageError {
50-
/// Error generated attempting to access a vault that is not available.
51-
#[error("cache not available for {0}")]
52-
CacheNotAvailable(Uuid),
53-
}
46+
// Backwards compatible
47+
pub use sos_core::StorageError;

crates/core/src/error.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Errors generated by the core library.
22
use thiserror::Error;
33

4+
use crate::VaultId;
5+
46
/// Error thrown by the core library.
57
#[derive(Debug, Error)]
68
pub enum Error {
@@ -148,3 +150,21 @@ pub trait ErrorExt {
148150
/// Whether this is a permission denied error.
149151
fn is_permission_denied(&self) -> bool;
150152
}
153+
154+
/// Generic storage error shared between the client and server.
155+
#[derive(Debug, Error)]
156+
pub enum StorageError {
157+
/// Error generated attempting to access a folder
158+
/// that is not available in-memory.
159+
#[error("cache not available for {0}")]
160+
CacheNotAvailable(VaultId),
161+
}
162+
163+
/// Authentication errors.
164+
#[derive(Debug, Error)]
165+
pub enum AuthenticationError {
166+
/// Error generated accessing an account that is not
167+
/// authenticated when authentication is required.
168+
#[error("account not authenticated, sign in required")]
169+
NotAuthenticated,
170+
}

crates/core/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub use account::AccountId;
2323
pub use date_time::UtcDateTime;
2424
// pub use device::{DevicePublicKey, TrustedDevice};
2525
pub use encoding::{decode, encode};
26-
pub use error::{Error, ErrorExt};
26+
pub use error::{AuthenticationError, Error, ErrorExt, StorageError};
2727
pub use file::{ExternalFile, ExternalFileName};
2828
pub use identity::{AccountRef, PublicIdentity};
2929
pub use origin::{Origin, RemoteOrigins};

crates/login/src/error.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ pub enum Error {
1313
#[error("could not find account {0}")]
1414
NoAccount(String),
1515

16-
/// Error generated accessing an account that is not
17-
/// authenticated.
18-
#[error("account not authenticated, sign in required")]
19-
NotAuthenticated,
20-
2116
/// Error generated when a folder password could not be located.
2217
#[error("could not find folder password for '{0}'")]
2318
NoFolderPassword(VaultId),
@@ -69,6 +64,10 @@ pub enum Error {
6964
#[error(transparent)]
7065
Core(#[from] sos_core::Error),
7166

67+
/// Authentication errors.
68+
#[error(transparent)]
69+
Authentication(#[from] sos_core::AuthenticationError),
70+
7271
/// Error generated by the backend library.
7372
#[error(transparent)]
7473
Backend(#[from] sos_backend::Error),

crates/login/src/identity.rs

+22-9
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use crate::{
1111
use secrecy::SecretString;
1212
use sos_backend::{database::async_sqlite::Client, BackendTarget};
1313
use sos_core::{
14-
crypto::AccessKey, decode, events::Event, AccountId, Paths, SecretId,
15-
VaultId,
14+
crypto::AccessKey, decode, events::Event, AccountId, AuthenticationError,
15+
Paths, SecretId, VaultId,
1616
};
1717
use sos_vault::{list_local_folders, read_public_identity, Summary, Vault};
1818
use sos_vfs as vfs;
@@ -81,29 +81,42 @@ impl Identity {
8181

8282
/// Device manager.
8383
pub fn devices(&self) -> Result<&DeviceManager> {
84-
self.identity
84+
Ok(self
85+
.identity
8586
.as_ref()
86-
.ok_or(Error::NotAuthenticated)?
87-
.devices()
87+
.ok_or(AuthenticationError::NotAuthenticated)?
88+
.devices()?)
8889
}
8990

9091
/// Account information.
9192
pub fn account(&self) -> Result<&PublicIdentity> {
92-
self.account.as_ref().ok_or(Error::NotAuthenticated)
93+
Ok(self
94+
.account
95+
.as_ref()
96+
.ok_or(AuthenticationError::NotAuthenticated)?)
9397
}
9498

9599
fn account_mut(&mut self) -> Result<&mut PublicIdentity> {
96-
self.account.as_mut().ok_or(Error::NotAuthenticated)
100+
Ok(self
101+
.account
102+
.as_mut()
103+
.ok_or(AuthenticationError::NotAuthenticated)?)
97104
}
98105

99106
/// Private identity.
100107
pub fn identity(&self) -> Result<&IdentityFolder> {
101-
self.identity.as_ref().ok_or(Error::NotAuthenticated)
108+
Ok(self
109+
.identity
110+
.as_ref()
111+
.ok_or(AuthenticationError::NotAuthenticated)?)
102112
}
103113

104114
#[doc(hidden)]
105115
pub fn identity_mut(&mut self) -> Result<&mut IdentityFolder> {
106-
self.identity.as_mut().ok_or(Error::NotAuthenticated)
116+
Ok(self
117+
.identity
118+
.as_mut()
119+
.ok_or(AuthenticationError::NotAuthenticated)?)
107120
}
108121

109122
/// Verify the access key for this account.

crates/login/src/identity_folder.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use sos_backend::{
1818
};
1919
use sos_core::{
2020
constants::LOGIN_AGE_KEY_URN, crypto::AccessKey, decode, encode,
21-
AccountId, Paths,
21+
AccountId, AuthenticationError, Paths,
2222
};
2323
use sos_password::diceware::generate_passphrase_words;
2424
use sos_signer::ed25519;
@@ -108,7 +108,10 @@ impl IdentityFolder {
108108

109109
/// Device manager.
110110
pub fn devices(&self) -> Result<&DeviceManager> {
111-
self.devices.as_ref().ok_or(Error::NotAuthenticated)
111+
Ok(self
112+
.devices
113+
.as_ref()
114+
.ok_or(AuthenticationError::NotAuthenticated)?)
112115
}
113116

114117
/// Rename this identity vault.

crates/net/src/account/network_account.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use sos_core::{
1818
AccountEvent, DeviceEvent, EventLog, EventLogType, EventRecord,
1919
ReadEvent, WriteEvent,
2020
},
21-
AccountId, AccountRef, FolderRef, Origin, Paths, PublicIdentity,
22-
RemoteOrigins, SecretId, UtcDateTime, VaultId,
21+
AccountId, AccountRef, AuthenticationError, FolderRef, Origin, Paths,
22+
PublicIdentity, RemoteOrigins, SecretId, UtcDateTime, VaultId,
2323
};
2424
use sos_login::device::{DeviceManager, DeviceSigner};
2525
use sos_protocol::{
@@ -460,7 +460,7 @@ impl NetworkAccount {
460460
#[cfg(feature = "files")]
461461
async fn start_file_transfers(&mut self) -> Result<()> {
462462
if !self.is_authenticated().await {
463-
return Err(sos_account::Error::NotAuthenticated.into());
463+
return Err(AuthenticationError::NotAuthenticated.into());
464464
}
465465

466466
if self.offline {
@@ -634,7 +634,7 @@ impl NetworkAccount {
634634
.file_transfers
635635
.as_ref()
636636
.map(|t| Arc::clone(&t.inflight))
637-
.ok_or_else(|| sos_account::Error::NotAuthenticated)?)
637+
.ok_or_else(|| AuthenticationError::NotAuthenticated)?)
638638
}
639639

640640
/// Convert file mutation events into file transfer queue entries.

crates/net/src/error.rs

+4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ pub enum Error {
7575
#[error(transparent)]
7676
Core(#[from] sos_core::Error),
7777

78+
/// Authentication errors.
79+
#[error(transparent)]
80+
Authentication(#[from] sos_core::AuthenticationError),
81+
7882
/// Error generated by the backend library.
7983
#[error(transparent)]
8084
Backend(#[from] sos_backend::Error),

crates/storage/client/src/error.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ use thiserror::Error;
55
/// Errors generated by the client storage library.
66
#[derive(Debug, Error)]
77
pub enum Error {
8-
/// Error generated accessing an account that is not
9-
/// authenticated.
10-
#[error("account not authenticated, sign in required")]
11-
NotAuthenticated,
12-
138
/// Error generated attempting to make changes to the current
149
/// vault but no vault is open.
1510
#[error("no vault is available, vault must be open")]
@@ -64,6 +59,10 @@ pub enum Error {
6459
#[error(transparent)]
6560
Core(#[from] sos_core::Error),
6661

62+
/// Authentication errors.
63+
#[error(transparent)]
64+
Authentication(#[from] sos_core::AuthenticationError),
65+
6766
#[cfg(feature = "search")]
6867
/// Errors generated by the search library.
6968
#[error(transparent)]

crates/storage/client/src/filesystem/mod.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use sos_core::{
2424
patch::FolderPatch, AccountEvent, Event, EventLog, EventRecord,
2525
ReadEvent, WriteEvent,
2626
},
27-
AccountId, FolderRef, Paths, UtcDateTime,
27+
AccountId, AuthenticationError, FolderRef, Paths, UtcDateTime,
2828
};
2929
use sos_login::{FolderKeys, Identity};
3030
use sos_password::diceware::generate_passphrase;
@@ -1417,11 +1417,17 @@ impl ClientAccountStorage for ClientFileSystemStorage {
14171417
}
14181418

14191419
fn authenticated_user(&self) -> Result<&Identity> {
1420-
self.authenticated.as_ref().ok_or(Error::NotAuthenticated)
1420+
Ok(self
1421+
.authenticated
1422+
.as_ref()
1423+
.ok_or(AuthenticationError::NotAuthenticated)?)
14211424
}
14221425

14231426
fn authenticated_user_mut(&mut self) -> Result<&mut Identity> {
1424-
self.authenticated.as_mut().ok_or(Error::NotAuthenticated)
1427+
Ok(self
1428+
.authenticated
1429+
.as_mut()
1430+
.ok_or(AuthenticationError::NotAuthenticated)?)
14251431
}
14261432

14271433
fn is_authenticated(&self) -> bool {
@@ -1445,7 +1451,9 @@ impl ClientAccountStorage for ClientFileSystemStorage {
14451451
&mut self,
14461452
vault: Vault,
14471453
) -> Result<AccountEvent> {
1448-
self.authenticated.as_ref().ok_or(Error::NotAuthenticated)?;
1454+
self.authenticated
1455+
.as_ref()
1456+
.ok_or(AuthenticationError::NotAuthenticated)?;
14491457

14501458
// Update the identity vault
14511459
let buffer = encode(&vault).await?;

crates/vault/src/vault.rs

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use sos_vfs::File;
2222
use std::io::Cursor;
2323
use std::{
2424
borrow::Cow, cmp::Ordering, collections::HashMap, fmt, path::Path,
25-
str::FromStr,
2625
};
2726
use tokio::io::{AsyncReadExt, AsyncSeek, BufReader};
2827
use typeshare::typeshare;

0 commit comments

Comments
 (0)