mirror of https://github.com/LemmyNet/lemmy
Make webfinger standard compliant
parent
3914c6c875
commit
579ca37ddf
@ -1,3 +1,4 @@
|
|||||||
pub mod post_or_comment;
|
pub mod post_or_comment;
|
||||||
pub mod search;
|
pub mod search;
|
||||||
pub mod user_or_community;
|
pub mod user_or_community;
|
||||||
|
pub mod webfinger;
|
||||||
|
@ -0,0 +1,107 @@
|
|||||||
|
use crate::{generate_local_apub_endpoint, EndpointType};
|
||||||
|
use anyhow::anyhow;
|
||||||
|
use itertools::Itertools;
|
||||||
|
use lemmy_apub_lib::{
|
||||||
|
object_id::ObjectId,
|
||||||
|
traits::{ActorType, ApubObject},
|
||||||
|
};
|
||||||
|
use lemmy_db_schema::newtypes::DbUrl;
|
||||||
|
use lemmy_utils::{
|
||||||
|
request::{retry, RecvError},
|
||||||
|
LemmyError,
|
||||||
|
};
|
||||||
|
use lemmy_websocket::LemmyContext;
|
||||||
|
use log::debug;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct WebfingerLink {
|
||||||
|
pub rel: Option<String>,
|
||||||
|
#[serde(rename(serialize = "type", deserialize = "type"))]
|
||||||
|
pub type_: Option<String>,
|
||||||
|
pub href: Option<Url>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct WebfingerResponse {
|
||||||
|
pub subject: String,
|
||||||
|
pub links: Vec<WebfingerLink>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Takes in a shortname of the type dessalines@xyz.tld or dessalines (assumed to be local), and
|
||||||
|
/// outputs the actor id. Used in the API for communities and users.
|
||||||
|
///
|
||||||
|
/// TODO: later provide a method in ApubObject to generate the endpoint, so that we dont have to
|
||||||
|
/// pass in EndpointType
|
||||||
|
pub async fn webfinger_resolve<Kind>(
|
||||||
|
identifier: &str,
|
||||||
|
endpoint_type: EndpointType,
|
||||||
|
context: &LemmyContext,
|
||||||
|
request_counter: &mut i32,
|
||||||
|
) -> Result<DbUrl, LemmyError>
|
||||||
|
where
|
||||||
|
Kind: ApubObject<DataType = LemmyContext> + ActorType + Send + 'static,
|
||||||
|
for<'de2> <Kind as ApubObject>::ApubType: serde::Deserialize<'de2>,
|
||||||
|
{
|
||||||
|
// remote actor
|
||||||
|
if identifier.contains('@') {
|
||||||
|
webfinger_resolve_actor::<Kind>(identifier, context, request_counter).await
|
||||||
|
}
|
||||||
|
// local actor
|
||||||
|
else {
|
||||||
|
let domain = context.settings().get_protocol_and_hostname();
|
||||||
|
Ok(generate_local_apub_endpoint(
|
||||||
|
endpoint_type,
|
||||||
|
identifier,
|
||||||
|
&domain,
|
||||||
|
)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
|
||||||
|
/// using webfinger.
|
||||||
|
async fn webfinger_resolve_actor<Kind>(
|
||||||
|
identifier: &str,
|
||||||
|
context: &LemmyContext,
|
||||||
|
request_counter: &mut i32,
|
||||||
|
) -> Result<DbUrl, LemmyError>
|
||||||
|
where
|
||||||
|
Kind: ApubObject<DataType = LemmyContext> + ActorType + Send + 'static,
|
||||||
|
for<'de2> <Kind as ApubObject>::ApubType: serde::Deserialize<'de2>,
|
||||||
|
{
|
||||||
|
let protocol = context.settings().get_protocol_string();
|
||||||
|
let (_, domain) = identifier
|
||||||
|
.splitn(2, '@')
|
||||||
|
.collect_tuple()
|
||||||
|
.expect("invalid query");
|
||||||
|
let fetch_url = format!(
|
||||||
|
"{}://{}/.well-known/webfinger?resource=acct:{}",
|
||||||
|
protocol, domain, identifier
|
||||||
|
);
|
||||||
|
debug!("Fetching webfinger url: {}", &fetch_url);
|
||||||
|
|
||||||
|
let response = retry(|| context.client().get(&fetch_url).send()).await?;
|
||||||
|
|
||||||
|
let res: WebfingerResponse = response
|
||||||
|
.json()
|
||||||
|
.await
|
||||||
|
.map_err(|e| RecvError(e.to_string()))?;
|
||||||
|
|
||||||
|
let links: Vec<Url> = res
|
||||||
|
.links
|
||||||
|
.iter()
|
||||||
|
.filter(|l| l.type_.eq(&Some("application/activity+json".to_string())))
|
||||||
|
.map(|l| l.href.clone())
|
||||||
|
.flatten()
|
||||||
|
.collect();
|
||||||
|
for l in links {
|
||||||
|
let object = ObjectId::<Kind>::new(l)
|
||||||
|
.dereference(context, request_counter)
|
||||||
|
.await;
|
||||||
|
if object.is_ok() {
|
||||||
|
return object.map(|o| o.actor_id().into());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(anyhow!("Failed to resolve actor for {}", identifier).into())
|
||||||
|
}
|
@ -1,68 +0,0 @@
|
|||||||
use anyhow::anyhow;
|
|
||||||
use lemmy_utils::{
|
|
||||||
request::{retry, RecvError},
|
|
||||||
LemmyError,
|
|
||||||
};
|
|
||||||
use log::debug;
|
|
||||||
use reqwest::Client;
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use url::Url;
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
pub struct WebfingerLink {
|
|
||||||
pub rel: Option<String>,
|
|
||||||
#[serde(rename(serialize = "type", deserialize = "type"))]
|
|
||||||
pub type_: Option<String>,
|
|
||||||
pub href: Option<Url>,
|
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
|
||||||
pub template: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
pub struct WebfingerResponse {
|
|
||||||
pub subject: String,
|
|
||||||
pub aliases: Vec<Url>,
|
|
||||||
pub links: Vec<WebfingerLink>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum WebfingerType {
|
|
||||||
Person,
|
|
||||||
Group,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
|
|
||||||
/// using webfinger.
|
|
||||||
pub async fn webfinger_resolve_actor(
|
|
||||||
name: &str,
|
|
||||||
domain: &str,
|
|
||||||
webfinger_type: WebfingerType,
|
|
||||||
client: &Client,
|
|
||||||
protocol_string: &str,
|
|
||||||
) -> Result<Url, LemmyError> {
|
|
||||||
let webfinger_type = match webfinger_type {
|
|
||||||
WebfingerType::Person => "acct",
|
|
||||||
WebfingerType::Group => "group",
|
|
||||||
};
|
|
||||||
let fetch_url = format!(
|
|
||||||
"{}://{}/.well-known/webfinger?resource={}:{}@{}",
|
|
||||||
protocol_string, domain, webfinger_type, name, domain
|
|
||||||
);
|
|
||||||
debug!("Fetching webfinger url: {}", &fetch_url);
|
|
||||||
|
|
||||||
let response = retry(|| client.get(&fetch_url).send()).await?;
|
|
||||||
|
|
||||||
let res: WebfingerResponse = response
|
|
||||||
.json()
|
|
||||||
.await
|
|
||||||
.map_err(|e| RecvError(e.to_string()))?;
|
|
||||||
|
|
||||||
let link = res
|
|
||||||
.links
|
|
||||||
.iter()
|
|
||||||
.find(|l| l.type_.eq(&Some("application/activity+json".to_string())))
|
|
||||||
.ok_or_else(|| anyhow!("No application/activity+json link found."))?;
|
|
||||||
link
|
|
||||||
.href
|
|
||||||
.to_owned()
|
|
||||||
.ok_or_else(|| anyhow!("No href found.").into())
|
|
||||||
}
|
|
Loading…
Reference in New Issue