lemmy/server/src/routes/webfinger.rs

97 lines
2.8 KiB
Rust
Raw Normal View History

2019-12-18 00:59:47 +00:00
use crate::db::community::Community;
use crate::db::establish_connection;
use crate::Settings;
use actix_web::body::Body;
use actix_web::web;
2019-12-18 00:59:47 +00:00
use actix_web::web::Query;
use actix_web::HttpResponse;
2019-12-26 19:48:13 +00:00
use regex::Regex;
2019-12-18 00:59:47 +00:00
use serde::Deserialize;
use serde_json::json;
#[derive(Deserialize)]
pub struct Params {
resource: String,
}
pub fn config(cfg: &mut web::ServiceConfig) {
if Settings::get().federation_enabled {
cfg.route(
".well-known/webfinger",
web::get().to(get_webfinger_response),
);
}
}
2019-12-26 19:48:13 +00:00
lazy_static! {
static ref WEBFINGER_COMMUNITY_REGEX: Regex = Regex::new(&format!(
"^group:([a-z0-9_]{{3, 20}})@{}$",
Settings::get().hostname
))
.unwrap();
}
2019-12-18 00:59:47 +00:00
/// Responds to webfinger requests of the following format. There isn't any real documentation for
/// this, but it described in this blog post:
/// https://mastodon.social/.well-known/webfinger?resource=acct:gargron@mastodon.social
///
/// You can also view the webfinger response that Mastodon sends:
/// https://radical.town/.well-known/webfinger?resource=acct:felix@radical.town
fn get_webfinger_response(info: Query<Params>) -> HttpResponse<Body> {
2019-12-26 19:48:13 +00:00
let regex_parsed = WEBFINGER_COMMUNITY_REGEX
.captures(&info.resource)
.map(|c| c.get(1));
// TODO: replace this with .flatten() once we are running rust 1.40
let regex_parsed_flattened = match regex_parsed {
Some(s) => s,
None => None,
};
let community_name = match regex_parsed_flattened {
Some(c) => c.as_str(),
None => return HttpResponse::NotFound().finish(),
};
2019-12-18 00:59:47 +00:00
// Make sure the requested community exists.
let conn = establish_connection();
2019-12-26 19:48:13 +00:00
let community = match Community::read_from_name(&conn, community_name.to_string()) {
Ok(o) => o,
2019-12-18 00:59:47 +00:00
Err(_) => return HttpResponse::NotFound().finish(),
};
// TODO: might want to move this into community or apub
// TODO: should use http during local testing, and https during production
let community_url = format!(
"http://{}/federation/c/{}",
Settings::get().hostname,
community.name
);
2019-12-18 00:59:47 +00:00
let json = json!({
"subject": info.resource,
"aliases": [
community_url,
],
"links": [
{
"rel": "http://webfinger.net/rel/profile-page",
"type": "text/html",
"href": community.get_url(),
2019-12-18 00:59:47 +00:00
},
{
"rel": "self",
"type": "application/activity+json",
2019-12-26 19:48:13 +00:00
// Yes this is correct, this link doesn't include the `.json` extension
"href": community_url
2019-12-18 00:59:47 +00:00
}
// TODO: this also needs to return the subscribe link once that's implemented
//{
// "rel": "http://ostatus.org/schema/1.0/subscribe",
// "template": "https://my_instance.com/authorize_interaction?uri={uri}"
//}
]
});
2019-12-26 19:48:13 +00:00
HttpResponse::Ok()
2019-12-18 00:59:47 +00:00
.content_type("application/activity+json")
2019-12-26 19:48:13 +00:00
.body(json.to_string())
2019-12-18 00:59:47 +00:00
}