completely rebuilt the auth implementation to support OIDC in the future

This commit is contained in:
Lennart
2024-10-03 19:47:50 +02:00
parent 235e7b207a
commit 6f12a1d80e
29 changed files with 257 additions and 312 deletions

View File

@@ -2,15 +2,15 @@ use actix_web::body::MessageBody;
use actix_web::dev::{ServiceFactory, ServiceRequest, ServiceResponse};
use actix_web::middleware::{Logger, NormalizePath};
use actix_web::{web, App};
use rustical_auth::CheckAuthentication;
use rustical_frontend::configure_frontend;
use rustical_store::auth::{AuthenticationMiddleware, AuthenticationProvider};
use rustical_store::CalendarStore;
use std::sync::Arc;
use tokio::sync::RwLock;
pub fn make_app<CS: CalendarStore + ?Sized, A: CheckAuthentication>(
pub fn make_app<CS: CalendarStore + ?Sized, AP: AuthenticationProvider + 'static>(
cal_store: Arc<RwLock<CS>>,
auth: Arc<A>,
auth_provider: Arc<AP>,
) -> App<
impl ServiceFactory<
ServiceRequest,
@@ -23,17 +23,14 @@ pub fn make_app<CS: CalendarStore + ?Sized, A: CheckAuthentication>(
App::new()
.wrap(Logger::new("[%s] %r"))
.wrap(NormalizePath::trim())
.wrap(AuthenticationMiddleware::new(auth_provider))
.service(web::scope("/caldav").configure(|cfg| {
rustical_caldav::configure_dav(
cfg,
"/caldav".to_string(),
auth.clone(),
cal_store.clone(),
)
}))
.service(web::scope("/carddav").configure(|cfg| {
rustical_carddav::configure_dav(cfg, "/carddav".to_string(), auth.clone())
rustical_caldav::configure_dav(cfg, "/caldav".to_string(), cal_store.clone())
}))
.service(
web::scope("/carddav")
.configure(|cfg| rustical_carddav::configure_dav(cfg, "/carddav".to_string())),
)
.service(
web::scope("/.well-known")
.configure(|cfg| rustical_caldav::configure_well_known(cfg, "/caldav".to_string())), // .configure(|cfg| {

View File

@@ -1,4 +1,5 @@
use rustical_auth::{AuthProvider, HtpasswdAuthConfig};
use rustical_frontend::FrontendConfig;
use rustical_store::auth::StaticUserStoreConfig;
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)]
@@ -21,19 +22,7 @@ pub enum CalendarStoreConfig {
#[derive(Debug, Deserialize, Serialize)]
#[serde(tag = "backend", rename_all = "snake_case")]
pub enum AuthConfig {
Htpasswd(HtpasswdAuthConfig),
None,
}
impl From<AuthConfig> for AuthProvider {
fn from(value: AuthConfig) -> Self {
match value {
AuthConfig::Htpasswd(config) => {
Self::Htpasswd(rustical_auth::htpasswd::HtpasswdAuth { config })
}
AuthConfig::None => Self::None(rustical_auth::none::NoneAuth),
}
}
Static(StaticUserStoreConfig),
}
#[derive(Debug, Deserialize, Serialize)]
@@ -41,4 +30,5 @@ pub struct Config {
pub calendar_store: CalendarStoreConfig,
pub auth: AuthConfig,
pub http: HttpConfig,
pub frontend: FrontendConfig,
}

View File

@@ -4,7 +4,7 @@ use anyhow::Result;
use app::make_app;
use clap::Parser;
use config::{CalendarStoreConfig, SqliteCalendarStoreConfig};
use rustical_auth::AuthProvider;
use rustical_store::auth::StaticUserStore;
use rustical_store::sqlite_store::{create_db_pool, SqliteCalendarStore};
use rustical_store::CalendarStore;
use std::fs;
@@ -45,9 +45,11 @@ async fn main() -> Result<()> {
let cal_store = get_cal_store(args.migrate, &config.calendar_store).await?;
let auth: Arc<AuthProvider> = Arc::new(config.auth.into());
let user_store = Arc::new(match config.auth {
config::AuthConfig::Static(config) => StaticUserStore::new(config),
});
HttpServer::new(move || make_app(cal_store.clone(), auth.clone()))
HttpServer::new(move || make_app(cal_store.clone(), user_store.clone()))
.bind((config.http.host, config.http.port))?
.run()
.await?;