pub mod either;
#[cfg(feature = "session")]
pub mod session;
use std::future::{ready, Ready};
use actix_web::{dev::Payload, FromRequest, HttpRequest};
use derive_more::{Deref, DerefMut};
pub use either::EitherExt;
pub trait Authenticate: Sized {
type Output;
type Error;
fn authenticate(request: &HttpRequest) -> Result<Self, Self::Error>;
fn data(&self) -> &Self::Output;
}
#[derive(Deref, DerefMut)]
pub struct Authenticated<T: Authenticate>(T);
impl<T: Authenticate> Authenticated<T> {
pub fn into_inner(self) -> T {
self.0
}
}
impl<T: Authenticate> FromRequest for Authenticated<T>
where
T::Error: Into<actix_web::Error>,
{
type Error = T::Error;
type Future = Ready<Result<Self, Self::Error>>;
fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
match T::authenticate(req) {
Ok(value) => ready(Ok(Self(value))),
Err(error) => ready(Err(error)),
}
}
}