2021-04-09 00:22:17 +00:00
|
|
|
use actix_web::{get, web, HttpResponse};
|
|
|
|
use regex::Regex;
|
2021-11-10 14:32:27 +00:00
|
|
|
use tokio_postgres::GenericClient;
|
2021-04-09 00:22:17 +00:00
|
|
|
|
2022-10-03 22:42:53 +00:00
|
|
|
use crate::activitypub::actors::types::{ActorAddress, ACTOR_ADDRESS_RE};
|
2022-10-01 16:56:57 +00:00
|
|
|
use crate::activitypub::constants::AP_MEDIA_TYPE;
|
2022-07-15 17:31:02 +00:00
|
|
|
use crate::activitypub::identifiers::{
|
|
|
|
local_actor_id,
|
|
|
|
local_instance_actor_id,
|
|
|
|
};
|
2021-11-10 14:32:27 +00:00
|
|
|
use crate::config::{Config, Instance};
|
2021-04-09 00:22:17 +00:00
|
|
|
use crate::database::{Pool, get_database_client};
|
2021-11-13 17:37:31 +00:00
|
|
|
use crate::errors::{HttpError, ValidationError};
|
2021-04-09 00:22:17 +00:00
|
|
|
use crate::models::users::queries::is_registered_user;
|
|
|
|
use super::types::{
|
|
|
|
JRD_CONTENT_TYPE,
|
|
|
|
WebfingerQueryParams,
|
|
|
|
Link,
|
|
|
|
JsonResourceDescriptor,
|
|
|
|
};
|
|
|
|
|
2022-10-03 22:42:53 +00:00
|
|
|
// https://datatracker.ietf.org/doc/html/rfc7565#section-7
|
|
|
|
fn parse_acct_uri(uri: &str) -> Result<ActorAddress, ValidationError> {
|
|
|
|
let uri_regexp = Regex::new(&format!("acct:{}", ACTOR_ADDRESS_RE)).unwrap();
|
|
|
|
let uri_caps = uri_regexp.captures(uri)
|
|
|
|
.ok_or(ValidationError("invalid query target"))?;
|
|
|
|
let actor_address = ActorAddress {
|
|
|
|
username: uri_caps["username"].to_string(),
|
|
|
|
instance: uri_caps["instance"].to_string(),
|
|
|
|
};
|
|
|
|
Ok(actor_address)
|
|
|
|
}
|
|
|
|
|
2021-11-07 08:52:57 +00:00
|
|
|
async fn get_user_info(
|
2021-11-10 14:32:27 +00:00
|
|
|
db_client: &impl GenericClient,
|
|
|
|
instance: Instance,
|
2021-04-09 00:22:17 +00:00
|
|
|
query_params: WebfingerQueryParams,
|
|
|
|
) -> Result<JsonResourceDescriptor, HttpError> {
|
2022-10-03 22:42:53 +00:00
|
|
|
let actor_address = parse_acct_uri(&query_params.resource)?;
|
|
|
|
if !actor_address.is_local(&instance.host()) {
|
2021-11-10 14:32:27 +00:00
|
|
|
// Wrong instance
|
2021-04-09 00:22:17 +00:00
|
|
|
return Err(HttpError::NotFoundError("user"));
|
2022-10-03 22:42:53 +00:00
|
|
|
};
|
|
|
|
let actor_url = if actor_address.username == instance.host() {
|
2022-07-15 17:31:02 +00:00
|
|
|
local_instance_actor_id(&instance.url())
|
2021-11-18 00:51:56 +00:00
|
|
|
} else {
|
2022-10-03 22:42:53 +00:00
|
|
|
if !is_registered_user(db_client, &actor_address.username).await? {
|
2021-11-18 00:51:56 +00:00
|
|
|
return Err(HttpError::NotFoundError("user"));
|
|
|
|
};
|
2022-10-03 22:42:53 +00:00
|
|
|
local_actor_id(&instance.url(), &actor_address.username)
|
2021-11-18 00:51:56 +00:00
|
|
|
};
|
2021-04-09 00:22:17 +00:00
|
|
|
let link = Link {
|
|
|
|
rel: "self".to_string(),
|
2022-10-01 16:56:57 +00:00
|
|
|
link_type: Some(AP_MEDIA_TYPE.to_string()),
|
2021-04-09 00:22:17 +00:00
|
|
|
href: Some(actor_url),
|
|
|
|
};
|
|
|
|
let jrd = JsonResourceDescriptor {
|
|
|
|
subject: query_params.resource,
|
|
|
|
links: vec![link],
|
|
|
|
};
|
|
|
|
Ok(jrd)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/.well-known/webfinger")]
|
2021-11-07 08:52:57 +00:00
|
|
|
pub async fn get_descriptor(
|
2021-04-09 00:22:17 +00:00
|
|
|
config: web::Data<Config>,
|
2021-11-10 14:32:27 +00:00
|
|
|
db_pool: web::Data<Pool>,
|
2021-04-09 00:22:17 +00:00
|
|
|
query_params: web::Query<WebfingerQueryParams>,
|
|
|
|
) -> Result<HttpResponse, HttpError> {
|
2021-11-10 14:32:27 +00:00
|
|
|
let db_client = &**get_database_client(&db_pool).await?;
|
|
|
|
let jrd = get_user_info(
|
|
|
|
db_client,
|
|
|
|
config.instance(),
|
|
|
|
query_params.into_inner(),
|
|
|
|
).await?;
|
2021-04-09 00:22:17 +00:00
|
|
|
let response = HttpResponse::Ok()
|
|
|
|
.content_type(JRD_CONTENT_TYPE)
|
|
|
|
.json(jrd);
|
|
|
|
Ok(response)
|
|
|
|
}
|
2022-10-03 22:42:53 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_acct_uri() {
|
|
|
|
let uri = "acct:user_1@example.com";
|
|
|
|
let actor_address = parse_acct_uri(uri).unwrap();
|
|
|
|
assert_eq!(actor_address.username, "user_1");
|
|
|
|
assert_eq!(actor_address.instance, "example.com");
|
|
|
|
}
|
|
|
|
}
|