Add auth module

This commit is contained in:
Lennart
2023-09-07 18:50:21 +02:00
parent dcd6c01b12
commit e7d73e180a
6 changed files with 232 additions and 0 deletions

46
crates/auth/src/lib.rs Normal file
View File

@@ -0,0 +1,46 @@
use actix_web::{HttpRequest, ResponseError};
use futures_util::{future::Ready, Future};
pub use extractor::AuthInfoExtractor;
pub use htpasswd::{HtpasswdAuth, HtpasswdAuthConfig};
pub use none::NoneAuth;
pub mod error;
pub mod extractor;
pub mod htpasswd;
pub mod none;
pub struct AuthInfo {
pub user_id: String,
}
pub trait CheckAuthentication: Send + Sync + 'static {
type Error: ResponseError;
type Future: Future<Output = Result<AuthInfo, Self::Error>>
where
Self: Sized;
fn validate(&self, req: &HttpRequest) -> Self::Future
where
Self: Sized;
}
#[derive(Debug)]
pub enum AuthProvider {
Htpasswd(HtpasswdAuth),
None(NoneAuth),
}
impl CheckAuthentication for AuthProvider {
type Error = crate::error::Error;
type Future = Ready<Result<AuthInfo, Self::Error>>;
fn validate(&self, req: &HttpRequest) -> Self::Future
where
Self: Sized,
{
match self {
Self::Htpasswd(auth) => auth.validate(req),
Self::None(auth) => auth.validate(req),
}
}
}