From c36b6edbfb2fdbdddf5b9602c6c791e68d3128bb Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 15 Nov 2024 18:12:13 +0100 Subject: [PATCH] `webfinger`: Serve XRD and JRD responses --- acrate-webfinger/Cargo.toml | 2 ++ acrate-webfinger/src/route.rs | 47 ++++++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/acrate-webfinger/Cargo.toml b/acrate-webfinger/Cargo.toml index f74ff6b..5bb8042 100644 --- a/acrate-webfinger/Cargo.toml +++ b/acrate-webfinger/Cargo.toml @@ -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"] } diff --git a/acrate-webfinger/src/route.rs b/acrate-webfinger/src/route.rs index dc98106..a94ec6e 100644 --- a/acrate-webfinger/src/route.rs +++ b/acrate-webfinger/src/route.rs @@ -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, #[serde(default)] pub rel: Vec, @@ -21,9 +20,10 @@ const WEBFINGER_DOC: &str = "/.well-known/webfinger"; pub async fn webfinger_handler( Query(WebfingerQuery {resource, rel}): Query, headers: HeaderMap, -) -> Result<(Body, HeaderMap), StatusCode> { +) -> Result, 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(""); + body.push_str(&xml); + } + + return Ok(response) }, _ => { continue;