summaryrefslogtreecommitdiffstats
path: root/src/auth/error.rs
diff options
context:
space:
mode:
authorToby Vincent <tobyv@tobyvin.dev>2024-04-16 20:28:46 -0500
committerToby Vincent <tobyv@tobyvin.dev>2024-04-16 20:28:46 -0500
commita2860a1294b250402114fa016c4639881abc2172 (patch)
tree96609af1fe197ec0186f0f409ccfbb8b1012951d /src/auth/error.rs
parent917293785bffdd64d467e7d69b5645099b21d5e9 (diff)
refactor: improve account extractors
Diffstat (limited to 'src/auth/error.rs')
-rw-r--r--src/auth/error.rs26
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