2024-11-15 17:12:13 +00:00
|
|
|
use axum::http::{HeaderMap, Response, StatusCode};
|
2024-11-14 03:02:40 +00:00
|
|
|
use axum_extra::extract::Query;
|
|
|
|
use serde::Deserialize;
|
2024-11-15 05:50:51 +00:00
|
|
|
use acrate_core::diesel_async::{AsyncConnection, AsyncPgConnection};
|
|
|
|
use acrate_hostmeta::jrd::ResourceDescriptorLinkJRD;
|
|
|
|
use acrate_hostmeta::xrd::{ResourceDescriptorLinkXRD, ResourceDescriptorPropertyXRD};
|
|
|
|
use crate::config;
|
2024-11-14 03:02:40 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
|
|
pub struct WebfingerQuery {
|
2024-11-15 17:12:13 +00:00
|
|
|
pub resource: Option<String>,
|
2024-11-14 03:02:40 +00:00
|
|
|
|
|
|
|
#[serde(default)]
|
|
|
|
pub rel: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2024-11-15 05:50:51 +00:00
|
|
|
const WEBFINGER_DOC: &str = "/.well-known/webfinger";
|
|
|
|
|
2024-11-14 03:16:12 +00:00
|
|
|
#[axum::debug_handler]
|
2024-11-14 03:02:40 +00:00
|
|
|
pub async fn webfinger_handler(
|
2024-11-15 05:50:51 +00:00
|
|
|
Query(WebfingerQuery {resource, rel}): Query<WebfingerQuery>,
|
|
|
|
headers: HeaderMap,
|
2024-11-15 17:12:13 +00:00
|
|
|
) -> Result<Response<String>, StatusCode> {
|
2024-11-14 03:16:12 +00:00
|
|
|
log::info!("Handling a WebFinger request!");
|
2024-11-15 05:50:51 +00:00
|
|
|
|
2024-11-15 17:12:13 +00:00
|
|
|
let resource = resource.unwrap_or_else(|| "".to_string());
|
2024-11-14 03:16:12 +00:00
|
|
|
log::debug!("Resource is: {resource:#?}");
|
2024-11-15 05:50:51 +00:00
|
|
|
|
2024-11-14 03:16:12 +00:00
|
|
|
log::debug!("Rel is: {rel:#?}");
|
|
|
|
|
2024-11-15 05:50:51 +00:00
|
|
|
let accept = headers.get("Accept")
|
|
|
|
.map(|v| v.to_str())
|
|
|
|
.filter(Result::is_ok)
|
|
|
|
.map(|v| v.unwrap())
|
|
|
|
.unwrap_or("application/json")
|
|
|
|
.to_string();
|
|
|
|
log::debug!("Accept is: {accept:#?}");
|
|
|
|
|
2024-11-15 17:12:13 +00:00
|
|
|
let mut response = Response::new("".to_string());
|
2024-11-15 05:50:51 +00:00
|
|
|
|
|
|
|
let mut conn = AsyncPgConnection::establish(config::ACRATE_WEBFINGER_DATABASE_URL())
|
|
|
|
.await
|
|
|
|
.map_err(|_| StatusCode::BAD_GATEWAY)?;
|
|
|
|
|
|
|
|
let aliases = acrate_core::meta::MetaAlias::query_matching(&mut conn, WEBFINGER_DOC, &resource)
|
|
|
|
.await
|
|
|
|
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
|
|
|
|
|
|
let properties = acrate_core::meta::MetaProperty::query_matching(&mut conn, WEBFINGER_DOC, &resource)
|
|
|
|
.await
|
|
|
|
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
|
|
|
|
|
|
let links = acrate_core::meta::MetaLink::query_matching(&mut conn, WEBFINGER_DOC, &resource)
|
|
|
|
.await
|
|
|
|
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
|
|
|
2024-11-15 17:12:13 +00:00
|
|
|
for mime in accept.split(",") {
|
|
|
|
{
|
|
|
|
let headers = response.headers_mut();
|
|
|
|
headers.insert("Content-Type", mime.parse().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
let (mime, _params) = match mime.trim().split_once(";") {
|
|
|
|
Some((mime, params)) => (mime, Some(params)),
|
|
|
|
None => (mime, None),
|
|
|
|
};
|
|
|
|
|
2024-11-15 05:50:51 +00:00
|
|
|
match mime {
|
2024-11-15 17:12:13 +00:00
|
|
|
"*/*" | "application/json" | "application/jrd+json" => {
|
2024-11-15 05:50:51 +00:00
|
|
|
let subject = Some(resource);
|
|
|
|
|
|
|
|
let aliases = aliases
|
|
|
|
.into_iter()
|
|
|
|
.map(|alias| alias.alias)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let properties = properties
|
|
|
|
.into_iter()
|
|
|
|
.map(|prop| (prop.rel, prop.value))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let links = links
|
|
|
|
.into_iter()
|
|
|
|
.map(|link| ResourceDescriptorLinkJRD {
|
|
|
|
rel: link.rel,
|
|
|
|
r#type: link.r#type,
|
|
|
|
href: link.href,
|
|
|
|
titles: Default::default(), // TODO: Titles
|
|
|
|
properties: Default::default(), // TODO: Link properties
|
|
|
|
template: None, // TODO: Template
|
|
|
|
})
|
|
|
|
.collect::<Vec<ResourceDescriptorLinkJRD>>();
|
|
|
|
|
|
|
|
let rd = acrate_hostmeta::jrd::ResourceDescriptorJRD {
|
|
|
|
subject,
|
|
|
|
aliases,
|
|
|
|
properties,
|
|
|
|
links,
|
|
|
|
};
|
|
|
|
|
2024-11-15 17:12:13 +00:00
|
|
|
let json = serde_json::to_string_pretty(&rd)
|
|
|
|
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
2024-11-15 05:50:51 +00:00
|
|
|
|
2024-11-15 17:12:13 +00:00
|
|
|
{
|
|
|
|
let body = response.body_mut();
|
|
|
|
body.push_str(&json);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(response)
|
2024-11-15 05:50:51 +00:00
|
|
|
},
|
|
|
|
"application/xml" | "application/xrd+xml" => {
|
|
|
|
let subject = Some(resource);
|
|
|
|
|
|
|
|
let aliases = aliases
|
|
|
|
.into_iter()
|
|
|
|
.map(|alias| alias.alias)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let properties = properties
|
|
|
|
.into_iter()
|
|
|
|
.map(|prop| ResourceDescriptorPropertyXRD {
|
|
|
|
r#type: prop.rel, // TODO: Ah si chiama type?
|
|
|
|
value: prop.value,
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let links = links
|
|
|
|
.into_iter()
|
|
|
|
.map(|link| ResourceDescriptorLinkXRD {
|
|
|
|
rel: link.rel,
|
|
|
|
r#type: link.r#type,
|
|
|
|
href: link.href,
|
|
|
|
titles: Default::default(), // TODO: Titles
|
|
|
|
properties: Default::default(), // TODO: Link properties
|
|
|
|
template: None, // TODO: Template
|
|
|
|
})
|
|
|
|
.collect::<Vec<ResourceDescriptorLinkXRD>>();
|
|
|
|
|
|
|
|
let rd = acrate_hostmeta::xrd::ResourceDescriptorXRD {
|
|
|
|
subject,
|
|
|
|
aliases,
|
|
|
|
properties,
|
|
|
|
links,
|
|
|
|
};
|
|
|
|
|
2024-11-15 17:12:13 +00:00
|
|
|
let xml = quick_xml::se::to_string(&rd)
|
|
|
|
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
|
|
|
|
|
|
{
|
|
|
|
let body = response.body_mut();
|
|
|
|
body.push_str("<?xml version='1.0' encoding='UTF-8'?>");
|
|
|
|
body.push_str(&xml);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(response)
|
2024-11-15 05:50:51 +00:00
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(StatusCode::NOT_ACCEPTABLE)
|
2024-11-14 02:50:59 +00:00
|
|
|
}
|