Slight genericity and boundary improvement.
This commit is contained in:
@@ -2,18 +2,20 @@ use axum::{extract::FromRequestParts, response::IntoResponse};
|
||||
|
||||
mod bearer;
|
||||
mod header;
|
||||
mod principal;
|
||||
|
||||
pub use bearer::*;
|
||||
pub use header::*;
|
||||
pub use principal::*;
|
||||
|
||||
use crate::{domain::Authorization, error::AppError};
|
||||
use crate::error::AppError;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Auth(pub Authorization);
|
||||
pub struct Auth<T = Bearer>(pub Principal<T>);
|
||||
|
||||
impl<S> FromRequestParts<S> for Auth
|
||||
impl<T, S> FromRequestParts<S> for Auth<T>
|
||||
where
|
||||
Bearer: AuthScheme + Authorizer<S>,
|
||||
T: AuthScheme + Authorizer<S, T>,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = AuthError;
|
||||
@@ -23,12 +25,12 @@ where
|
||||
state: &S,
|
||||
) -> Result<Self, Self::Rejection> {
|
||||
let header = AuthHeader::from_request_parts(parts, state).await?;
|
||||
if !header.scheme().eq_ignore_ascii_case(Bearer::SCHEME) {
|
||||
if !header.scheme().eq_ignore_ascii_case(T::SCHEME) {
|
||||
return Err(AuthError::UnsupportedScheme);
|
||||
}
|
||||
|
||||
let credentials = Bearer::credentials(header.token()).map_err(Into::<AuthError>::into)?;
|
||||
let authorization = Bearer::authorize(credentials, state)
|
||||
let credentials = T::credentials(header.token()).map_err(Into::<AuthError>::into)?;
|
||||
let authorization = T::authorize(credentials, state)
|
||||
.await
|
||||
.map_err(Into::<AuthError>::into)?;
|
||||
|
||||
@@ -92,9 +94,9 @@ trait AuthScheme {
|
||||
fn credentials(token: &str) -> Result<Self::Credentials, Self::Error>;
|
||||
}
|
||||
|
||||
trait Authorizer<S>: AuthScheme {
|
||||
trait Authorizer<S, T>: AuthScheme {
|
||||
fn authorize(
|
||||
credentials: Self::Credentials,
|
||||
state: &S,
|
||||
) -> impl Future<Output = Result<Authorization, Self::Error>> + Send;
|
||||
) -> impl Future<Output = Result<Principal<T>, Self::Error>> + Send;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user