authlogic/
errors.rs

1use actix_web::http::StatusCode;
2
3#[derive(Debug)]
4pub enum Error {
5    /// Indicates that the user is authenticated but cannot continue until they
6    /// verify their email address. This can happen if a user registers a new
7    /// account with a password, then uses their password to log in before
8    /// completing the email verification challenge.
9    EmailNotVerified,
10
11    /// Indicates that the user tried to complete an email challenge, but the
12    /// code in their challenge link is not correct. The challenge might
13    /// already have been completed, or expired, or it never existed.
14    IncorrectChallengeCode,
15
16    /// Indicates that the user did not provide a correct password when
17    /// attempting to authenticate.
18    IncorrectPassword,
19
20    /// Indicates that the user did not provide a correct identifier (e.g.
21    /// username or email address) when attempting to log in. Usually this
22    /// should be handled the same way as `IncorrectPassword`, to avoid leaking
23    /// information about which accounts exist. However, some apps may wish to
24    /// give more specific feedback to improve the user experience.
25    NoSuchUser,
26
27    /// Indicates that the user is not authenticated in a context where they
28    /// need to be.
29    NotAuthenticated,
30
31    /// Indicates that the user chose a password which is shorter than
32    /// `AppConfig::minimum_password_length()`.
33    PasswordTooShort,
34
35    /// Indicates that, when changing their password, the user chose a new
36    /// password which is the same as the old one.
37    PasswordsNotDifferent,
38
39    /// Indicates that the user is authenticated but cannot continue until they
40    /// choose a new password. This can happen if a new user is created with a
41    /// temporary password, or the user completed a password reset challenge.
42    RequirePasswordChange,
43
44    /// Indicates that the user attempted to log in using a password, but the
45    /// user account has no password associated with it.
46    UserHasNoPassword,
47
48    /// Indicates that the user is authenticated but cannot continue, because
49    /// their account has been suspended.
50    UserIsSuspended,
51
52    /// Internal error which occurs when an authenticated user attempts to
53    /// reauthenticate, but when `AppDb::get_user_data_by_id` was called to
54    /// fetch the user's password, no record was returned.
55    /// 
56    /// This either indicates a logic error in your `AppDb` implementation, or
57    /// a race condition in which the user is deleted after the session cookie
58    /// is verified but before the reauthentication is checked.
59    UserDataQueryFailed {user_id: i64},
60
61    /// Internal error which occurs when serializing or deserializing challenge
62    /// data.
63    Serde(serde_json::Error),
64
65    /// Internal error which occurs when hashing or verifying a password. This
66    /// could indicate, for example, that a hash stored in the database is in
67    /// the wrong format, or uses an unsupported algorithm.
68    Hasher(password_hash::Error),
69}
70
71impl Error {
72    pub fn status_code(&self) -> StatusCode {
73        match self {
74            Self::UserHasNoPassword
75            | Self::PasswordTooShort
76            | Self::PasswordsNotDifferent => StatusCode::BAD_REQUEST,
77
78            Self::NoSuchUser
79            | Self::UserIsSuspended
80            | Self::IncorrectPassword
81            | Self::IncorrectChallengeCode
82            | Self::EmailNotVerified
83            | Self::RequirePasswordChange
84            | Self::NotAuthenticated => StatusCode::UNAUTHORIZED,
85
86            Self::UserDataQueryFailed {..}
87            | Self::Hasher(_)
88            | Self::Serde(_) => StatusCode::INTERNAL_SERVER_ERROR,
89        }
90    }
91
92    pub(crate) fn as_app_err<T, E: From<Self>>(self) -> Result<T, E> {
93        Err(E::from(self))
94    }
95}