2020-05-16 14:04:08 +00:00
|
|
|
use actix_web::{error::ErrorBadRequest, web::Query, *};
|
2020-08-01 14:04:42 +00:00
|
|
|
use anyhow::anyhow;
|
2021-07-20 07:00:20 +00:00
|
|
|
use lemmy_api_common::blocking;
|
|
|
|
use lemmy_apub_lib::webfinger::{WebfingerLink, WebfingerResponse};
|
2021-03-10 22:33:55 +00:00
|
|
|
use lemmy_db_queries::source::{community::Community_, person::Person_};
|
|
|
|
use lemmy_db_schema::source::{community::Community, person::Person};
|
2020-09-01 14:25:34 +00:00
|
|
|
use lemmy_utils::{
|
2021-03-01 17:24:11 +00:00
|
|
|
settings::structs::Settings,
|
2020-09-01 14:25:34 +00:00
|
|
|
LemmyError,
|
|
|
|
WEBFINGER_COMMUNITY_REGEX,
|
2021-03-11 22:47:44 +00:00
|
|
|
WEBFINGER_USERNAME_REGEX,
|
2020-09-01 14:25:34 +00:00
|
|
|
};
|
2020-09-24 13:53:21 +00:00
|
|
|
use lemmy_websocket::LemmyContext;
|
|
|
|
use serde::Deserialize;
|
2019-12-18 00:59:47 +00:00
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
2020-11-16 15:44:04 +00:00
|
|
|
struct Params {
|
2019-12-18 00:59:47 +00:00
|
|
|
resource: String,
|
|
|
|
}
|
|
|
|
|
2019-12-31 12:55:33 +00:00
|
|
|
pub fn config(cfg: &mut web::ServiceConfig) {
|
2021-08-04 21:13:51 +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-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-08-18 13:43:50 +00:00
|
|
|
context: web::Data<LemmyContext>,
|
2020-04-21 20:40:03 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2020-07-01 12:54:29 +00:00
|
|
|
let community_regex_parsed = WEBFINGER_COMMUNITY_REGEX
|
|
|
|
.captures(&info.resource)
|
|
|
|
.map(|c| c.get(1))
|
|
|
|
.flatten();
|
2019-12-18 00:59:47 +00:00
|
|
|
|
2021-03-11 22:47:44 +00:00
|
|
|
let username_regex_parsed = WEBFINGER_USERNAME_REGEX
|
2020-07-01 12:54:29 +00:00
|
|
|
.captures(&info.resource)
|
|
|
|
.map(|c| c.get(1))
|
|
|
|
.flatten();
|
2020-05-15 16:36:11 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let url = if let Some(community_name) = community_regex_parsed {
|
|
|
|
let community_name = community_name.as_str().to_owned();
|
|
|
|
// Make sure the requested community exists.
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
2020-07-01 12:54:29 +00:00
|
|
|
Community::read_from_name(conn, &community_name)
|
|
|
|
})
|
|
|
|
.await?
|
2020-08-01 14:04:42 +00:00
|
|
|
.map_err(|_| ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))?
|
2020-07-01 12:54:29 +00:00
|
|
|
.actor_id
|
2021-03-11 22:47:44 +00:00
|
|
|
} else if let Some(person_name) = username_regex_parsed {
|
2021-03-10 22:33:55 +00:00
|
|
|
let person_name = person_name.as_str().to_owned();
|
2021-03-11 22:47:44 +00:00
|
|
|
// Make sure the requested person exists.
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
Person::find_by_name(conn, &person_name)
|
2020-08-18 13:43:50 +00:00
|
|
|
})
|
|
|
|
.await?
|
|
|
|
.map_err(|_| ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))?
|
|
|
|
.actor_id
|
2020-07-01 12:54:29 +00:00
|
|
|
} else {
|
2020-08-01 14:04:42 +00:00
|
|
|
return Err(ErrorBadRequest(LemmyError::from(anyhow!("not_found"))));
|
2020-07-01 12:54:29 +00:00
|
|
|
};
|
2019-12-18 00:59:47 +00:00
|
|
|
|
2021-07-20 07:00:20 +00:00
|
|
|
let json = WebfingerResponse {
|
2020-07-01 12:54:29 +00:00
|
|
|
subject: info.resource.to_owned(),
|
2021-01-27 16:42:23 +00:00
|
|
|
aliases: vec![url.to_owned().into()],
|
2020-07-01 12:54:29 +00:00
|
|
|
links: vec![
|
2021-07-20 07:00:20 +00:00
|
|
|
WebfingerLink {
|
2020-07-01 12:54:29 +00:00
|
|
|
rel: Some("http://webfinger.net/rel/profile-page".to_string()),
|
|
|
|
type_: Some("text/html".to_string()),
|
2021-01-27 16:42:23 +00:00
|
|
|
href: Some(url.to_owned().into()),
|
2020-07-01 12:54:29 +00:00
|
|
|
template: None,
|
|
|
|
},
|
2021-07-20 07:00:20 +00:00
|
|
|
WebfingerLink {
|
2020-07-01 12:54:29 +00:00
|
|
|
rel: Some("self".to_string()),
|
|
|
|
type_: Some("application/activity+json".to_string()),
|
2021-01-27 16:42:23 +00:00
|
|
|
href: Some(url.into()),
|
2020-07-01 12:54:29 +00:00
|
|
|
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}"
|
|
|
|
//}
|
|
|
|
],
|
|
|
|
};
|
2019-12-18 00:59:47 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
Ok(HttpResponse::Ok().json(json))
|
2019-12-18 00:59:47 +00:00
|
|
|
}
|