From 5f857138924f672f37df58c314bd453558a5fa43 Mon Sep 17 00:00:00 2001 From: Rogier Reerink Date: Sat, 10 Oct 2026 15:54:23 +0200 Subject: [PATCH] Slight genericity and boundary improvement. --- server/src/domain.rs | 2 -- server/src/domain/authorization.rs | 16 --------------- server/src/routes/extractors/auth.rs | 20 ++++++++++--------- server/src/routes/extractors/auth/bearer.rs | 8 ++++---- .../src/routes/extractors/auth/principal.rs | 20 +++++++++++++++++++ 5 files changed, 35 insertions(+), 31 deletions(-) delete mode 100644 server/src/domain/authorization.rs create mode 100644 server/src/routes/extractors/auth/principal.rs diff --git a/server/src/domain.rs b/server/src/domain.rs index e4d73f1..eaa25e9 100644 --- a/server/src/domain.rs +++ b/server/src/domain.rs @@ -1,5 +1,4 @@ mod account; -mod authorization; mod cents; mod direction; mod recurring_transaction; @@ -11,7 +10,6 @@ mod user_app; mod user_app_secret; pub use account::*; -pub use authorization::*; pub use cents::*; pub use direction::*; pub use recurring_transaction::*; diff --git a/server/src/domain/authorization.rs b/server/src/domain/authorization.rs deleted file mode 100644 index 79c87d7..0000000 --- a/server/src/domain/authorization.rs +++ /dev/null @@ -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 - } -} diff --git a/server/src/routes/extractors/auth.rs b/server/src/routes/extractors/auth.rs index 8fe29db..f2be4de 100644 --- a/server/src/routes/extractors/auth.rs +++ b/server/src/routes/extractors/auth.rs @@ -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(pub Principal); -impl FromRequestParts for Auth +impl FromRequestParts for Auth where - Bearer: AuthScheme + Authorizer, + T: AuthScheme + Authorizer, S: Send + Sync, { type Rejection = AuthError; @@ -23,12 +25,12 @@ where state: &S, ) -> Result { 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::::into)?; - let authorization = Bearer::authorize(credentials, state) + let credentials = T::credentials(header.token()).map_err(Into::::into)?; + let authorization = T::authorize(credentials, state) .await .map_err(Into::::into)?; @@ -92,9 +94,9 @@ trait AuthScheme { fn credentials(token: &str) -> Result; } -trait Authorizer: AuthScheme { +trait Authorizer: AuthScheme { fn authorize( credentials: Self::Credentials, state: &S, - ) -> impl Future> + Send; + ) -> impl Future, Self::Error>> + Send; } diff --git a/server/src/routes/extractors/auth/bearer.rs b/server/src/routes/extractors/auth/bearer.rs index 4fab13d..0128855 100644 --- a/server/src/routes/extractors/auth/bearer.rs +++ b/server/src/routes/extractors/auth/bearer.rs @@ -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 Authorizer for Bearer +impl Authorizer for Bearer where PgPool: FromRef, S: Send + Sync, @@ -29,7 +29,7 @@ where async fn authorize( credentials: Self::Credentials, state: &S, - ) -> Result { + ) -> Result, 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::::new(token.user_id)) } } diff --git a/server/src/routes/extractors/auth/principal.rs b/server/src/routes/extractors/auth/principal.rs new file mode 100644 index 0000000..b4aa3f6 --- /dev/null +++ b/server/src/routes/extractors/auth/principal.rs @@ -0,0 +1,20 @@ +use uuid::Uuid; + +#[derive(Debug)] +pub struct Principal { + user_id: Uuid, + _marker: std::marker::PhantomData T>, +} + +impl Principal { + pub fn new(user_id: Uuid) -> Self { + Self { + user_id, + _marker: std::marker::PhantomData, + } + } + + pub fn user_id(&self) -> Uuid { + self.user_id + } +}