fastauth_plugin_two_factor/
error.rs1use fastauth_core::plugin_prelude::*;
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub enum TwoFactorError {
8 InvalidCode,
10 InvalidBackupCode,
12 OtpHasExpired,
14 TooManyAttempts,
16 TotpNotEnabled,
18 TwoFactorNotEnabled,
20 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 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}