making auth sync since that suffices for now and makes the code simpler

This commit is contained in:
Lennart
2024-03-15 22:40:04 +01:00
parent 4f1a59b66d
commit efeec7d1d4
5 changed files with 35 additions and 55 deletions

View File

@@ -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<A: CheckAuthentication> {
pub inner: AuthInfo,
@@ -19,28 +20,21 @@ impl<T: CheckAuthentication> From<AuthInfo> for AuthInfoExtractor<T> {
}
}
pub struct AuthInfoExtractorFuture<A>
where
A: CheckAuthentication,
{
future: Pin<Box<A::Future>>,
}
pub struct AuthInfoExtractorFuture<A: CheckAuthentication>(Result<AuthInfo, Error>, PhantomData<A>);
impl<A> Future for AuthInfoExtractorFuture<A>
where
A: CheckAuthentication,
{
type Output = Result<AuthInfoExtractor<A>, A::Error>;
impl<A: CheckAuthentication> Future for AuthInfoExtractorFuture<A> {
type Output = Result<AuthInfoExtractor<A>, Error>;
fn poll(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
_cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
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<A> FromRequest for AuthInfoExtractor<A>
where
A: CheckAuthentication,
{
type Error = A::Error;
type Error = Error;
type Future = AuthInfoExtractorFuture<A>;
fn extract(req: &HttpRequest) -> Self::Future {
let a = req.app_data::<Data<A>>().unwrap().validate(req);
Self::Future {
future: Box::pin(a),
}
let result = req.app_data::<Data<A>>().unwrap().validate(req);
AuthInfoExtractorFuture(result, PhantomData)
}
fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
Self::extract(req)