Skip to main content

fastauth_plugin_passkey/
error.rs

1//! The error codes passkeys answer with.
2
3use fastauth_core::plugin_prelude::*;
4
5/// Why a passkey request failed. Codes and statuses match Better Auth's passkey plugin.
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub enum PasskeyError {
8    /// `CHALLENGE_NOT_FOUND` (400): no challenge cookie, or its challenge expired, was used
9    /// already, or belongs to the other ceremony.
10    ChallengeNotFound,
11    /// `YOU_ARE_NOT_ALLOWED_TO_REGISTER_THIS_PASSKEY` (401): the challenge was issued to
12    /// another user, or the passkey being renamed isn't the caller's.
13    NotAllowed,
14    /// `FAILED_TO_VERIFY_REGISTRATION` (400): the attestation doesn't check out, or the
15    /// credential is registered already.
16    FailedToVerifyRegistration,
17    /// `PASSKEY_NOT_FOUND` (401): no passkey has the credential that signed in.
18    UnknownCredential,
19    /// `PASSKEY_NOT_FOUND` (404): no passkey has this id.
20    PasskeyNotFound,
21    /// `AUTHENTICATION_FAILED` (401): the assertion's signature is wrong.
22    AuthenticationFailed,
23    /// `AUTHENTICATION_FAILED` (400): the assertion is malformed, answers another challenge
24    /// or origin, lacks user verification, or its signature counter didn't advance.
25    InvalidAssertion,
26    /// `ORIGIN_NOT_CONFIGURED` (500): the rpID and origins don't make a valid relying party.
27    OriginNotConfigured,
28}
29
30impl PasskeyError {
31    const fn parts(self) -> (StatusCode, &'static str, &'static str) {
32        match self {
33            Self::ChallengeNotFound => (
34                StatusCode::BAD_REQUEST,
35                "CHALLENGE_NOT_FOUND",
36                "Challenge not found",
37            ),
38            Self::NotAllowed => (
39                StatusCode::UNAUTHORIZED,
40                "YOU_ARE_NOT_ALLOWED_TO_REGISTER_THIS_PASSKEY",
41                "You are not allowed to register this passkey",
42            ),
43            Self::FailedToVerifyRegistration => (
44                StatusCode::BAD_REQUEST,
45                "FAILED_TO_VERIFY_REGISTRATION",
46                "Failed to verify registration",
47            ),
48            Self::UnknownCredential => (
49                StatusCode::UNAUTHORIZED,
50                "PASSKEY_NOT_FOUND",
51                "Passkey not found",
52            ),
53            Self::PasskeyNotFound => (
54                StatusCode::NOT_FOUND,
55                "PASSKEY_NOT_FOUND",
56                "Passkey not found",
57            ),
58            Self::AuthenticationFailed => (
59                StatusCode::UNAUTHORIZED,
60                "AUTHENTICATION_FAILED",
61                "Authentication failed",
62            ),
63            Self::InvalidAssertion => (
64                StatusCode::BAD_REQUEST,
65                "AUTHENTICATION_FAILED",
66                "Authentication failed",
67            ),
68            Self::OriginNotConfigured => (
69                StatusCode::INTERNAL_SERVER_ERROR,
70                "ORIGIN_NOT_CONFIGURED",
71                "Passkey origin is not configured. Set the passkey plugin's `rp_id` and `origin` \
72                 to match `base_url`.",
73            ),
74        }
75    }
76
77    /// The machine-readable code clients see.
78    pub const fn code(self) -> &'static str {
79        self.parts().1
80    }
81}
82
83impl From<PasskeyError> for AuthError {
84    fn from(err: PasskeyError) -> Self {
85        let (status, code, message) = err.parts();
86        AuthError::new(status, code, message)
87    }
88}
89
90impl ErrorCodes for PasskeyError {
91    const CODES: &'static [&'static str] = &[
92        Self::ChallengeNotFound.code(),
93        Self::NotAllowed.code(),
94        Self::FailedToVerifyRegistration.code(),
95        Self::PasskeyNotFound.code(),
96        Self::AuthenticationFailed.code(),
97        Self::OriginNotConfigured.code(),
98    ];
99}