Skip to main content

fastauth_plugin_two_factor/
error.rs

1//! The error codes two-factor answers with.
2
3use fastauth_core::plugin_prelude::*;
4
5/// Why a two-factor request failed. Codes and statuses match Better Auth's two-factor plugin.
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub enum TwoFactorError {
8    /// `INVALID_CODE`: the TOTP or email code is wrong, expired or already used.
9    InvalidCode,
10    /// `INVALID_BACKUP_CODE`: the backup code is wrong or already used.
11    InvalidBackupCode,
12    /// `OTP_HAS_EXPIRED`: the email code is past its lifetime; send a new one.
13    OtpHasExpired,
14    /// `TOO_MANY_ATTEMPTS_REQUEST_NEW_CODE`: five wrong email codes burn the code.
15    TooManyAttempts,
16    /// `TOTP_NOT_ENABLED`: the user never called `/two-factor/enable`.
17    TotpNotEnabled,
18    /// `TWO_FACTOR_NOT_ENABLED`: two-factor isn't on for this user yet.
19    TwoFactorNotEnabled,
20    /// `INVALID_TWO_FACTOR_COOKIE`: no session, and no valid pending sign-in cookie.
21    InvalidTwoFactorCookie,
22}
23
24impl TwoFactorError {
25    const fn parts(self) -> (StatusCode, &'static str, &'static str) {
26        match self {
27            Self::InvalidCode => (StatusCode::UNAUTHORIZED, "INVALID_CODE", "invalid code"),
28            Self::InvalidBackupCode => (
29                StatusCode::UNAUTHORIZED,
30                "INVALID_BACKUP_CODE",
31                "invalid backup code",
32            ),
33            Self::OtpHasExpired => (
34                StatusCode::BAD_REQUEST,
35                "OTP_HAS_EXPIRED",
36                "the code has expired, request a new one",
37            ),
38            Self::TooManyAttempts => (
39                StatusCode::BAD_REQUEST,
40                "TOO_MANY_ATTEMPTS_REQUEST_NEW_CODE",
41                "too many attempts, request a new code",
42            ),
43            Self::TotpNotEnabled => (
44                StatusCode::BAD_REQUEST,
45                "TOTP_NOT_ENABLED",
46                "TOTP is not enabled",
47            ),
48            Self::TwoFactorNotEnabled => (
49                StatusCode::BAD_REQUEST,
50                "TWO_FACTOR_NOT_ENABLED",
51                "two-factor authentication is not enabled",
52            ),
53            Self::InvalidTwoFactorCookie => (
54                StatusCode::UNAUTHORIZED,
55                "INVALID_TWO_FACTOR_COOKIE",
56                "the two-factor sign-in has expired, sign in again",
57            ),
58        }
59    }
60
61    /// The machine-readable code clients see.
62    pub const fn code(self) -> &'static str {
63        self.parts().1
64    }
65}
66
67impl From<TwoFactorError> for AuthError {
68    fn from(err: TwoFactorError) -> Self {
69        let (status, code, message) = err.parts();
70        AuthError::new(status, code, message)
71    }
72}
73
74impl ErrorCodes for TwoFactorError {
75    const CODES: &'static [&'static str] = &[
76        Self::InvalidCode.code(),
77        Self::InvalidBackupCode.code(),
78        Self::OtpHasExpired.code(),
79        Self::TooManyAttempts.code(),
80        Self::TotpNotEnabled.code(),
81        Self::TwoFactorNotEnabled.code(),
82        Self::InvalidTwoFactorCookie.code(),
83    ];
84}