dav: Make reusable for other projects

This commit is contained in:
Lennart
2025-04-18 13:26:44 +02:00
parent 626eff0373
commit 54e327d764
36 changed files with 210 additions and 121 deletions

View File

@@ -12,7 +12,6 @@ actix-web = { workspace = true }
async-trait = { workspace = true }
futures-util = { workspace = true }
quick-xml = { workspace = true }
rustical_store = { workspace = true }
serde = { workspace = true }
thiserror = { workspace = true }
itertools = { workspace = true }

View File

@@ -1,10 +1,10 @@
use crate::{
Principal,
privileges::UserPrivilegeSet,
resource::{NamedRoute, Resource},
xml::{HrefElement, Resourcetype},
};
use actix_web::dev::ResourceMap;
use rustical_store::auth::User;
use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumUnitVariants, EnumVariants)]
@@ -31,7 +31,7 @@ pub trait CommonPropertiesExtension: Resource {
fn get_prop(
&self,
rmap: &ResourceMap,
user: &User,
principal: &Self::Principal,
prop: &CommonPropertiesPropName,
) -> Result<CommonPropertiesProp, <Self as Resource>::Error> {
Ok(match prop {
@@ -40,13 +40,13 @@ pub trait CommonPropertiesExtension: Resource {
}
CommonPropertiesPropName::CurrentUserPrincipal => {
CommonPropertiesProp::CurrentUserPrincipal(
Self::PrincipalResource::get_url(rmap, [&user.id])
Self::PrincipalResource::get_url(rmap, [&principal.get_id()])
.unwrap()
.into(),
)
}
CommonPropertiesPropName::CurrentUserPrivilegeSet => {
CommonPropertiesProp::CurrentUserPrivilegeSet(self.get_user_privileges(user)?)
CommonPropertiesProp::CurrentUserPrivilegeSet(self.get_user_privileges(principal)?)
}
CommonPropertiesPropName::Owner => {
CommonPropertiesProp::Owner(self.get_owner().map(|owner| {

View File

@@ -1,37 +0,0 @@
use crate::push::Transports;
use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumUnitVariants, EnumVariants)]
#[xml(unit_variants_ident = "DavPushExtensionPropName")]
pub enum DavPushExtensionProp {
// WebDav Push
#[xml(skip_deserializing)]
#[xml(ns = "crate::namespace::NS_DAVPUSH")]
Transports(Transports),
#[xml(ns = "crate::namespace::NS_DAVPUSH")]
Topic(String),
}
pub trait DavPushExtension {
fn get_topic(&self) -> String;
fn get_prop(
&self,
prop: &DavPushExtensionPropName,
) -> Result<DavPushExtensionProp, crate::Error> {
Ok(match &prop {
DavPushExtensionPropName::Transports => {
DavPushExtensionProp::Transports(Default::default())
}
DavPushExtensionPropName::Topic => DavPushExtensionProp::Topic(self.get_topic()),
})
}
fn set_prop(&self, _prop: DavPushExtensionProp) -> Result<(), crate::Error> {
Err(crate::Error::PropReadOnly)
}
fn remove_prop(&self, _prop: &DavPushExtensionPropName) -> Result<(), crate::Error> {
Err(crate::Error::PropReadOnly)
}
}

View File

@@ -1,7 +1,5 @@
mod common;
mod davpush;
mod synctoken;
pub use common::*;
pub use davpush::*;
pub use synctoken::*;

View File

@@ -3,9 +3,13 @@ pub mod error;
pub mod extensions;
pub mod namespace;
pub mod privileges;
pub mod push;
pub mod resource;
pub mod resources;
pub mod xml;
use actix_web::FromRequest;
pub use error::Error;
pub trait Principal: std::fmt::Debug + Clone + FromRequest + 'static {
fn get_id(&self) -> &str;
}

View File

@@ -1,7 +0,0 @@
mod prop;
mod push_notifier;
mod push_register;
pub use prop::*;
pub use push_notifier::push_notifier;
pub use push_register::*;

View File

@@ -1,22 +0,0 @@
use rustical_xml::XmlSerialize;
#[derive(Debug, Clone, XmlSerialize, PartialEq)]
pub enum Transport {
#[xml(ns = "crate::namespace::NS_DAVPUSH")]
WebPush,
}
#[derive(Debug, Clone, XmlSerialize, PartialEq)]
pub struct Transports {
#[xml(flatten, ty = "untagged")]
#[xml(ns = "crate::namespace::NS_DAVPUSH")]
transports: Vec<Transport>,
}
impl Default for Transports {
fn default() -> Self {
Self {
transports: vec![Transport::WebPush],
}
}
}

View File

@@ -1,94 +0,0 @@
use crate::xml::multistatus::PropstatElement;
use actix_web::http::StatusCode;
use rustical_store::{CollectionOperation, CollectionOperationType, SubscriptionStore};
use rustical_xml::{XmlRootTag, XmlSerialize, XmlSerializeRoot};
use std::sync::Arc;
use tokio::sync::mpsc::Receiver;
use tracing::{error, info, warn};
#[derive(XmlSerialize, Debug)]
struct PushMessageProp {
#[xml(ns = "crate::namespace::NS_DAV")]
topic: String,
#[xml(ns = "crate::namespace::NS_DAV")]
sync_token: Option<String>,
}
#[derive(XmlSerialize, XmlRootTag, Debug)]
#[xml(root = b"push-message", ns = "crate::namespace::NS_DAVPUSH")]
#[xml(ns_prefix(crate::namespace::NS_DAVPUSH = b"", crate::namespace::NS_DAV = b"D",))]
struct PushMessage {
#[xml(ns = "crate::namespace::NS_DAV")]
propstat: PropstatElement<PushMessageProp>,
}
pub async fn push_notifier(
allowed_push_servers: Option<Vec<String>>,
mut recv: Receiver<CollectionOperation>,
sub_store: Arc<impl SubscriptionStore>,
) {
let client = reqwest::Client::new();
while let Some(message) = recv.recv().await {
let subscribers = match sub_store.get_subscriptions(&message.topic).await {
Ok(subs) => subs,
Err(err) => {
error!("{err}");
continue;
}
};
let status = match message.r#type {
CollectionOperationType::Object => StatusCode::OK,
CollectionOperationType::Delete => StatusCode::NOT_FOUND,
};
let push_message = PushMessage {
propstat: PropstatElement {
prop: PushMessageProp {
topic: message.topic,
sync_token: message.sync_token,
},
status,
},
};
let mut output: Vec<_> = b"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".into();
let mut writer = quick_xml::Writer::new_with_indent(&mut output, b' ', 4);
if let Err(err) = push_message.serialize_root(&mut writer) {
error!("Could not serialize push message: {}", err);
continue;
}
let payload = String::from_utf8(output).unwrap();
for subscriber in subscribers {
let push_resource = subscriber.push_resource;
let allowed = if let Some(allowed_push_servers) = &allowed_push_servers {
if let Ok(resource_url) = reqwest::Url::parse(&push_resource) {
let origin = resource_url.origin().ascii_serialization();
allowed_push_servers
.iter()
.any(|allowed_push_server| allowed_push_server == &origin)
} else {
warn!("Invalid push url: {push_resource}");
false
}
} else {
true
};
if allowed {
info!("Sending a push message to {}: {}", push_resource, payload);
if let Err(err) = client
.post(push_resource)
.body(payload.to_owned())
.send()
.await
{
error!("{err}");
}
} else {
warn!("Not sending a push notification to {} since it's not allowed in dav_push::allowed_push_servers", push_resource);
}
}
}
}

View File

@@ -1,59 +0,0 @@
use rustical_xml::{XmlDeserialize, XmlRootTag};
#[derive(XmlDeserialize, Clone, Debug, PartialEq)]
#[xml(ns = "crate::namespace::NS_DAVPUSH")]
pub struct WebPushSubscription {
#[xml(ns = "crate::namespace::NS_DAVPUSH")]
pub push_resource: String,
}
#[derive(XmlDeserialize, Clone, Debug, PartialEq)]
pub struct SubscriptionElement {
#[xml(ns = "crate::namespace::NS_DAVPUSH")]
pub web_push_subscription: WebPushSubscription,
}
#[derive(XmlDeserialize, XmlRootTag, Clone, Debug, PartialEq)]
#[xml(root = b"push-register")]
#[xml(ns = "crate::namespace::NS_DAVPUSH")]
pub struct PushRegister {
#[xml(ns = "crate::namespace::NS_DAVPUSH")]
pub subscription: SubscriptionElement,
#[xml(ns = "crate::namespace::NS_DAVPUSH")]
pub expires: Option<String>,
}
#[cfg(test)]
mod tests {
use super::*;
use rustical_xml::XmlDocument;
#[test]
fn test_xml_push_register() {
let push_register = PushRegister::parse_str(
r#"
<?xml version="1.0" encoding="utf-8" ?>
<push-register xmlns="https://bitfire.at/webdav-push">
<subscription>
<web-push-subscription>
<push-resource>https://up.example.net/yohd4yai5Phiz1wi</push-resource>
</web-push-subscription>
</subscription>
<expires>Wed, 20 Dec 2023 10:03:31 GMT</expires>
</push-register>
"#,
)
.unwrap();
assert_eq!(
push_register,
PushRegister {
subscription: SubscriptionElement {
web_push_subscription: WebPushSubscription {
push_resource: "https://up.example.net/yohd4yai5Phiz1wi".to_owned()
}
},
expires: Some("Wed, 20 Dec 2023 10:03:31 GMT".to_owned())
}
)
}
}

View File

@@ -1,16 +1,15 @@
use crate::Error;
use crate::privileges::UserPrivilege;
use crate::resource::Resource;
use crate::resource::ResourceService;
use crate::Error;
use actix_web::HttpRequest;
use actix_web::HttpResponse;
use actix_web::Responder;
use actix_web::http::header::IfMatch;
use actix_web::http::header::IfNoneMatch;
use actix_web::web;
use actix_web::web::Data;
use actix_web::web::Path;
use actix_web::HttpRequest;
use actix_web::HttpResponse;
use actix_web::Responder;
use rustical_store::auth::User;
use tracing::instrument;
use tracing_actix_web::RootSpan;
@@ -18,7 +17,7 @@ use tracing_actix_web::RootSpan;
pub async fn route_delete<R: ResourceService>(
path: Path<R::PathComponents>,
req: HttpRequest,
user: User,
principal: R::Principal,
resource_service: Data<R>,
root_span: RootSpan,
if_match: web::Header<IfMatch>,
@@ -32,7 +31,7 @@ pub async fn route_delete<R: ResourceService>(
let resource = resource_service.get_resource(&path).await?;
let privileges = resource.get_user_privileges(&user)?;
let privileges = resource.get_user_privileges(&principal)?;
if !privileges.has(&UserPrivilege::Write) {
return Err(Error::Unauthorized.into());
}

View File

@@ -1,3 +1,4 @@
use crate::Error;
use crate::depth_header::Depth;
use crate::privileges::UserPrivilege;
use crate::resource::Resource;
@@ -6,11 +7,9 @@ use crate::xml::MultistatusElement;
use crate::xml::PropElement;
use crate::xml::PropfindElement;
use crate::xml::PropfindType;
use crate::Error;
use actix_web::HttpRequest;
use actix_web::web::Data;
use actix_web::web::Path;
use actix_web::HttpRequest;
use rustical_store::auth::User;
use rustical_xml::XmlDocument;
use tracing::instrument;
use tracing_actix_web::RootSpan;
@@ -21,7 +20,7 @@ pub(crate) async fn route_propfind<R: ResourceService>(
path: Path<R::PathComponents>,
body: String,
req: HttpRequest,
user: User,
user: R::Principal,
depth: Depth,
root_span: RootSpan,
resource_service: Data<R>,

View File

@@ -1,15 +1,14 @@
use crate::Error;
use crate::privileges::UserPrivilege;
use crate::resource::Resource;
use crate::resource::ResourceService;
use crate::xml::multistatus::{PropstatElement, PropstatWrapper, ResponseElement};
use crate::xml::MultistatusElement;
use crate::xml::TagList;
use crate::Error;
use crate::xml::multistatus::{PropstatElement, PropstatWrapper, ResponseElement};
use actix_web::http::StatusCode;
use actix_web::web::Data;
use actix_web::{web::Path, HttpRequest};
use actix_web::{HttpRequest, web::Path};
use quick_xml::name::Namespace;
use rustical_store::auth::User;
use rustical_xml::EnumUnitVariants;
use rustical_xml::Unparsed;
use rustical_xml::XmlDeserialize;
@@ -69,7 +68,7 @@ pub(crate) async fn route_proppatch<R: ResourceService>(
path: Path<R::PathComponents>,
body: String,
req: HttpRequest,
user: User,
principal: R::Principal,
root_span: RootSpan,
resource_service: Data<R>,
) -> Result<MultistatusElement<String, String>, R::Error> {
@@ -81,7 +80,7 @@ pub(crate) async fn route_proppatch<R: ResourceService>(
) = XmlDocument::parse_str(&body).map_err(Error::XmlError)?;
let mut resource = resource_service.get_resource(&path).await?;
let privileges = resource.get_user_privileges(&user)?;
let privileges = resource.get_user_privileges(&principal)?;
if !privileges.has(&UserPrivilege::Write) {
return Err(Error::Unauthorized.into());
}
@@ -131,7 +130,7 @@ pub(crate) async fn route_proppatch<R: ResourceService>(
}
}
Operation::Remove(remove_el) => {
let propname = remove_el.prop.0 .0;
let propname = remove_el.prop.0.0;
match <<R::Resource as Resource>::Prop as EnumUnitVariants>::UnitVariants::from_str(
&propname,
) {

View File

@@ -1,15 +1,14 @@
use crate::privileges::UserPrivilegeSet;
use crate::xml::multistatus::{PropTagWrapper, PropstatElement, PropstatWrapper};
use crate::xml::Resourcetype;
use crate::xml::{multistatus::ResponseElement, TagList};
use crate::Error;
use crate::xml::multistatus::{PropTagWrapper, PropstatElement, PropstatWrapper};
use crate::xml::{TagList, multistatus::ResponseElement};
use crate::{Error, Principal};
use actix_web::dev::ResourceMap;
use actix_web::http::header::{EntityTag, IfMatch, IfNoneMatch};
use actix_web::{http::StatusCode, ResponseError};
use actix_web::{ResponseError, http::StatusCode};
use itertools::Itertools;
use quick_xml::name::Namespace;
pub use resource_service::ResourceService;
use rustical_store::auth::User;
use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
use std::str::FromStr;
@@ -28,6 +27,7 @@ pub trait Resource: Clone + 'static {
type Prop: ResourceProp + PartialEq + Clone + EnumVariants + EnumUnitVariants;
type Error: ResponseError + From<crate::Error>;
type PrincipalResource: Resource + NamedRoute;
type Principal: Principal;
fn get_resourcetype(&self) -> Resourcetype;
@@ -38,7 +38,7 @@ pub trait Resource: Clone + 'static {
fn get_prop(
&self,
rmap: &ResourceMap,
user: &User,
principal: &Self::Principal,
prop: &<Self::Prop as EnumUnitVariants>::UnitVariants,
) -> Result<Self::Prop, Self::Error>;
@@ -93,13 +93,16 @@ pub trait Resource: Clone + 'static {
}
}
fn get_user_privileges(&self, user: &User) -> Result<UserPrivilegeSet, Self::Error>;
fn get_user_privileges(
&self,
principal: &Self::Principal,
) -> Result<UserPrivilegeSet, Self::Error>;
fn propfind(
&self,
path: &str,
props: &[&str],
user: &User,
principal: &Self::Principal,
rmap: &ResourceMap,
) -> Result<ResponseElement<Self::Prop>, Self::Error> {
let mut props = props.to_vec();
@@ -152,7 +155,7 @@ pub trait Resource: Clone + 'static {
let prop_responses = valid_props
.into_iter()
.map(|prop| self.get_prop(rmap, user, &prop))
.map(|prop| self.get_prop(rmap, principal, &prop))
.collect::<Result<Vec<_>, Self::Error>>()?;
let mut propstats = vec![PropstatWrapper::Normal(PropstatElement {

View File

@@ -2,20 +2,23 @@ use actix_web::dev::{AppService, HttpServiceFactory};
use actix_web::error::UrlGenerationError;
use actix_web::test::TestRequest;
use actix_web::web::Data;
use actix_web::{dev::ResourceMap, http::Method, web, ResponseError};
use actix_web::{ResponseError, dev::ResourceMap, http::Method, web};
use async_trait::async_trait;
use serde::Deserialize;
use std::str::FromStr;
use super::methods::{route_delete, route_propfind, route_proppatch};
use crate::Principal;
use super::Resource;
use super::methods::{route_delete, route_propfind, route_proppatch};
#[async_trait(?Send)]
pub trait ResourceService: Sized + 'static {
type MemberType: Resource<Error = Self::Error>;
type MemberType: Resource<Error = Self::Error, Principal = Self::Principal>;
type PathComponents: for<'de> Deserialize<'de> + Sized + Clone + 'static; // defines how the resource URI maps to parameters, i.e. /{principal}/{calendar} -> (String, String)
type Resource: Resource<Error = Self::Error>;
type Resource: Resource<Error = Self::Error, Principal = Self::Principal>;
type Error: ResponseError + From<crate::Error>;
type Principal: Principal;
async fn get_members(
&self,

View File

@@ -1,3 +1,4 @@
use crate::Principal;
use crate::extensions::{
CommonPropertiesExtension, CommonPropertiesProp, CommonPropertiesPropName,
};
@@ -6,22 +7,22 @@ use crate::resource::{NamedRoute, Resource, ResourceService};
use crate::xml::{Resourcetype, ResourcetypeInner};
use actix_web::dev::ResourceMap;
use async_trait::async_trait;
use rustical_store::auth::User;
use std::marker::PhantomData;
#[derive(Clone)]
pub struct RootResource<PR: Resource>(PhantomData<PR>);
pub struct RootResource<PR: Resource, P: Principal>(PhantomData<PR>, PhantomData<P>);
impl<PR: Resource> Default for RootResource<PR> {
impl<PR: Resource, P: Principal> Default for RootResource<PR, P> {
fn default() -> Self {
Self(Default::default())
Self(PhantomData, PhantomData)
}
}
impl<PR: Resource + NamedRoute> Resource for RootResource<PR> {
impl<PR: Resource + NamedRoute, P: Principal> Resource for RootResource<PR, P> {
type Prop = CommonPropertiesProp;
type Error = PR::Error;
type PrincipalResource = PR;
type Principal = P;
fn get_resourcetype(&self) -> Resourcetype {
Resourcetype(&[ResourcetypeInner(
@@ -33,34 +34,37 @@ impl<PR: Resource + NamedRoute> Resource for RootResource<PR> {
fn get_prop(
&self,
rmap: &ResourceMap,
user: &User,
user: &P,
prop: &CommonPropertiesPropName,
) -> Result<Self::Prop, Self::Error> {
CommonPropertiesExtension::get_prop(self, rmap, user, prop)
}
fn get_user_privileges(&self, _user: &User) -> Result<UserPrivilegeSet, Self::Error> {
fn get_user_privileges(&self, _user: &P) -> Result<UserPrivilegeSet, Self::Error> {
Ok(UserPrivilegeSet::all())
}
}
#[derive(Clone)]
pub struct RootResourceService<PR: Resource>(PhantomData<PR>);
pub struct RootResourceService<PR: Resource, P: Principal>(PhantomData<PR>, PhantomData<P>);
impl<PR: Resource> Default for RootResourceService<PR> {
impl<PR: Resource, P: Principal> Default for RootResourceService<PR, P> {
fn default() -> Self {
Self(PhantomData)
Self(PhantomData, PhantomData)
}
}
#[async_trait(?Send)]
impl<PR: Resource + NamedRoute> ResourceService for RootResourceService<PR> {
impl<PR: Resource<Principal = P> + NamedRoute, P: Principal> ResourceService
for RootResourceService<PR, P>
{
type PathComponents = ();
type MemberType = PR;
type Resource = RootResource<PR>;
type Resource = RootResource<PR, P>;
type Error = PR::Error;
type Principal = P;
async fn get_resource(&self, _: &()) -> Result<Self::Resource, Self::Error> {
Ok(RootResource::<PR>::default())
Ok(RootResource::<PR, P>::default())
}
}