2024-11-01 20:42:32 +01:00
|
|
|
import { apiRoute, applyConfig } from "@/api";
|
2024-09-16 15:29:09 +02:00
|
|
|
import { createRoute } from "@hono/zod-openapi";
|
2024-11-01 20:42:32 +01:00
|
|
|
import { getLogger } from "@logtape/logtape";
|
|
|
|
|
import type { Entity } from "@versia/federation/types";
|
2024-05-06 09:16:33 +02:00
|
|
|
import { z } from "zod";
|
2024-10-24 16:28:38 +02:00
|
|
|
import { User } from "~/classes/database/user";
|
2024-11-01 20:42:32 +01:00
|
|
|
import { InboxProcessor } from "~/classes/inbox/processor";
|
2024-09-16 15:29:09 +02:00
|
|
|
import { ErrorSchema } from "~/types/api";
|
2024-05-06 09:16:33 +02:00
|
|
|
|
|
|
|
|
export const meta = applyConfig({
|
|
|
|
|
auth: {
|
|
|
|
|
required: false,
|
|
|
|
|
},
|
|
|
|
|
ratelimits: {
|
|
|
|
|
duration: 60,
|
|
|
|
|
max: 500,
|
|
|
|
|
},
|
2024-05-17 09:51:49 +02:00
|
|
|
route: "/users/:uuid/inbox",
|
2024-05-06 09:16:33 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const schemas = {
|
|
|
|
|
param: z.object({
|
|
|
|
|
uuid: z.string().uuid(),
|
|
|
|
|
}),
|
|
|
|
|
header: z.object({
|
2024-08-27 02:17:07 +02:00
|
|
|
"x-signature": z.string(),
|
|
|
|
|
"x-nonce": z.string(),
|
|
|
|
|
"x-signed-by": z.string().url().or(z.literal("instance")),
|
2024-05-22 02:59:03 +02:00
|
|
|
authorization: z.string().optional(),
|
2024-05-06 09:16:33 +02:00
|
|
|
}),
|
2024-05-17 10:37:06 +02:00
|
|
|
body: z.any(),
|
2024-05-06 09:16:33 +02:00
|
|
|
};
|
|
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
const route = createRoute({
|
|
|
|
|
method: "post",
|
|
|
|
|
path: "/users/{uuid}/inbox",
|
|
|
|
|
summary: "Receive federation inbox",
|
|
|
|
|
request: {
|
|
|
|
|
params: schemas.param,
|
|
|
|
|
headers: schemas.header,
|
|
|
|
|
body: {
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: schemas.body,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
responses: {
|
|
|
|
|
200: {
|
|
|
|
|
description: "Request processed",
|
|
|
|
|
},
|
|
|
|
|
201: {
|
|
|
|
|
description: "Request accepted",
|
|
|
|
|
},
|
|
|
|
|
400: {
|
|
|
|
|
description: "Bad request",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: ErrorSchema,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
401: {
|
|
|
|
|
description: "Signature could not be verified",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: ErrorSchema,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
403: {
|
|
|
|
|
description: "Cannot view users from remote instances",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: ErrorSchema,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
404: {
|
|
|
|
|
description: "Not found",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: ErrorSchema,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
500: {
|
|
|
|
|
description: "Internal server error",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: z.object({
|
|
|
|
|
error: z.string(),
|
|
|
|
|
message: z.string(),
|
2024-05-22 02:59:03 +02:00
|
|
|
}),
|
2024-09-16 15:29:09 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
2024-05-17 19:56:13 +02:00
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
export default apiRoute((app) =>
|
|
|
|
|
app.openapi(route, async (context) => {
|
|
|
|
|
const {
|
|
|
|
|
"x-signature": signature,
|
|
|
|
|
"x-nonce": nonce,
|
|
|
|
|
"x-signed-by": signedBy,
|
|
|
|
|
authorization,
|
|
|
|
|
} = context.req.valid("header");
|
|
|
|
|
|
2024-11-01 20:42:32 +01:00
|
|
|
const logger = getLogger(["federation", "inbox"]);
|
2024-09-16 15:29:09 +02:00
|
|
|
const body: Entity = await context.req.valid("json");
|
|
|
|
|
|
|
|
|
|
const sender = await User.resolve(signedBy);
|
|
|
|
|
|
2024-10-28 13:13:50 +01:00
|
|
|
if (!sender) {
|
|
|
|
|
return context.json(
|
|
|
|
|
{ error: `Couldn't resolve sender ${signedBy}` },
|
|
|
|
|
404,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
if (sender?.isLocal()) {
|
|
|
|
|
return context.json(
|
|
|
|
|
{ error: "Cannot send federation requests to local users" },
|
|
|
|
|
400,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-01 20:42:32 +01:00
|
|
|
const processor = new InboxProcessor(
|
|
|
|
|
context,
|
|
|
|
|
body,
|
|
|
|
|
sender,
|
|
|
|
|
{
|
2024-10-28 13:13:50 +01:00
|
|
|
signature,
|
|
|
|
|
nonce,
|
2024-11-01 20:42:32 +01:00
|
|
|
authorization,
|
2024-10-28 13:13:50 +01:00
|
|
|
},
|
2024-11-01 20:42:32 +01:00
|
|
|
logger,
|
2024-10-28 13:13:50 +01:00
|
|
|
);
|
|
|
|
|
|
2024-11-01 20:42:32 +01:00
|
|
|
return await processor.process();
|
|
|
|
|
}),
|
|
|
|
|
);
|