]> Untitled Git - lemmy.git/blob - crates/apub/src/fetcher/webfinger.rs
e623bee22496df70542a7f537f6b91e1e19a734a
[lemmy.git] / crates / apub / src / fetcher / webfinger.rs
1 use crate::{generate_local_apub_endpoint, EndpointType};
2 use itertools::Itertools;
3 use lemmy_apub_lib::{
4   object_id::ObjectId,
5   traits::{ActorType, ApubObject},
6 };
7 use lemmy_db_schema::newtypes::DbUrl;
8 use lemmy_utils::{
9   request::{retry, RecvError},
10   LemmyError,
11 };
12 use lemmy_websocket::LemmyContext;
13 use serde::{Deserialize, Serialize};
14 use tracing::debug;
15 use url::Url;
16
17 #[derive(Serialize, Deserialize, Debug)]
18 pub struct WebfingerLink {
19   pub rel: Option<String>,
20   #[serde(rename = "type")]
21   pub kind: Option<String>,
22   pub href: Option<Url>,
23 }
24
25 #[derive(Serialize, Deserialize, Debug)]
26 pub struct WebfingerResponse {
27   pub subject: String,
28   pub links: Vec<WebfingerLink>,
29 }
30
31 /// Takes in a shortname of the type dessalines@xyz.tld or dessalines (assumed to be local), and
32 /// outputs the actor id. Used in the API for communities and users.
33 ///
34 /// TODO: later provide a method in ApubObject to generate the endpoint, so that we dont have to
35 ///       pass in EndpointType
36 #[tracing::instrument(skip_all)]
37 pub async fn webfinger_resolve<Kind>(
38   identifier: &str,
39   endpoint_type: EndpointType,
40   context: &LemmyContext,
41   request_counter: &mut i32,
42 ) -> Result<DbUrl, LemmyError>
43 where
44   Kind: ApubObject<DataType = LemmyContext> + ActorType + Send + 'static,
45   for<'de2> <Kind as ApubObject>::ApubType: serde::Deserialize<'de2>,
46 {
47   // remote actor
48   if identifier.contains('@') {
49     webfinger_resolve_actor::<Kind>(identifier, context, request_counter).await
50   }
51   // local actor
52   else {
53     let domain = context.settings().get_protocol_and_hostname();
54     Ok(generate_local_apub_endpoint(
55       endpoint_type,
56       identifier,
57       &domain,
58     )?)
59   }
60 }
61
62 /// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
63 /// using webfinger.
64 #[tracing::instrument(skip_all)]
65 pub(crate) async fn webfinger_resolve_actor<Kind>(
66   identifier: &str,
67   context: &LemmyContext,
68   request_counter: &mut i32,
69 ) -> Result<DbUrl, LemmyError>
70 where
71   Kind: ApubObject<DataType = LemmyContext> + ActorType + Send + 'static,
72   for<'de2> <Kind as ApubObject>::ApubType: serde::Deserialize<'de2>,
73 {
74   let protocol = context.settings().get_protocol_string();
75   let (_, domain) = identifier
76     .splitn(2, '@')
77     .collect_tuple()
78     .expect("invalid query");
79   let fetch_url = format!(
80     "{}://{}/.well-known/webfinger?resource=acct:{}",
81     protocol, domain, identifier
82   );
83   debug!("Fetching webfinger url: {}", &fetch_url);
84
85   *request_counter += 1;
86   if *request_counter > context.settings().http_fetch_retry_limit {
87     return Err(LemmyError::from_message("Request retry limit reached"));
88   }
89
90   let response = retry(|| context.client().get(&fetch_url).send()).await?;
91
92   let res: WebfingerResponse = response
93     .json()
94     .await
95     .map_err(|e| RecvError(e.to_string()))?;
96
97   let links: Vec<Url> = res
98     .links
99     .iter()
100     .filter(|link| {
101       if let Some(type_) = &link.kind {
102         type_.starts_with("application/")
103       } else {
104         false
105       }
106     })
107     .map(|l| l.href.clone())
108     .flatten()
109     .collect();
110   for l in links {
111     let object = ObjectId::<Kind>::new(l)
112       .dereference(context, context.client(), request_counter)
113       .await;
114     if object.is_ok() {
115       return object.map(|o| o.actor_id().into());
116     }
117   }
118   let error = LemmyError::from(anyhow::anyhow!(
119     "Failed to resolve actor for {}",
120     identifier
121   ));
122   Err(error.with_message("failed_to_resolve"))
123 }