Always use JSON for Webfinger

This commit is contained in:
Bat 2018-04-24 12:38:43 +01:00
parent 0c9a1bfc3a
commit 270786ad3d
3 changed files with 15 additions and 49 deletions

View File

@ -6,7 +6,7 @@ pub trait Webfinger {
fn webfinger_aliases(&self, conn: &PgConnection) -> Vec<String>; fn webfinger_aliases(&self, conn: &PgConnection) -> Vec<String>;
fn webfinger_links(&self, conn: &PgConnection) -> Vec<Vec<(String, String)>>; fn webfinger_links(&self, conn: &PgConnection) -> Vec<Vec<(String, String)>>;
fn webfinger_json(&self, conn: &PgConnection) -> serde_json::Value { fn webfinger(&self, conn: &PgConnection) -> String {
json!({ json!({
"subject": self.webfinger_subject(conn), "subject": self.webfinger_subject(conn),
"aliases": self.webfinger_aliases(conn), "aliases": self.webfinger_aliases(conn),
@ -17,34 +17,6 @@ pub trait Webfinger {
} }
serde_json::Value::Object(link_obj) serde_json::Value::Object(link_obj)
}).collect::<Vec<serde_json::Value>>() }).collect::<Vec<serde_json::Value>>()
}) }).to_string()
}
fn webfinger_xml(&self, conn: &PgConnection) -> String {
format!(r#"
<?xml version="1.0"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
<Subject>{subject}</Subject>
{aliases}
{links}
</XRD>
"#,
subject = self.webfinger_subject(conn),
aliases = self.webfinger_aliases(conn).into_iter().map(|a| {
format!("<Alias>{a}</Alias>", a = a)
}).collect::<Vec<String>>().join("\n"),
links = self.webfinger_links(conn).into_iter().map(|l| {
format!("<Link {} />", l.into_iter().map(|prop| {
format!("{}=\"{}\"", prop.0, prop.1)
}).collect::<Vec<String>>().join(" "))
}).collect::<Vec<String>>().join("\n")
)
}
fn webfinger(&self, format: &'static str, conn: &PgConnection) -> String {
match format {
"json" => self.webfinger_json(conn).to_string(),
_ => self.webfinger_xml(conn)
}
} }
} }

View File

@ -54,8 +54,7 @@ fn main() {
rocket::ignite() rocket::ignite()
.mount("/", routes![ .mount("/", routes![
routes::well_known::host_meta, routes::well_known::host_meta,
routes::well_known::webfinger_json, routes::well_known::webfinger,
routes::well_known::webfinger_xml,
routes::instance::configure, routes::instance::configure,
routes::instance::post_config, routes::instance::post_config,

View File

@ -1,3 +1,6 @@
use rocket::http::ContentType;
use rocket::response::Content;
use activity_pub::webfinger::Webfinger; use activity_pub::webfinger::Webfinger;
use db_conn::DbConn; use db_conn::DbConn;
use models::blogs::Blog; use models::blogs::Blog;
@ -20,17 +23,8 @@ struct WebfingerQuery {
resource: String resource: String
} }
#[get("/.well-known/webfinger?<query>", format = "application/jrd+json")] #[get("/.well-known/webfinger?<query>")]
fn webfinger_json(query: WebfingerQuery, conn: DbConn) -> Result<String, &'static str> { fn webfinger(query: WebfingerQuery, conn: DbConn) -> Content<Result<String, &'static str>> {
webfinger(query, conn, "json")
}
#[get("/.well-known/webfinger?<query>", format = "application/xrd+xml")]
fn webfinger_xml(query: WebfingerQuery, conn: DbConn) -> Result<String, &'static str> {
webfinger(query, conn, "xml")
}
fn webfinger(query: WebfingerQuery, conn: DbConn, format: &'static str) -> Result<String, &'static str> {
let mut parsed_query = query.resource.split(":"); let mut parsed_query = query.resource.split(":");
println!("{:?}", parsed_query.clone().collect::<Vec<&str>>()); println!("{:?}", parsed_query.clone().collect::<Vec<&str>>());
let res_type = parsed_query.next().unwrap(); let res_type = parsed_query.next().unwrap();
@ -43,17 +37,18 @@ fn webfinger(query: WebfingerQuery, conn: DbConn, format: &'static str) -> Resul
let domain = Instance::get_local(&*conn).unwrap().public_domain; let domain = Instance::get_local(&*conn).unwrap().public_domain;
if res_dom == domain { if res_dom == domain {
match User::find_by_name(&*conn, String::from(user)) { let res = match User::find_by_name(&*conn, String::from(user)) {
Some(usr) => Ok(usr.webfinger(format, &*conn)), Some(usr) => Ok(usr.webfinger(&*conn)),
None => match Blog::find_by_actor_id(&*conn, String::from(user)) { None => match Blog::find_by_actor_id(&*conn, String::from(user)) {
Some(blog) => Ok(blog.webfinger(format, &*conn)), Some(blog) => Ok(blog.webfinger(&*conn)),
None => Err("Requested actor not found") None => Err("Requested actor not found")
} }
};
Content(ContentType::new("application", "jrd+json"), res)
} else {
Content(ContentType::new("text", "plain"), Err("Invalid instance"))
} }
} else { } else {
Err("Invalid instance") Content(ContentType::new("text", "plain"), Err("Invalid resource type. Only acct is supported"))
}
} else {
Err("Invalid resource type. Only acct is supported")
} }
} }