use fastauth_core::plugin_prelude::*;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PasskeyError {
ChallengeNotFound,
NotAllowed,
FailedToVerifyRegistration,
UnknownCredential,
PasskeyNotFound,
AuthenticationFailed,
InvalidAssertion,
OriginNotConfigured,
}
impl PasskeyError {
const fn parts(self) -> (StatusCode, &'static str, &'static str) {
match self {
Self::ChallengeNotFound => (
StatusCode::BAD_REQUEST,
"CHALLENGE_NOT_FOUND",
"Challenge not found",
),
Self::NotAllowed => (
StatusCode::UNAUTHORIZED,
"YOU_ARE_NOT_ALLOWED_TO_REGISTER_THIS_PASSKEY",
"You are not allowed to register this passkey",
),
Self::FailedToVerifyRegistration => (
StatusCode::BAD_REQUEST,
"FAILED_TO_VERIFY_REGISTRATION",
"Failed to verify registration",
),
Self::UnknownCredential => (
StatusCode::UNAUTHORIZED,
"PASSKEY_NOT_FOUND",
"Passkey not found",
),
Self::PasskeyNotFound => (
StatusCode::NOT_FOUND,
"PASSKEY_NOT_FOUND",
"Passkey not found",
),
Self::AuthenticationFailed => (
StatusCode::UNAUTHORIZED,
"AUTHENTICATION_FAILED",
"Authentication failed",
),
Self::InvalidAssertion => (
StatusCode::BAD_REQUEST,
"AUTHENTICATION_FAILED",
"Authentication failed",
),
Self::OriginNotConfigured => (
StatusCode::INTERNAL_SERVER_ERROR,
"ORIGIN_NOT_CONFIGURED",
"Passkey origin is not configured. Set the passkey plugin's `rp_id` and `origin` \
to match `base_url`.",
),
}
}
pub const fn code(self) -> &'static str {
self.parts().1
}
}
impl From<PasskeyError> for AuthError {
fn from(err: PasskeyError) -> Self {
let (status, code, message) = err.parts();
AuthError::new(status, code, message)
}
}
impl ErrorCodes for PasskeyError {
const CODES: &'static [&'static str] = &[
Self::ChallengeNotFound.code(),
Self::NotAllowed.code(),
Self::FailedToVerifyRegistration.code(),
Self::PasskeyNotFound.code(),
Self::AuthenticationFailed.code(),
Self::OriginNotConfigured.code(),
];
}