Struggling to clean up error handling.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user