2023-01-13 05:40:33 +01:00
|
|
|
import Router from "@koa/router";
|
2019-02-05 09:42:55 +01:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
import config from "@/config/index.js";
|
|
|
|
import * as Acct from "@/misc/acct.js";
|
|
|
|
import { links } from "./nodeinfo.js";
|
|
|
|
import { escapeAttribute, escapeValue } from "@/prelude/xml.js";
|
|
|
|
import { Users } from "@/models/index.js";
|
|
|
|
import type { User } from "@/models/entities/user.js";
|
|
|
|
import type { FindOptionsWhere } from "typeorm";
|
|
|
|
import { IsNull } from "typeorm";
|
2019-02-05 09:42:55 +01:00
|
|
|
|
|
|
|
// Init router
|
|
|
|
const router = new Router();
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const XRD = (
|
|
|
|
...x: {
|
|
|
|
element: string;
|
|
|
|
value?: string;
|
|
|
|
attributes?: Record<string, string>;
|
|
|
|
}[]
|
|
|
|
) =>
|
|
|
|
`<?xml version="1.0" encoding="UTF-8"?><XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">${x
|
|
|
|
.map(
|
|
|
|
({ element, value, attributes }) =>
|
|
|
|
`<${Object.entries(
|
|
|
|
(typeof attributes === "object" && attributes) || {},
|
|
|
|
).reduce((a, [k, v]) => `${a} ${k}="${escapeAttribute(v)}"`, element)}${
|
|
|
|
typeof value === "string" ? `>${escapeValue(value)}</${element}` : "/"
|
|
|
|
}>`,
|
|
|
|
)
|
|
|
|
.reduce((a, c) => a + c, "")}</XRD>`;
|
|
|
|
|
|
|
|
const allPath = "/.well-known/(.*)";
|
|
|
|
const webFingerPath = "/.well-known/webfinger";
|
|
|
|
const jrd = "application/jrd+json";
|
|
|
|
const xrd = "application/xrd+xml";
|
2019-02-05 09:42:55 +01:00
|
|
|
|
2020-01-15 20:46:43 +01:00
|
|
|
router.use(allPath, async (ctx, next) => {
|
|
|
|
ctx.set({
|
2023-01-13 05:40:33 +01:00
|
|
|
"Access-Control-Allow-Headers": "Accept",
|
|
|
|
"Access-Control-Allow-Methods": "GET, OPTIONS",
|
|
|
|
"Access-Control-Allow-Origin": "*",
|
|
|
|
"Access-Control-Expose-Headers": "Vary",
|
2020-01-15 20:46:43 +01:00
|
|
|
});
|
|
|
|
await next();
|
|
|
|
});
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
router.options(allPath, async (ctx) => {
|
2020-01-15 20:46:43 +01:00
|
|
|
ctx.status = 204;
|
|
|
|
});
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
router.get("/.well-known/host-meta", async (ctx) => {
|
|
|
|
ctx.set("Content-Type", xrd);
|
|
|
|
ctx.body = XRD({
|
|
|
|
element: "Link",
|
|
|
|
attributes: {
|
|
|
|
rel: "lrdd",
|
|
|
|
type: xrd,
|
|
|
|
template: `${config.url}${webFingerPath}?resource={uri}`,
|
|
|
|
},
|
|
|
|
});
|
2019-02-05 09:42:55 +01:00
|
|
|
});
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
router.get("/.well-known/host-meta.json", async (ctx) => {
|
|
|
|
ctx.set("Content-Type", jrd);
|
2019-02-05 09:42:55 +01:00
|
|
|
ctx.body = {
|
2023-01-13 05:40:33 +01:00
|
|
|
links: [
|
|
|
|
{
|
|
|
|
rel: "lrdd",
|
|
|
|
type: jrd,
|
|
|
|
template: `${config.url}${webFingerPath}?resource={uri}`,
|
|
|
|
},
|
|
|
|
],
|
2019-02-05 09:42:55 +01:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2023-01-21 21:55:38 +01:00
|
|
|
if (config.twa.nameSpace != null) {
|
|
|
|
router.get("/.well-known/assetlinks.json", async (ctx) => {
|
|
|
|
ctx.set("Content-Type", "application/json");
|
|
|
|
ctx.body = [
|
|
|
|
{
|
|
|
|
relation: ["delegate_permission/common.handle_all_urls"],
|
|
|
|
target: {
|
|
|
|
namespace: config.twa.nameSpace,
|
|
|
|
package_name: config.twa.packageName,
|
|
|
|
sha256_cert_fingerprints: config.twa.sha256CertFingerprints,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
router.get("/.well-known/nodeinfo", async (ctx) => {
|
2019-02-05 09:42:55 +01:00
|
|
|
ctx.body = { links };
|
|
|
|
});
|
|
|
|
|
2021-05-04 08:05:34 +02:00
|
|
|
/* TODO
|
|
|
|
router.get('/.well-known/change-password', async ctx => {
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
router.get(webFingerPath, async (ctx) => {
|
|
|
|
const fromId = (id: User["id"]): FindOptionsWhere<User> => ({
|
2019-04-07 14:50:36 +02:00
|
|
|
id,
|
2022-03-26 11:48:22 +01:00
|
|
|
host: IsNull(),
|
2021-12-09 15:58:30 +01:00
|
|
|
isSuspended: false,
|
2019-04-07 14:50:36 +02:00
|
|
|
});
|
|
|
|
|
2022-03-26 11:48:22 +01:00
|
|
|
const generateQuery = (resource: string): FindOptionsWhere<User> | number =>
|
2023-01-13 05:40:33 +01:00
|
|
|
resource.startsWith(`${config.url.toLowerCase()}/users/`)
|
|
|
|
? fromId(resource.split("/").pop()!)
|
|
|
|
: fromAcct(
|
|
|
|
Acct.parse(
|
|
|
|
resource.startsWith(`${config.url.toLowerCase()}/@`)
|
|
|
|
? resource.split("/").pop()!
|
|
|
|
: resource.startsWith("acct:")
|
|
|
|
? resource.slice("acct:".length)
|
|
|
|
: resource,
|
|
|
|
),
|
|
|
|
);
|
2019-02-05 09:42:55 +01:00
|
|
|
|
2022-03-26 11:48:22 +01:00
|
|
|
const fromAcct = (acct: Acct.Acct): FindOptionsWhere<User> | number =>
|
2023-01-13 05:40:33 +01:00
|
|
|
!acct.host || acct.host === config.host.toLowerCase()
|
|
|
|
? {
|
|
|
|
usernameLower: acct.username,
|
|
|
|
host: IsNull(),
|
|
|
|
isSuspended: false,
|
|
|
|
}
|
|
|
|
: 422;
|
|
|
|
|
|
|
|
if (typeof ctx.query.resource !== "string") {
|
2019-02-05 09:42:55 +01:00
|
|
|
ctx.status = 400;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const query = generateQuery(ctx.query.resource.toLowerCase());
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
if (typeof query === "number") {
|
2019-02-05 09:42:55 +01:00
|
|
|
ctx.status = query;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
const user = await Users.findOneBy(query);
|
2019-02-05 09:42:55 +01:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (user == null) {
|
2019-02-05 09:42:55 +01:00
|
|
|
ctx.status = 404;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-20 08:13:43 +01:00
|
|
|
const subject = `acct:${user.username}@${config.host}`;
|
|
|
|
const self = {
|
2023-01-13 05:40:33 +01:00
|
|
|
rel: "self",
|
|
|
|
type: "application/activity+json",
|
2021-12-09 15:58:30 +01:00
|
|
|
href: `${config.url}/users/${user.id}`,
|
2019-02-05 09:42:55 +01:00
|
|
|
};
|
2019-02-20 08:13:43 +01:00
|
|
|
const profilePage = {
|
2023-01-13 05:40:33 +01:00
|
|
|
rel: "http://webfinger.net/rel/profile-page",
|
|
|
|
type: "text/html",
|
2021-12-09 15:58:30 +01:00
|
|
|
href: `${config.url}/@${user.username}`,
|
2019-02-20 08:13:43 +01:00
|
|
|
};
|
|
|
|
const subscribe = {
|
2023-01-13 05:40:33 +01:00
|
|
|
rel: "http://ostatus.org/schema/1.0/subscribe",
|
2021-12-09 15:58:30 +01:00
|
|
|
template: `${config.url}/authorize-follow?acct={uri}`,
|
2019-02-20 08:13:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if (ctx.accepts(jrd, xrd) === xrd) {
|
|
|
|
ctx.body = XRD(
|
2023-01-13 05:40:33 +01:00
|
|
|
{ element: "Subject", value: subject },
|
|
|
|
{ element: "Link", attributes: self },
|
|
|
|
{ element: "Link", attributes: profilePage },
|
|
|
|
{ element: "Link", attributes: subscribe },
|
|
|
|
);
|
2019-02-20 08:13:43 +01:00
|
|
|
ctx.type = xrd;
|
|
|
|
} else {
|
|
|
|
ctx.body = {
|
|
|
|
subject,
|
2021-12-09 15:58:30 +01:00
|
|
|
links: [self, profilePage, subscribe],
|
2019-02-20 08:13:43 +01:00
|
|
|
};
|
|
|
|
ctx.type = jrd;
|
|
|
|
}
|
2019-02-05 09:42:55 +01:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
ctx.vary("Accept");
|
|
|
|
ctx.set("Cache-Control", "public, max-age=180");
|
2019-02-05 09:42:55 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Return 404 for other .well-known
|
2023-01-13 05:40:33 +01:00
|
|
|
router.all(allPath, async (ctx) => {
|
2019-02-05 09:42:55 +01:00
|
|
|
ctx.status = 404;
|
|
|
|
});
|
|
|
|
|
|
|
|
export default router;
|