Slight genericity and boundary improvement.
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
mod account;
|
mod account;
|
||||||
mod authorization;
|
|
||||||
mod cents;
|
mod cents;
|
||||||
mod direction;
|
mod direction;
|
||||||
mod recurring_transaction;
|
mod recurring_transaction;
|
||||||
@@ -11,7 +10,6 @@ mod user_app;
|
|||||||
mod user_app_secret;
|
mod user_app_secret;
|
||||||
|
|
||||||
pub use account::*;
|
pub use account::*;
|
||||||
pub use authorization::*;
|
|
||||||
pub use cents::*;
|
pub use cents::*;
|
||||||
pub use direction::*;
|
pub use direction::*;
|
||||||
pub use recurring_transaction::*;
|
pub use recurring_transaction::*;
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Authorization {
|
|
||||||
user_id: Uuid,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Authorization {
|
|
||||||
pub fn new(user_id: Uuid) -> Self {
|
|
||||||
Self { user_id }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn user_id(&self) -> Uuid {
|
|
||||||
self.user_id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,18 +2,20 @@ use axum::{extract::FromRequestParts, response::IntoResponse};
|
|||||||
|
|
||||||
mod bearer;
|
mod bearer;
|
||||||
mod header;
|
mod header;
|
||||||
|
mod principal;
|
||||||
|
|
||||||
pub use bearer::*;
|
pub use bearer::*;
|
||||||
pub use header::*;
|
pub use header::*;
|
||||||
|
pub use principal::*;
|
||||||
|
|
||||||
use crate::{domain::Authorization, error::AppError};
|
use crate::error::AppError;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[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
|
where
|
||||||
Bearer: AuthScheme + Authorizer<S>,
|
T: AuthScheme + Authorizer<S, T>,
|
||||||
S: Send + Sync,
|
S: Send + Sync,
|
||||||
{
|
{
|
||||||
type Rejection = AuthError;
|
type Rejection = AuthError;
|
||||||
@@ -23,12 +25,12 @@ where
|
|||||||
state: &S,
|
state: &S,
|
||||||
) -> Result<Self, Self::Rejection> {
|
) -> Result<Self, Self::Rejection> {
|
||||||
let header = AuthHeader::from_request_parts(parts, state).await?;
|
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);
|
return Err(AuthError::UnsupportedScheme);
|
||||||
}
|
}
|
||||||
|
|
||||||
let credentials = Bearer::credentials(header.token()).map_err(Into::<AuthError>::into)?;
|
let credentials = T::credentials(header.token()).map_err(Into::<AuthError>::into)?;
|
||||||
let authorization = Bearer::authorize(credentials, state)
|
let authorization = T::authorize(credentials, state)
|
||||||
.await
|
.await
|
||||||
.map_err(Into::<AuthError>::into)?;
|
.map_err(Into::<AuthError>::into)?;
|
||||||
|
|
||||||
@@ -92,9 +94,9 @@ trait AuthScheme {
|
|||||||
fn credentials(token: &str) -> Result<Self::Credentials, Self::Error>;
|
fn credentials(token: &str) -> Result<Self::Credentials, Self::Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
trait Authorizer<S>: AuthScheme {
|
trait Authorizer<S, T>: AuthScheme {
|
||||||
fn authorize(
|
fn authorize(
|
||||||
credentials: Self::Credentials,
|
credentials: Self::Credentials,
|
||||||
state: &S,
|
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 axum::extract::FromRef;
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
|
|
||||||
use super::{AuthScheme, Authorizer};
|
use super::{AuthScheme, Authorizer, Principal};
|
||||||
use crate::{
|
use crate::{
|
||||||
crypto::hash_secret,
|
crypto::hash_secret,
|
||||||
repository::{self, RepositoryError},
|
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
|
where
|
||||||
PgPool: FromRef<S>,
|
PgPool: FromRef<S>,
|
||||||
S: Send + Sync,
|
S: Send + Sync,
|
||||||
@@ -29,7 +29,7 @@ where
|
|||||||
async fn authorize(
|
async fn authorize(
|
||||||
credentials: Self::Credentials,
|
credentials: Self::Credentials,
|
||||||
state: &S,
|
state: &S,
|
||||||
) -> Result<crate::domain::Authorization, Self::Error> {
|
) -> Result<Principal<T>, Self::Error> {
|
||||||
let pool = PgPool::from_ref(state);
|
let pool = PgPool::from_ref(state);
|
||||||
|
|
||||||
let token_hash = hash_secret(&credentials);
|
let token_hash = hash_secret(&credentials);
|
||||||
@@ -38,7 +38,7 @@ where
|
|||||||
.map_err(BearerError::Repository)?
|
.map_err(BearerError::Repository)?
|
||||||
.ok_or(BearerError::InvalidCredentials)?;
|
.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