diff options
Diffstat (limited to 'src/auth/error.rs')
-rw-r--r-- | src/auth/error.rs | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/src/auth/error.rs b/src/auth/error.rs index 17cf6d1..3a111ca 100644 --- a/src/auth/error.rs +++ b/src/auth/error.rs @@ -1,7 +1,13 @@ #[derive(thiserror::Error, Debug)] pub enum Error { #[error("Failed to parse header: {0}")] - Header(#[from] axum::http::header::InvalidHeaderValue), + HeaderValue(#[from] axum::http::header::InvalidHeaderValue), + + #[error("Required header not found: {0}")] + HeaderNotFound(axum::http::HeaderName), + + #[error("Failed to parse header: {0} (wrong token type?)")] + HeaderRejection(axum_extra::typed_header::TypedHeaderRejection), #[error("Database error: {0}")] Sqlx(#[from] sqlx::Error), @@ -37,6 +43,16 @@ pub enum Error { UserNotFound, } +impl From<axum_extra::typed_header::TypedHeaderRejection> for Error { + fn from(value: axum_extra::typed_header::TypedHeaderRejection) -> Self { + if value.is_missing() { + Self::HeaderNotFound(value.name().clone()) + } else { + Self::HeaderRejection(value) + } + } +} + impl From<argon2::password_hash::Error> for Error { fn from(value: argon2::password_hash::Error) -> Self { match value { @@ -73,10 +89,14 @@ impl From<uuid::Error> for Error { impl axum::response::IntoResponse for Error { fn into_response(self) -> axum::response::Response { - use axum::http::StatusCode; + use axum::http::{header::AUTHORIZATION, StatusCode}; let status = match self { - Error::JwtFormat(_) | Error::Uuid(_) => StatusCode::UNPROCESSABLE_ENTITY, + Self::HeaderNotFound(ref h) if h == AUTHORIZATION => StatusCode::UNAUTHORIZED, + Self::HeaderNotFound(_) => StatusCode::BAD_REQUEST, + Self::HeaderRejection(_) | Error::JwtFormat(_) | Error::Uuid(_) => { + StatusCode::UNPROCESSABLE_ENTITY + } Error::JwtValidation(_) | Error::LoginInvalid | Error::UserNotFound |