From efeec7d1d4f5ff2a08f1904e07df257a45f80b0c Mon Sep 17 00:00:00 2001 From: Lennart <18233294+lennart-k@users.noreply.github.com> Date: Fri, 15 Mar 2024 22:40:04 +0100 Subject: [PATCH] making auth sync since that suffices for now and makes the code simpler --- crates/auth/src/error.rs | 2 +- crates/auth/src/extractor.rs | 44 +++++++++++++++--------------------- crates/auth/src/htpasswd.rs | 15 +++++------- crates/auth/src/lib.rs | 17 ++++---------- crates/auth/src/none.rs | 12 ++++------ 5 files changed, 35 insertions(+), 55 deletions(-) diff --git a/crates/auth/src/error.rs b/crates/auth/src/error.rs index 07526cc..763b4b1 100644 --- a/crates/auth/src/error.rs +++ b/crates/auth/src/error.rs @@ -1,7 +1,7 @@ use actix_web::{http::StatusCode, HttpResponse}; use derive_more::{Display, Error}; -#[derive(Debug, Display, Error)] +#[derive(Debug, Display, Error, Clone)] pub enum Error { #[display(fmt = "Internal server error")] InternalError, diff --git a/crates/auth/src/extractor.rs b/crates/auth/src/extractor.rs index 5ad0759..8530d6a 100644 --- a/crates/auth/src/extractor.rs +++ b/crates/auth/src/extractor.rs @@ -1,9 +1,10 @@ use actix_web::{dev::Payload, web::Data, FromRequest, HttpRequest}; -use futures_util::{Future, FutureExt}; -use std::marker::PhantomData; -use std::pin::Pin; +use futures_util::Future; +use std::{marker::PhantomData, task::Poll}; -use super::{CheckAuthentication, AuthInfo}; +use crate::error::Error; + +use super::{AuthInfo, CheckAuthentication}; pub struct AuthInfoExtractor { pub inner: AuthInfo, @@ -19,28 +20,21 @@ impl From for AuthInfoExtractor { } } -pub struct AuthInfoExtractorFuture -where - A: CheckAuthentication, -{ - future: Pin>, -} +pub struct AuthInfoExtractorFuture(Result, PhantomData); -impl Future for AuthInfoExtractorFuture -where - A: CheckAuthentication, -{ - type Output = Result, A::Error>; +impl Future for AuthInfoExtractorFuture { + type Output = Result, Error>; fn poll( self: std::pin::Pin<&mut Self>, - cx: &mut std::task::Context<'_>, + _cx: &mut std::task::Context<'_>, ) -> std::task::Poll { - match self.get_mut().future.poll_unpin(cx) { - std::task::Poll::Pending => std::task::Poll::Pending, - std::task::Poll::Ready(result) => { - std::task::Poll::Ready(result.map(|auth_info| auth_info.into())) - } + match &self.0 { + Ok(auth_info) => Poll::Ready(Ok(AuthInfoExtractor { + inner: auth_info.clone(), + _provider_type: PhantomData, + })), + Err(err) => Poll::Ready(Err(err.clone())), } } } @@ -49,14 +43,12 @@ impl FromRequest for AuthInfoExtractor where A: CheckAuthentication, { - type Error = A::Error; + type Error = Error; type Future = AuthInfoExtractorFuture; fn extract(req: &HttpRequest) -> Self::Future { - let a = req.app_data::>().unwrap().validate(req); - Self::Future { - future: Box::pin(a), - } + let result = req.app_data::>().unwrap().validate(req); + AuthInfoExtractorFuture(result, PhantomData) } fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future { Self::extract(req) diff --git a/crates/auth/src/htpasswd.rs b/crates/auth/src/htpasswd.rs index b3db7c3..0a80a5e 100644 --- a/crates/auth/src/htpasswd.rs +++ b/crates/auth/src/htpasswd.rs @@ -1,6 +1,6 @@ +use crate::error::Error; use actix_web::{http::header::Header, HttpRequest}; use actix_web_httpauth::headers::authorization::{Authorization, Basic}; -use futures_util::future::{err, ok, Ready}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; @@ -22,10 +22,7 @@ pub struct HtpasswdAuthConfig { } impl CheckAuthentication for HtpasswdAuth { - type Error = crate::error::Error; - type Future = Ready>; - - fn validate(&self, req: &HttpRequest) -> Self::Future { + fn validate(&self, req: &HttpRequest) -> Result { if let Ok(auth) = Authorization::::parse(req) { let user_id = auth.as_ref().user_id(); // Map None to empty password @@ -34,19 +31,19 @@ impl CheckAuthentication for HtpasswdAuth { let user_config = if let Some(user_config) = self.config.users.get(user_id) { user_config } else { - return err(crate::error::Error::Unauthorized); + return Err(crate::error::Error::Unauthorized); }; if let Err(e) = password_auth::verify_password(password, &user_config.password) { dbg!(e); - return err(crate::error::Error::Unauthorized); + return Err(crate::error::Error::Unauthorized); } - ok(AuthInfo { + Ok(AuthInfo { user_id: user_id.to_string(), }) } else { - err(crate::error::Error::Unauthorized) + Err(crate::error::Error::Unauthorized) } } } diff --git a/crates/auth/src/lib.rs b/crates/auth/src/lib.rs index 7f328b7..bc6ea23 100644 --- a/crates/auth/src/lib.rs +++ b/crates/auth/src/lib.rs @@ -1,6 +1,6 @@ -use actix_web::{HttpRequest, ResponseError}; -use futures_util::{future::Ready, Future}; +use actix_web::HttpRequest; +use crate::error::Error; pub use extractor::AuthInfoExtractor; pub use htpasswd::{HtpasswdAuth, HtpasswdAuthConfig}; pub use none::NoneAuth; @@ -9,17 +9,13 @@ pub mod extractor; pub mod htpasswd; pub mod none; +#[derive(Clone)] pub struct AuthInfo { pub user_id: String, } pub trait CheckAuthentication: Send + Sync + 'static { - type Error: ResponseError; - type Future: Future> - where - Self: Sized; - - fn validate(&self, req: &HttpRequest) -> Self::Future + fn validate(&self, req: &HttpRequest) -> Result where Self: Sized; } @@ -31,10 +27,7 @@ pub enum AuthProvider { } impl CheckAuthentication for AuthProvider { - type Error = crate::error::Error; - type Future = Ready>; - - fn validate(&self, req: &HttpRequest) -> Self::Future + fn validate(&self, req: &HttpRequest) -> Result where Self: Sized, { diff --git a/crates/auth/src/none.rs b/crates/auth/src/none.rs index 11cabbb..b02e230 100644 --- a/crates/auth/src/none.rs +++ b/crates/auth/src/none.rs @@ -1,6 +1,7 @@ use actix_web::{http::header::Header, HttpRequest}; use actix_web_httpauth::headers::authorization::{Authorization, Basic}; -use futures_util::future::{err, ok, Ready}; + +use crate::error::Error; use super::{AuthInfo, CheckAuthentication}; @@ -8,16 +9,13 @@ use super::{AuthInfo, CheckAuthentication}; pub struct NoneAuth; impl CheckAuthentication for NoneAuth { - type Error = crate::error::Error; - type Future = Ready>; - - fn validate(&self, req: &HttpRequest) -> Self::Future { + fn validate(&self, req: &HttpRequest) -> Result { if let Ok(auth) = Authorization::::parse(req) { - ok(AuthInfo { + Ok(AuthInfo { user_id: auth.as_ref().user_id().to_string(), }) } else { - err(crate::error::Error::Unauthorized) + Err(crate::error::Error::Unauthorized) } } }