1
Fork 0

Create webfinger crate #5

Merged
steffo merged 53 commits from feature/webfinger into main 2024-11-16 06:33:06 +00:00
2 changed files with 37 additions and 12 deletions
Showing only changes of commit c36b6edbfb - Show all commits

View file

@ -12,5 +12,7 @@ axum-extra = { version = "0.9.4", features = ["query"] }
log = "0.4.22"
micronfig = "0.3.0"
pretty_env_logger = "0.5.0"
quick-xml = { version = "0.37.0", features = ["serialize"] }
serde = { version = "1.0.215", features = ["derive"] }
serde_json = "1.0.132"
tokio = { version = "1.41.1", features = ["macros", "net", "rt-multi-thread"] }

View file

@ -1,5 +1,4 @@
use axum::body::Body;
use axum::http::{HeaderMap, HeaderValue, StatusCode};
use axum::http::{HeaderMap, Response, StatusCode};
use axum_extra::extract::Query;
use serde::Deserialize;
use acrate_core::diesel_async::{AsyncConnection, AsyncPgConnection};
@ -9,7 +8,7 @@ use crate::config;
#[derive(Debug, Clone, Deserialize)]
pub struct WebfingerQuery {
pub resource: String,
pub resource: Option<String>,
#[serde(default)]
pub rel: Vec<String>,
@ -21,9 +20,10 @@ const WEBFINGER_DOC: &str = "/.well-known/webfinger";
pub async fn webfinger_handler(
Query(WebfingerQuery {resource, rel}): Query<WebfingerQuery>,
headers: HeaderMap,
) -> Result<(Body, HeaderMap), StatusCode> {
) -> Result<Response<String>, StatusCode> {
log::info!("Handling a WebFinger request!");
let resource = resource.unwrap_or_else(|| "".to_string());
log::debug!("Resource is: {resource:#?}");
log::debug!("Rel is: {rel:#?}");
@ -36,7 +36,7 @@ pub async fn webfinger_handler(
.to_string();
log::debug!("Accept is: {accept:#?}");
let mut response_headers = HeaderMap::new();
let mut response = Response::new("".to_string());
let mut conn = AsyncPgConnection::establish(config::ACRATE_WEBFINGER_DATABASE_URL())
.await
@ -54,11 +54,19 @@ pub async fn webfinger_handler(
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
for mime in accept.split(", ") {
response_headers.insert("Content-Type", mime.parse().unwrap());
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),
};
match mime {
"application/json" | "application/jrd+json" => {
"*/*" | "application/json" | "application/jrd+json" => {
let subject = Some(resource);
let aliases = aliases
@ -90,9 +98,15 @@ pub async fn webfinger_handler(
links,
};
let body = rd.
let json = serde_json::to_string_pretty(&rd)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
return Ok(rd, response_headers)
{
let body = response.body_mut();
body.push_str(&json);
}
return Ok(response)
},
"application/xml" | "application/xrd+xml" => {
let subject = Some(resource);
@ -129,7 +143,16 @@ pub async fn webfinger_handler(
links,
};
return Ok(StatusCode::OK)
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)
},
_ => {
continue;