Move authentication middleware into the caldav scope

This commit is contained in:
Lennart
2024-10-03 19:55:05 +02:00
parent 5a8644032f
commit dd3d05907c
4 changed files with 83 additions and 71 deletions

View File

@@ -8,6 +8,7 @@ use root::RootResourceService;
use rustical_dav::methods::{ use rustical_dav::methods::{
propfind::ServicePrefix, route_delete, route_propfind, route_proppatch, propfind::ServicePrefix, route_delete, route_propfind, route_proppatch,
}; };
use rustical_store::auth::{AuthenticationMiddleware, AuthenticationProvider};
use rustical_store::CalendarStore; use rustical_store::CalendarStore;
use std::str::FromStr; use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
@@ -29,9 +30,10 @@ pub fn configure_well_known(cfg: &mut web::ServiceConfig, caldav_root: String) {
cfg.service(web::redirect("/caldav", caldav_root).permanent()); cfg.service(web::redirect("/caldav", caldav_root).permanent());
} }
pub fn configure_dav<C: CalendarStore + ?Sized>( pub fn configure_dav<AP: AuthenticationProvider, C: CalendarStore + ?Sized>(
cfg: &mut web::ServiceConfig, cfg: &mut web::ServiceConfig,
prefix: String, prefix: String,
auth_provider: Arc<AP>,
store: Arc<RwLock<C>>, store: Arc<RwLock<C>>,
) { ) {
let propfind_method = || web::method(Method::from_str("PROPFIND").unwrap()); let propfind_method = || web::method(Method::from_str("PROPFIND").unwrap());
@@ -39,82 +41,89 @@ pub fn configure_dav<C: CalendarStore + ?Sized>(
let report_method = || web::method(Method::from_str("REPORT").unwrap()); let report_method = || web::method(Method::from_str("REPORT").unwrap());
let mkcalendar_method = || web::method(Method::from_str("MKCALENDAR").unwrap()); let mkcalendar_method = || web::method(Method::from_str("MKCALENDAR").unwrap());
cfg.app_data(Data::new(CalDavContext { cfg.service(
store: store.clone(), web::scope("")
})) .wrap(AuthenticationMiddleware::new(auth_provider))
.app_data(Data::new(ServicePrefix(prefix))) .app_data(Data::new(CalDavContext {
.app_data(Data::from(store.clone())) store: store.clone(),
.service( }))
web::resource("{path:.*}") .app_data(Data::new(ServicePrefix(prefix)))
// Without the guard this service would handle all requests .app_data(Data::from(store.clone()))
.guard(guard::Method(Method::OPTIONS)) .service(
.to(options_handler), web::resource("{path:.*}")
) // Without the guard this service would handle all requests
.service( .guard(guard::Method(Method::OPTIONS))
web::resource("") .to(options_handler),
.route(propfind_method().to(route_propfind::<RootResourceService>)) )
.route(proppatch_method().to(route_proppatch::<RootResourceService>)), .service(
) web::resource("")
.service( .route(propfind_method().to(route_propfind::<RootResourceService>))
web::scope("/user").service( .route(proppatch_method().to(route_proppatch::<RootResourceService>)),
web::scope("/{principal}") )
.service( .service(
web::resource("") web::scope("/user").service(
.route(propfind_method().to(route_propfind::<PrincipalResourceService<C>>)) web::scope("/{principal}")
.route(
proppatch_method().to(route_proppatch::<PrincipalResourceService<C>>),
),
)
.service(
web::scope("/{calendar}")
.service( .service(
web::resource("") web::resource("")
.route(
report_method()
.to(calendar::methods::report::route_report_calendar::<C>),
)
.route( .route(
propfind_method() propfind_method()
.to(route_propfind::<CalendarResourceService<C>>), .to(route_propfind::<PrincipalResourceService<C>>),
) )
.route( .route(
proppatch_method() proppatch_method()
.to(route_proppatch::<CalendarResourceService<C>>), .to(route_proppatch::<PrincipalResourceService<C>>),
)
.route(
web::method(Method::DELETE)
.to(route_delete::<CalendarResourceService<C>>),
)
.route(
mkcalendar_method()
.to(calendar::methods::mkcalendar::route_mkcalendar::<C>),
), ),
) )
.service( .service(
web::resource("/{event}") web::scope("/{calendar}")
.route( .service(
propfind_method() web::resource("")
.to(route_propfind::<CalendarObjectResourceService<C>>), .route(report_method().to(
calendar::methods::report::route_report_calendar::<C>,
))
.route(
propfind_method()
.to(route_propfind::<CalendarResourceService<C>>),
)
.route(
proppatch_method()
.to(route_proppatch::<CalendarResourceService<C>>),
)
.route(
web::method(Method::DELETE)
.to(route_delete::<CalendarResourceService<C>>),
)
.route(mkcalendar_method().to(
calendar::methods::mkcalendar::route_mkcalendar::<C>,
)),
) )
.route( .service(
proppatch_method() web::resource("/{event}")
.to(route_proppatch::<CalendarObjectResourceService<C>>), .route(
) propfind_method().to(route_propfind::<
.route( CalendarObjectResourceService<C>,
web::method(Method::DELETE) >),
.to(route_delete::<CalendarObjectResourceService<C>>), )
) .route(proppatch_method().to(route_proppatch::<
.route( CalendarObjectResourceService<C>,
web::method(Method::GET) >))
.to(calendar_object::methods::get_event::<C>), .route(
) web::method(Method::DELETE).to(route_delete::<
.route( CalendarObjectResourceService<C>,
web::method(Method::PUT) >),
.to(calendar_object::methods::put_event::<C>), )
.route(
web::method(Method::GET)
.to(calendar_object::methods::get_event::<C>),
)
.route(
web::method(Method::PUT)
.to(calendar_object::methods::put_event::<C>),
),
), ),
), ),
), ),
), ),
); );
} }

View File

@@ -53,7 +53,7 @@ impl<S, B, AP> Service<ServiceRequest> for InnerAuthenticationMiddleware<S, AP>
where where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = actix_web::Error> + 'static, S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = actix_web::Error> + 'static,
S::Future: 'static, S::Future: 'static,
AP: AuthenticationProvider + 'static, AP: AuthenticationProvider,
{ {
type Response = ServiceResponse<B>; type Response = ServiceResponse<B>;
type Error = actix_web::Error; type Error = actix_web::Error;

View File

@@ -6,11 +6,10 @@ use crate::error::Error;
use async_trait::async_trait; use async_trait::async_trait;
#[async_trait] #[async_trait]
pub trait AuthenticationProvider { pub trait AuthenticationProvider: 'static {
async fn validate_user_token(&self, user_id: &str, token: &str) -> Result<Option<User>, Error>; async fn validate_user_token(&self, user_id: &str, token: &str) -> Result<Option<User>, Error>;
} }
pub use middleware::AuthenticationMiddleware; pub use middleware::AuthenticationMiddleware;
pub use static_user_store::{StaticUserStore, StaticUserStoreConfig}; pub use static_user_store::{StaticUserStore, StaticUserStoreConfig};
pub use user::User; pub use user::User;

View File

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