BlueGit

ict rusty-teams

Files

rusty-teams / crates / chatsvc / src / caldav / discovery.rs

discovery.rs

discovery.rs 3.3 KiB Download Raw
1use reqwest::Url;
2
3use crate::error::{Error, Result};
4
5use super::client::CalDavClient;
6use super::xml::{parse_multistatus, propfind_body};
7
8#[derive(Debug, Clone)]
9pub struct DiscoverResult {
10 pub principal: String,
11 pub home_set: String,
12 pub user_addresses: Vec<String>,
13 pub schedule_outbox: Option<String>,
14}
15
16pub async fn discover(client: &CalDavClient, start_url: &str) -> Result<DiscoverResult> {
17 let well_known = join_well_known(start_url)?;
18 let principal_href = match current_user_principal(client, &well_known).await {
19 Ok(href) => href,
20 Err(_) => current_user_principal(client, start_url).await?,
21 };
22 let principal = resolve_url(start_url, &principal_href)?;
23 let home_href = calendar_home_set(client, &principal).await?;
24 let home_set = resolve_url(&principal, &home_href)?;
25 let (user_addresses, schedule_outbox) = user_scheduling(client, &principal).await?;
26 Ok(DiscoverResult {
27 principal,
28 home_set,
29 user_addresses,
30 schedule_outbox,
31 })
32}
33
34fn join_well_known(url: &str) -> Result<String> {
35 let parsed = Url::parse(url).map_err(|e| Error::UnexpectedAnswer(e.to_string()))?;
36 let mut base = parsed.clone();
37 base.set_path("/.well-known/caldav");
38 base.set_query(None);
39 base.set_fragment(None);
40 Ok(base.to_string())
41}
42
43async fn current_user_principal(client: &CalDavClient, url: &str) -> Result<String> {
44 let body = propfind_body(&["<d:current-user-principal/>"]);
45 let xml = client.propfind(url, "0", &body).await?;
46 parse_multistatus(&xml)
47 .into_iter()
48 .find_map(|r| r.current_user_principal)
49 .ok_or_else(|| Error::UnexpectedAnswer("CalDAV current-user-principal missing".into()))
50}
51
52async fn calendar_home_set(client: &CalDavClient, principal: &str) -> Result<String> {
53 let body = propfind_body(&["<c:calendar-home-set/>"]);
54 let xml = client.propfind(principal, "0", &body).await?;
55 parse_multistatus(&xml)
56 .into_iter()
57 .find_map(|r| r.calendar_home_set)
58 .ok_or_else(|| Error::UnexpectedAnswer("CalDAV calendar-home-set missing".into()))
59}
60
61async fn user_scheduling(
62 client: &CalDavClient,
63 principal: &str,
64) -> Result<(Vec<String>, Option<String>)> {
65 let body = propfind_body(&["<c:calendar-user-address-set/>", "<c:schedule-outbox-URL/>"]);
66 let xml = client.propfind(principal, "0", &body).await?;
67 let mut addresses = Vec::new();
68 let mut outbox = None;
69 for response in parse_multistatus(&xml) {
70 addresses.extend(response.calendar_user_addresses);
71 if outbox.is_none() {
72 outbox = response.schedule_outbox;
73 }
74 }
75 Ok((addresses, outbox))
76}
77
78pub fn resolve_url(base: &str, href: &str) -> Result<String> {
79 if href.starts_with("http://") || href.starts_with("https://") {
80 return Ok(href.to_owned());
81 }
82 let base = Url::parse(base).map_err(|e| Error::UnexpectedAnswer(e.to_string()))?;
83 Ok(base
84 .join(href)
85 .map_err(|e| Error::UnexpectedAnswer(e.to_string()))?
86 .to_string())
87}
88
89pub fn reject_https_downgrade(from: &str, to: &str) -> Result<()> {
90 if from.starts_with("https://") && to.starts_with("http://") {
91 return Err(Error::UnexpectedAnswer(
92 "CalDAV refused HTTPS to HTTP redirect (credentials would be sent in cleartext)".into(),
93 ));
94 }
95 Ok(())
96}