2023-04-21 23:39:52 +00:00
|
|
|
use axum::extract::{Query, State};
|
2023-02-18 14:28:09 +00:00
|
|
|
use axum::http::StatusCode;
|
2023-04-21 23:39:52 +00:00
|
|
|
use axum::response::IntoResponse;
|
2023-02-18 14:28:09 +00:00
|
|
|
use axum::Json;
|
2023-04-21 23:39:52 +00:00
|
|
|
use hyper::header;
|
|
|
|
use magnetar_calckey_model::CalckeyModel;
|
2023-07-07 19:22:30 +00:00
|
|
|
use magnetar_common::config::MagnetarConfig;
|
|
|
|
use magnetar_common::util::{lenient_parse_acct_decode, FediverseTag};
|
2023-02-26 02:51:59 +00:00
|
|
|
use magnetar_core::web_model::acct::Acct;
|
2023-04-21 23:39:52 +00:00
|
|
|
use magnetar_core::web_model::content_type::{ContentActivityStreams, ContentHtml, ContentJrdJson};
|
2023-02-18 14:28:09 +00:00
|
|
|
use magnetar_core::web_model::rel::{RelOStatusSubscribe, RelSelf, RelWebFingerProfilePage};
|
2023-02-26 02:51:59 +00:00
|
|
|
use magnetar_webfinger::webfinger::{WebFinger, WebFingerRel, WebFingerSubject};
|
2023-02-18 14:28:09 +00:00
|
|
|
use serde::Deserialize;
|
2023-04-21 23:39:52 +00:00
|
|
|
use tracing::error;
|
2023-02-18 14:28:09 +00:00
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct WebFingerQuery {
|
|
|
|
resource: WebFingerSubject,
|
|
|
|
rel: Option<Vec<String>>,
|
|
|
|
}
|
|
|
|
|
2023-02-26 02:31:39 +00:00
|
|
|
// TODO: Filter by rel
|
2023-02-18 14:28:09 +00:00
|
|
|
pub async fn handle_webfinger(
|
|
|
|
Query(WebFingerQuery { resource, rel, .. }): Query<WebFingerQuery>,
|
2023-04-21 23:39:52 +00:00
|
|
|
State((config, ck)): State<(&'static MagnetarConfig, CalckeyModel)>,
|
|
|
|
) -> Result<impl IntoResponse, StatusCode> {
|
2023-02-18 14:28:09 +00:00
|
|
|
let resource = match resource {
|
|
|
|
acct @ WebFingerSubject::Acct(_) => acct,
|
|
|
|
// Leniently re-add the acct
|
2023-04-21 23:39:52 +00:00
|
|
|
WebFingerSubject::Url(url) if !url.starts_with("http:") && !url.starts_with("https:") => {
|
2023-02-18 14:28:09 +00:00
|
|
|
WebFingerSubject::Acct(Acct::new(url.into()))
|
|
|
|
}
|
|
|
|
other => other,
|
|
|
|
};
|
|
|
|
|
2023-04-21 23:39:52 +00:00
|
|
|
let user = match resource {
|
|
|
|
WebFingerSubject::Acct(acct) => {
|
|
|
|
let tag = lenient_parse_acct_decode(&acct).map_err(|e| {
|
|
|
|
error!("Failed to parse tag: {e}");
|
|
|
|
StatusCode::UNPROCESSABLE_ENTITY
|
|
|
|
})?;
|
|
|
|
|
2023-04-22 00:35:12 +00:00
|
|
|
ck.get_user_by_tag(
|
|
|
|
&tag.name,
|
|
|
|
tag.host
|
|
|
|
.filter(|host| *host != config.networking.host)
|
|
|
|
.as_deref(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.map_err(|e| {
|
|
|
|
error!("Data error: {e}");
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR
|
|
|
|
})?
|
2023-04-21 23:39:52 +00:00
|
|
|
}
|
2023-06-29 20:24:26 +00:00
|
|
|
// TODO: Make this work for local users
|
2023-04-21 23:39:52 +00:00
|
|
|
WebFingerSubject::Url(url) => ck.get_user_by_uri(&url).await.map_err(|e| {
|
|
|
|
error!("Data error: {e}");
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR
|
|
|
|
})?,
|
|
|
|
};
|
|
|
|
|
|
|
|
if user.is_none() {
|
|
|
|
return Err(StatusCode::NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
let user = user.unwrap();
|
2023-04-22 00:35:12 +00:00
|
|
|
let tag = FediverseTag::from((
|
|
|
|
&user.username,
|
|
|
|
user.host.as_ref().or(Some(&config.networking.host)),
|
|
|
|
));
|
2023-04-21 23:39:52 +00:00
|
|
|
|
|
|
|
let mut links = Vec::new();
|
|
|
|
let mut aliases = Vec::new();
|
|
|
|
|
|
|
|
match tag.host {
|
2023-04-22 00:53:37 +00:00
|
|
|
Some(ref host) if host != &config.networking.host => {
|
|
|
|
if let Some(uri) = user.uri {
|
|
|
|
links.push(WebFingerRel::RelSelf {
|
|
|
|
rel: RelSelf,
|
|
|
|
content_type: ContentActivityStreams,
|
|
|
|
href: uri,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-04-21 23:39:52 +00:00
|
|
|
_ => {
|
|
|
|
links.push(WebFingerRel::RelOStatusSubscribe {
|
|
|
|
rel: RelOStatusSubscribe,
|
|
|
|
template: format!(
|
|
|
|
"{}://{}/authorize-follow?acct={{uri}}",
|
|
|
|
config.networking.protocol, config.networking.host
|
|
|
|
),
|
|
|
|
});
|
|
|
|
|
|
|
|
let user_url = format!(
|
|
|
|
"{}://{}/@{}",
|
|
|
|
config.networking.protocol, config.networking.host, tag.name
|
|
|
|
);
|
|
|
|
|
|
|
|
links.push(WebFingerRel::RelWebFingerProfilePage {
|
2023-02-18 14:28:09 +00:00
|
|
|
rel: RelWebFingerProfilePage,
|
|
|
|
content_type: ContentHtml,
|
2023-04-21 23:39:52 +00:00
|
|
|
href: user_url.clone(),
|
|
|
|
});
|
|
|
|
|
|
|
|
aliases.push(WebFingerSubject::Url(user_url));
|
|
|
|
|
2023-04-22 00:53:37 +00:00
|
|
|
links.push(WebFingerRel::RelSelf {
|
|
|
|
rel: RelSelf,
|
|
|
|
content_type: ContentActivityStreams,
|
|
|
|
href: format!(
|
|
|
|
"{}://{}/users/{}",
|
|
|
|
config.networking.protocol, config.networking.host, user.id
|
|
|
|
),
|
|
|
|
});
|
|
|
|
}
|
2023-04-21 23:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
[(header::CONTENT_TYPE, ContentJrdJson.as_ref())],
|
|
|
|
Json(WebFinger {
|
|
|
|
subject: WebFingerSubject::Acct(tag.into()),
|
|
|
|
aliases: Some(aliases),
|
|
|
|
links,
|
|
|
|
}),
|
|
|
|
))
|
2023-02-18 14:28:09 +00:00
|
|
|
}
|