Struggling to clean up error handling.

This commit is contained in:
2026-10-10 17:18:36 +02:00
parent 5f85713892
commit 94b2f23a59
4 changed files with 64 additions and 48 deletions
+6 -32
View File
@@ -8,14 +8,14 @@ pub use bearer::*;
pub use header::*;
pub use principal::*;
use crate::error::AppError;
use crate::{error::AppError, repository::RepositoryError};
#[derive(Debug)]
pub struct Auth<T = Bearer>(pub Principal<T>);
impl<T, S> FromRequestParts<S> for Auth<T>
where
T: AuthScheme + Authorizer<S, T>,
T: AuthScheme + Authorizer<S>,
S: Send + Sync,
{
type Rejection = AuthError;
@@ -48,35 +48,9 @@ pub enum AuthError {
#[error(transparent)]
Bearer(#[from] BearerError),
}
impl From<AuthError> for AppError {
fn from(err: AuthError) -> Self {
match err {
AuthError::UnsupportedScheme => {
AppError::Unauthorized("Unsupported authorization scheme".into())
}
AuthError::Header(AuthHeaderError::Missing) => {
AppError::Unauthorized("Missing authorization header".into())
}
AuthError::Header(AuthHeaderError::NotAscii(_)) => {
AppError::Unauthorized("Invalid authorization header encoding".into())
}
AuthError::Header(AuthHeaderError::MalformedValue) => {
AppError::Unauthorized("Malformed authorization header".into())
}
AuthError::Header(AuthHeaderError::EmptyScheme) => {
AppError::Unauthorized("Empty authorization scheme".into())
}
AuthError::Header(AuthHeaderError::EmptyToken) => {
AppError::Unauthorized("Empty token value".into())
}
AuthError::Bearer(BearerError::InvalidCredentials) => {
AppError::Unauthorized("Unauthorized".into())
}
AuthError::Bearer(BearerError::Repository(error)) => error.into(),
}
}
#[error(transparent)]
Repository(#[from] RepositoryError),
}
impl IntoResponse for AuthError {
@@ -94,9 +68,9 @@ trait AuthScheme {
fn credentials(token: &str) -> Result<Self::Credentials, Self::Error>;
}
trait Authorizer<S, T>: AuthScheme {
trait Authorizer<S>: AuthScheme + Sized {
fn authorize(
credentials: Self::Credentials,
state: &S,
) -> impl Future<Output = Result<Principal<T>, Self::Error>> + Send;
) -> impl Future<Output = Result<Principal<Self>, Self::Error>> + Send;
}
+17 -6
View File
@@ -13,15 +13,15 @@ pub struct Bearer;
impl AuthScheme for Bearer {
const SCHEME: &'static str = "Bearer";
type Credentials = String;
type Credentials = BearerToken;
type Error = BearerError;
fn credentials(token: &str) -> Result<Self::Credentials, Self::Error> {
Ok(token.to_string())
Ok(BearerToken(token.to_string()))
}
}
impl<S, T> Authorizer<S, T> for Bearer
impl<S> Authorizer<S> for Bearer
where
PgPool: FromRef<S>,
S: Send + Sync,
@@ -29,19 +29,21 @@ where
async fn authorize(
credentials: Self::Credentials,
state: &S,
) -> Result<Principal<T>, Self::Error> {
) -> Result<Principal<Self>, Self::Error> {
let pool = PgPool::from_ref(state);
let token_hash = hash_secret(&credentials);
let token_hash = hash_secret(&credentials.0);
let token = repository::tokens::get_by_access_token_hash(&pool, &token_hash)
.await
.map_err(BearerError::Repository)?
.ok_or(BearerError::InvalidCredentials)?;
Ok(Principal::<T>::new(token.user_id))
Ok(Principal::new(token.user_id))
}
}
pub struct BearerToken(String);
#[derive(Debug, thiserror::Error)]
pub enum BearerError {
#[error("invalid credentials")]
@@ -50,3 +52,12 @@ pub enum BearerError {
#[error(transparent)]
Repository(#[from] RepositoryError),
}
impl crate::error::ClientError for BearerError {
fn client_error_message(&self) -> &'static str {
match self {
BearerError::InvalidCredentials => "Invalid credentials",
BearerError::Repository(_) => "Repository error",
}
}
}
+13 -3
View File
@@ -4,8 +4,6 @@ use axum::{
response::IntoResponse,
};
use crate::routes::extractors::auth::AuthError;
#[derive(Debug)]
pub struct AuthHeader {
scheme: String,
@@ -77,9 +75,21 @@ pub enum AuthHeaderError {
EmptyToken,
}
impl crate::error::ClientError for AuthHeaderError {
fn client_error_message(&self) -> &'static str {
match self {
AuthHeaderError::Missing => "Missing authorization header",
AuthHeaderError::NotAscii(_) => "Invalid authorization header encoding",
AuthHeaderError::MalformedValue => "Malformed authorization header",
AuthHeaderError::EmptyScheme => "Empty authorization scheme",
AuthHeaderError::EmptyToken => "Empty authorization token",
}
}
}
impl IntoResponse for AuthHeaderError {
fn into_response(self) -> axum::response::Response {
Into::<AuthError>::into(self).into_response()
Into::<super::AuthError>::into(self).into_response()
}
}