diff --git a/crates/caldav/src/lib.rs b/crates/caldav/src/lib.rs index 0e58a85..a3ae9dd 100644 --- a/crates/caldav/src/lib.rs +++ b/crates/caldav/src/lib.rs @@ -8,6 +8,7 @@ use root::RootResourceService; use rustical_dav::methods::{ propfind::ServicePrefix, route_delete, route_propfind, route_proppatch, }; +use rustical_store::auth::{AuthenticationMiddleware, AuthenticationProvider}; use rustical_store::CalendarStore; use std::str::FromStr; 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()); } -pub fn configure_dav( +pub fn configure_dav( cfg: &mut web::ServiceConfig, prefix: String, + auth_provider: Arc, store: Arc>, ) { let propfind_method = || web::method(Method::from_str("PROPFIND").unwrap()); @@ -39,82 +41,89 @@ pub fn configure_dav( let report_method = || web::method(Method::from_str("REPORT").unwrap()); let mkcalendar_method = || web::method(Method::from_str("MKCALENDAR").unwrap()); - cfg.app_data(Data::new(CalDavContext { - store: store.clone(), - })) - .app_data(Data::new(ServicePrefix(prefix))) - .app_data(Data::from(store.clone())) - .service( - web::resource("{path:.*}") - // Without the guard this service would handle all requests - .guard(guard::Method(Method::OPTIONS)) - .to(options_handler), - ) - .service( - web::resource("") - .route(propfind_method().to(route_propfind::)) - .route(proppatch_method().to(route_proppatch::)), - ) - .service( - web::scope("/user").service( - web::scope("/{principal}") - .service( - web::resource("") - .route(propfind_method().to(route_propfind::>)) - .route( - proppatch_method().to(route_proppatch::>), - ), - ) - .service( - web::scope("/{calendar}") + cfg.service( + web::scope("") + .wrap(AuthenticationMiddleware::new(auth_provider)) + .app_data(Data::new(CalDavContext { + store: store.clone(), + })) + .app_data(Data::new(ServicePrefix(prefix))) + .app_data(Data::from(store.clone())) + .service( + web::resource("{path:.*}") + // Without the guard this service would handle all requests + .guard(guard::Method(Method::OPTIONS)) + .to(options_handler), + ) + .service( + web::resource("") + .route(propfind_method().to(route_propfind::)) + .route(proppatch_method().to(route_proppatch::)), + ) + .service( + web::scope("/user").service( + web::scope("/{principal}") .service( web::resource("") - .route( - report_method() - .to(calendar::methods::report::route_report_calendar::), - ) .route( propfind_method() - .to(route_propfind::>), + .to(route_propfind::>), ) .route( proppatch_method() - .to(route_proppatch::>), - ) - .route( - web::method(Method::DELETE) - .to(route_delete::>), - ) - .route( - mkcalendar_method() - .to(calendar::methods::mkcalendar::route_mkcalendar::), + .to(route_proppatch::>), ), ) .service( - web::resource("/{event}") - .route( - propfind_method() - .to(route_propfind::>), + web::scope("/{calendar}") + .service( + web::resource("") + .route(report_method().to( + calendar::methods::report::route_report_calendar::, + )) + .route( + propfind_method() + .to(route_propfind::>), + ) + .route( + proppatch_method() + .to(route_proppatch::>), + ) + .route( + web::method(Method::DELETE) + .to(route_delete::>), + ) + .route(mkcalendar_method().to( + calendar::methods::mkcalendar::route_mkcalendar::, + )), ) - .route( - proppatch_method() - .to(route_proppatch::>), - ) - .route( - web::method(Method::DELETE) - .to(route_delete::>), - ) - .route( - web::method(Method::GET) - .to(calendar_object::methods::get_event::), - ) - .route( - web::method(Method::PUT) - .to(calendar_object::methods::put_event::), + .service( + web::resource("/{event}") + .route( + propfind_method().to(route_propfind::< + CalendarObjectResourceService, + >), + ) + .route(proppatch_method().to(route_proppatch::< + CalendarObjectResourceService, + >)) + .route( + web::method(Method::DELETE).to(route_delete::< + CalendarObjectResourceService, + >), + ) + .route( + web::method(Method::GET) + .to(calendar_object::methods::get_event::), + ) + .route( + web::method(Method::PUT) + .to(calendar_object::methods::put_event::), + ), ), ), ), - ), + ), ); } diff --git a/crates/store/src/auth/middleware.rs b/crates/store/src/auth/middleware.rs index 16ed877..e2d03e4 100644 --- a/crates/store/src/auth/middleware.rs +++ b/crates/store/src/auth/middleware.rs @@ -53,7 +53,7 @@ impl Service for InnerAuthenticationMiddleware where S: Service, Error = actix_web::Error> + 'static, S::Future: 'static, - AP: AuthenticationProvider + 'static, + AP: AuthenticationProvider, { type Response = ServiceResponse; type Error = actix_web::Error; diff --git a/crates/store/src/auth/mod.rs b/crates/store/src/auth/mod.rs index 6a0e1d9..5b91c17 100644 --- a/crates/store/src/auth/mod.rs +++ b/crates/store/src/auth/mod.rs @@ -6,11 +6,10 @@ use crate::error::Error; use 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, Error>; } pub use middleware::AuthenticationMiddleware; pub use static_user_store::{StaticUserStore, StaticUserStoreConfig}; pub use user::User; - diff --git a/src/app.rs b/src/app.rs index d21881d..60eeafc 100644 --- a/src/app.rs +++ b/src/app.rs @@ -3,14 +3,14 @@ use actix_web::dev::{ServiceFactory, ServiceRequest, ServiceResponse}; use actix_web::middleware::{Logger, NormalizePath}; use actix_web::{web, App}; use rustical_frontend::configure_frontend; -use rustical_store::auth::{AuthenticationMiddleware, AuthenticationProvider}; +use rustical_store::auth::AuthenticationProvider; use rustical_store::CalendarStore; use std::sync::Arc; use tokio::sync::RwLock; -pub fn make_app( +pub fn make_app( cal_store: Arc>, - auth_provider: Arc, + auth_provider: Arc, ) -> App< impl ServiceFactory< ServiceRequest, @@ -23,9 +23,13 @@ pub fn make_app