add webfinger and routes boilerplate

This commit is contained in:
sam 2023-07-21 01:08:06 +02:00
parent cad4c59d51
commit 9f74db9857
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
8 changed files with 589 additions and 17 deletions

View file

@ -0,0 +1,80 @@
import type { FastifyReply, FastifyRequest, RouteOptions } from "fastify";
import { IsNull } from "typeorm";
import log from "../../log.js";
import MercuryDataSource from "../../db/index.js";
import { Blog } from "../../db/entities/blog.js";
import { BASE_URL } from "../../config.js";
const route: RouteOptions = {
method: "GET",
url: "/.well-known/webfinger",
handler: async (req, res) => {
// TypeScript complains if we just use plain `req.query` :(
const encodedResource = (req.query as { resource: string }).resource;
if (!encodedResource || typeof encodedResource !== "string") {
res.status(400).send({
error: "resource query parameter is missing or invalid",
});
return;
}
const resource = decodeURIComponent(encodedResource);
log.debug("Handling WebFinger request for %s", resource);
if (resource.startsWith("acct:")) {
await handleAcctWebfinger(req, res, resource);
return;
}
res.status(500).send({ error: "Unhandled WebFinger resource type" });
},
};
export default route;
async function handleAcctWebfinger(
req: FastifyRequest,
res: FastifyReply,
resource: string
): Promise<void> {
const [username, domain] = resource.slice("acct:".length).split("@");
if (domain !== process.env.DOMAIN) {
res.status(404).send({
error: "Account not found",
});
return;
}
const blog = await MercuryDataSource.getRepository(Blog).findOneBy({
username: username,
host: IsNull(),
});
if (!blog) {
res.status(404).send({
error: "Account not found",
});
return;
}
res.send({
subject: resource,
aliases: [
`${BASE_URL}/@${blog.username}`,
`${BASE_URL}/blogs/${blog.username}`,
],
links: [
{
rel: "http://webfinger.net/rel/profile-page",
type: "text/html",
href: `${BASE_URL}/@${blog.username}`,
},
{
rel: "self",
type: "application/activity+json",
href: `${BASE_URL}/blogs/${blog.username}`,
},
],
});
}