Skip to main content

doido_auth/
error.rs

1//! Typed auth errors (`thiserror` per crate, per the framework convention).
2
3/// Errors raised by the auth layer.
4#[derive(Debug, thiserror::Error)]
5pub enum AuthError {
6    /// Email/password combination is wrong or the account has no password.
7    #[error("auth: invalid credentials")]
8    InvalidCredentials,
9
10    /// Registration attempted with an email that is already taken.
11    #[error("auth: email already taken")]
12    EmailTaken,
13
14    /// A `validatable`-module check failed (email format, password length, …).
15    #[error("auth: {0}")]
16    Validation(String),
17
18    /// Sign-in blocked because the account's email is not confirmed
19    /// (`confirmable` module).
20    #[error("auth: email not confirmed")]
21    NotConfirmed,
22
23    /// Sign-in blocked because the account is locked (`lockable` module).
24    #[error("auth: account locked")]
25    AccountLocked,
26
27    /// No authenticated identity was found on the request.
28    #[error("auth: unauthorized")]
29    Unauthorized,
30
31    /// A bearer or JWT token is missing or malformed.
32    #[error("auth: invalid token")]
33    InvalidToken,
34
35    /// JWT verification failed (wrong secret, expired, etc.).
36    #[error("auth: jwt error: {0}")]
37    Jwt(String),
38
39    /// OAuth provider or callback failed.
40    #[error("auth: oauth error: {0}")]
41    OAuth(String),
42
43    /// Two-factor verification failed.
44    #[cfg(feature = "auth-2fa")]
45    #[error("auth: two-factor error: {0}")]
46    TwoFactor(String),
47
48    /// The `auth` configuration is invalid or incomplete.
49    #[error("auth: config error: {0}")]
50    Config(String),
51
52    /// An unknown auth strategy was referenced in config.
53    #[error("auth: unknown strategy: {0}")]
54    UnknownStrategy(String),
55
56    /// Database or internal failure.
57    #[error("auth: internal error: {0}")]
58    Internal(String),
59}
60
61impl From<jsonwebtoken::errors::Error> for AuthError {
62    fn from(e: jsonwebtoken::errors::Error) -> Self {
63        AuthError::Jwt(e.to_string())
64    }
65}