Add auth config

This commit is contained in:
Lennart
2023-09-07 18:53:12 +02:00
parent 7f5df657d2
commit 5ee509b7bc
3 changed files with 29 additions and 11 deletions

View File

@@ -25,8 +25,10 @@ pub fn configure_well_known(cfg: &mut web::ServiceConfig, caldav_root: String) {
} }
pub fn configure_dav<C: CalendarStore>( pub fn configure_dav<C: CalendarStore>(
pub fn configure_dav<A: CheckAuthentication, C: CalendarStore>(
cfg: &mut web::ServiceConfig, cfg: &mut web::ServiceConfig,
prefix: String, prefix: String,
auth: Arc<A>,
store: Arc<RwLock<C>>, store: Arc<RwLock<C>>,
) { ) {
let propfind_method = || Method::from_str("PROPFIND").unwrap(); let propfind_method = || Method::from_str("PROPFIND").unwrap();

View File

@@ -1,3 +1,4 @@
use rustical_auth::{AuthProvider, HtpasswdAuthConfig};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
@@ -11,7 +12,26 @@ pub enum CalendarStoreConfig {
Toml(TomlCalendarStoreConfig), Toml(TomlCalendarStoreConfig),
} }
#[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),
}
}
}
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
pub struct Config { pub struct Config {
pub calendar_store: CalendarStoreConfig, pub calendar_store: CalendarStoreConfig,
pub auth: AuthConfig,
} }

View File

@@ -1,6 +1,3 @@
use std::fs;
use std::sync::Arc;
use crate::config::Config; use crate::config::Config;
use actix_web::middleware::{Logger, NormalizePath}; use actix_web::middleware::{Logger, NormalizePath};
use actix_web::{web, App, HttpServer}; use actix_web::{web, App, HttpServer};
@@ -8,9 +5,12 @@ use anyhow::Result;
use clap::Parser; use clap::Parser;
use config::{CalendarStoreConfig, TomlCalendarStoreConfig}; use config::{CalendarStoreConfig, TomlCalendarStoreConfig};
use rustical_api::configure_api; use rustical_api::configure_api;
use rustical_auth::AuthProvider;
use rustical_dav::{configure_dav, configure_well_known}; use rustical_dav::{configure_dav, configure_well_known};
use rustical_frontend::configure_frontend; use rustical_frontend::configure_frontend;
use rustical_store::calendar::TomlCalendarStore; use rustical_store::calendar::TomlCalendarStore;
use std::fs;
use std::sync::Arc;
use tokio::sync::RwLock; use tokio::sync::RwLock;
mod config; mod config;
@@ -38,20 +38,16 @@ async fn main() -> Result<()> {
} }
})); }));
let auth = match config.auth { let auth: Arc<AuthProvider> = Arc::new(config.auth.into());
config::AuthConfig::Htpasswd(config) => 1,
_ => panic!("invalid auth config"),
};
HttpServer::new(move || { HttpServer::new(move || {
let cal_store = cal_store.clone(); let cal_store = cal_store.clone();
App::new() App::new()
.wrap(Logger::new("[%s] %r")) .wrap(Logger::new("[%s] %r"))
.wrap(NormalizePath::trim()) .wrap(NormalizePath::trim())
.service( .service(web::scope("/caldav").configure(|cfg| {
web::scope("/caldav") configure_dav(cfg, "/caldav".to_string(), auth.clone(), cal_store.clone())
.configure(|cfg| configure_dav(cfg, "/caldav".to_string(), cal_store.clone())), }))
)
.service( .service(
web::scope("/.well-known") web::scope("/.well-known")
.configure(|cfg| configure_well_known(cfg, "/caldav".to_string())), .configure(|cfg| configure_well_known(cfg, "/caldav".to_string())),