2020-05-17 01:09:26 +00:00
|
|
|
use crate::{
|
|
|
|
db::{community::Community, user::User_},
|
|
|
|
routes::DbPoolParam,
|
|
|
|
Settings,
|
|
|
|
};
|
2020-05-16 14:04:08 +00:00
|
|
|
use actix_web::{error::ErrorBadRequest, web::Query, *};
|
|
|
|
use regex::Regex;
|
2020-05-17 01:09:26 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2019-12-18 00:59:47 +00:00
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct Params {
|
|
|
|
resource: String,
|
|
|
|
}
|
|
|
|
|
2020-05-15 16:36:11 +00:00
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub struct WebFingerResponse {
|
|
|
|
pub subject: String,
|
|
|
|
pub aliases: Vec<String>,
|
|
|
|
pub links: Vec<WebFingerLink>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub struct WebFingerLink {
|
|
|
|
pub rel: Option<String>,
|
|
|
|
#[serde(rename(serialize = "type", deserialize = "type"))]
|
|
|
|
pub type_: Option<String>,
|
|
|
|
pub href: Option<String>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub template: Option<String>,
|
|
|
|
}
|
|
|
|
|
2019-12-31 12:55:33 +00:00
|
|
|
pub fn config(cfg: &mut web::ServiceConfig) {
|
2020-03-18 21:09:00 +00:00
|
|
|
if Settings::get().federation.enabled {
|
2019-12-31 12:55:33 +00:00
|
|
|
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();
|
2020-05-15 16:36:11 +00:00
|
|
|
static ref WEBFINGER_USER_REGEX: Regex = Regex::new(&format!(
|
|
|
|
"^acct:([a-z0-9_]{{3, 20}})@{}$",
|
|
|
|
Settings::get().hostname
|
|
|
|
))
|
|
|
|
.unwrap();
|
2019-12-26 19:48:13 +00:00
|
|
|
}
|
|
|
|
|
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
|
2020-01-12 15:31:51 +00:00
|
|
|
async fn get_webfinger_response(
|
|
|
|
info: Query<Params>,
|
2020-04-24 14:04:36 +00:00
|
|
|
db: DbPoolParam,
|
2020-04-21 20:40:03 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2020-01-12 15:31:51 +00:00
|
|
|
let res = web::block(move || {
|
|
|
|
let conn = db.get()?;
|
2019-12-18 00:59:47 +00:00
|
|
|
|
2020-05-15 16:36:11 +00:00
|
|
|
let community_regex_parsed = WEBFINGER_COMMUNITY_REGEX
|
|
|
|
.captures(&info.resource)
|
|
|
|
.map(|c| c.get(1))
|
|
|
|
.flatten();
|
|
|
|
|
|
|
|
let user_regex_parsed = WEBFINGER_USER_REGEX
|
2020-01-12 15:31:51 +00:00
|
|
|
.captures(&info.resource)
|
2020-03-19 18:01:01 +00:00
|
|
|
.map(|c| c.get(1))
|
|
|
|
.flatten();
|
2019-12-18 00:59:47 +00:00
|
|
|
|
2020-05-15 16:36:11 +00:00
|
|
|
let url = if let Some(community_name) = community_regex_parsed {
|
|
|
|
// Make sure the requested community exists.
|
|
|
|
let community = match Community::read_from_name(&conn, &community_name.as_str()) {
|
|
|
|
Ok(o) => o,
|
|
|
|
Err(_) => return Err(format_err!("not_found")),
|
|
|
|
};
|
|
|
|
community.actor_id
|
|
|
|
} else if let Some(user_name) = user_regex_parsed {
|
|
|
|
// Make sure the requested user exists.
|
|
|
|
let user = match User_::read_from_name(&conn, &user_name.as_str()) {
|
|
|
|
Ok(o) => o,
|
|
|
|
Err(_) => return Err(format_err!("not_found")),
|
|
|
|
};
|
|
|
|
user.actor_id
|
|
|
|
} else {
|
|
|
|
return Err(format_err!("not_found"));
|
2020-01-12 15:31:51 +00:00
|
|
|
};
|
2019-12-18 00:59:47 +00:00
|
|
|
|
2020-05-15 16:36:11 +00:00
|
|
|
let wf_res = WebFingerResponse {
|
|
|
|
subject: info.resource.to_owned(),
|
|
|
|
aliases: vec![url.to_owned()],
|
|
|
|
links: vec![
|
|
|
|
WebFingerLink {
|
|
|
|
rel: Some("http://webfinger.net/rel/profile-page".to_string()),
|
|
|
|
type_: Some("text/html".to_string()),
|
|
|
|
href: Some(url.to_owned()),
|
|
|
|
template: None,
|
|
|
|
},
|
|
|
|
WebFingerLink {
|
|
|
|
rel: Some("self".to_string()),
|
|
|
|
type_: Some("application/activity+json".to_string()),
|
|
|
|
href: Some(url),
|
|
|
|
template: None,
|
|
|
|
}, // 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}"
|
|
|
|
//}
|
|
|
|
],
|
|
|
|
};
|
2020-01-12 15:31:51 +00:00
|
|
|
|
2020-05-15 16:36:11 +00:00
|
|
|
Ok(wf_res)
|
2020-01-12 15:31:51 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.map(|json| HttpResponse::Ok().json(json))
|
2020-04-21 20:48:54 +00:00
|
|
|
.map_err(ErrorBadRequest)?;
|
2020-01-12 15:31:51 +00:00
|
|
|
Ok(res)
|
2019-12-18 00:59:47 +00:00
|
|
|
}
|