Plume/src/routes/well_known.rs

81 lines
2.7 KiB
Rust
Raw Normal View History

2018-04-24 13:38:43 +02:00
use rocket::http::ContentType;
use rocket::response::Content;
2018-06-18 23:50:40 +02:00
use webfinger::*;
2018-04-24 13:38:43 +02:00
2021-01-30 13:44:29 +01:00
use plume_models::{ap_url, blogs::Blog, db_conn::DbConn, users::User, CONFIG};
2018-04-24 10:35:45 +02:00
2018-06-10 21:33:42 +02:00
#[get("/.well-known/nodeinfo")]
pub fn nodeinfo() -> Content<String> {
2019-03-20 17:56:17 +01:00
Content(
ContentType::new("application", "jrd+json"),
json!({
"links": [
{
"rel": "http://nodeinfo.diaspora.software/ns/schema/2.0",
"href": ap_url(&format!("{domain}/nodeinfo/2.0", domain = CONFIG.base_url.as_str()))
2019-03-20 17:56:17 +01:00
},
{
"rel": "http://nodeinfo.diaspora.software/ns/schema/2.1",
"href": ap_url(&format!("{domain}/nodeinfo/2.1", domain = CONFIG.base_url.as_str()))
2019-03-20 17:56:17 +01:00
}
]
})
.to_string(),
)
2018-06-10 21:33:42 +02:00
}
#[get("/.well-known/host-meta")]
pub fn host_meta() -> String {
2019-03-20 17:56:17 +01:00
format!(
r#"
2018-04-24 10:35:45 +02:00
<?xml version="1.0"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
<Link rel="lrdd" type="application/xrd+xml" template="{url}"/>
2018-04-24 10:35:45 +02:00
</XRD>
2019-03-20 17:56:17 +01:00
"#,
url = ap_url(&format!(
"{domain}/.well-known/webfinger?resource={{uri}}",
domain = CONFIG.base_url.as_str()
2019-03-20 17:56:17 +01:00
))
)
2018-04-24 10:35:45 +02:00
}
2018-06-18 23:50:40 +02:00
struct WebfingerResolver;
2021-01-30 13:44:29 +01:00
impl Resolver<DbConn> for WebfingerResolver {
2018-06-18 23:50:40 +02:00
fn instance_domain<'a>() -> &'a str {
CONFIG.base_url.as_str()
2018-06-18 23:50:40 +02:00
}
2021-01-30 13:44:29 +01:00
fn find(prefix: Prefix, acct: String, conn: DbConn) -> Result<Webfinger, ResolverError> {
match prefix {
2021-01-30 13:44:29 +01:00
Prefix::Acct => User::find_by_fqn(&conn, &acct)
.and_then(|usr| usr.webfinger(&*conn))
.or(Err(ResolverError::NotFound)),
2021-01-30 13:44:29 +01:00
Prefix::Group => Blog::find_by_fqn(&conn, &acct)
.and_then(|blog| blog.webfinger(&*conn))
.or(Err(ResolverError::NotFound)),
Prefix::Custom(_) => Err(ResolverError::NotFound),
}
2018-06-18 23:50:40 +02:00
}
}
#[get("/.well-known/webfinger?<resource>")]
2021-01-30 13:44:29 +01:00
pub fn webfinger(resource: String, conn: DbConn) -> Content<String> {
match WebfingerResolver::endpoint(resource, conn)
2019-03-20 17:56:17 +01:00
.and_then(|wf| serde_json::to_string(&wf).map_err(|_| ResolverError::NotFound))
{
2018-06-18 23:50:40 +02:00
Ok(wf) => Content(ContentType::new("application", "jrd+json"), wf),
2019-03-20 17:56:17 +01:00
Err(err) => Content(
ContentType::new("text", "plain"),
String::from(match err {
ResolverError::InvalidResource => {
"Invalid resource. Make sure to request an acct: URI"
}
ResolverError::NotFound => "Requested resource was not found",
ResolverError::WrongDomain => "This is not the instance of the requested resource",
2019-03-20 17:56:17 +01:00
}),
),
2018-04-24 10:35:45 +02:00
}
}