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;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use axum::extract::FromRef;
|
||||
use sqlx::PgPool;
|
||||
|
||||
use super::{AuthScheme, Authorizer};
|
||||
use super::{AuthScheme, Authorizer, Principal};
|
||||
use crate::{
|
||||
crypto::hash_secret,
|
||||
repository::{self, RepositoryError},
|
||||
@@ -21,7 +21,7 @@ impl AuthScheme for Bearer {
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Authorizer<S> for Bearer
|
||||
impl<S, T> Authorizer<S, T> for Bearer
|
||||
where
|
||||
PgPool: FromRef<S>,
|
||||
S: Send + Sync,
|
||||
@@ -29,7 +29,7 @@ where
|
||||
async fn authorize(
|
||||
credentials: Self::Credentials,
|
||||
state: &S,
|
||||
) -> Result<crate::domain::Authorization, Self::Error> {
|
||||
) -> Result<Principal<T>, Self::Error> {
|
||||
let pool = PgPool::from_ref(state);
|
||||
|
||||
let token_hash = hash_secret(&credentials);
|
||||
@@ -38,7 +38,7 @@ where
|
||||
.map_err(BearerError::Repository)?
|
||||
.ok_or(BearerError::InvalidCredentials)?;
|
||||
|
||||
Ok(crate::domain::Authorization::new(token.user_id))
|
||||
Ok(Principal::<T>::new(token.user_id))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Principal<T> {
|
||||
user_id: Uuid,
|
||||
_marker: std::marker::PhantomData<fn() -> T>,
|
||||
}
|
||||
|
||||
impl<T> Principal<T> {
|
||||
pub fn new(user_id: Uuid) -> Self {
|
||||
Self {
|
||||
user_id,
|
||||
_marker: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn user_id(&self) -> Uuid {
|
||||
self.user_id
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user