2024-05-15 02:35:13 +02:00
|
|
|
import type { EntityValidator } from "@lysand-org/federation";
|
2024-04-10 07:51:00 +02:00
|
|
|
import { config } from "config-manager";
|
2024-04-25 05:40:27 +02:00
|
|
|
import type { User } from "~packages/database-interface/user";
|
2024-04-10 07:51:00 +02:00
|
|
|
|
2024-04-17 06:09:21 +02:00
|
|
|
export const localObjectURI = (id: string) => `/objects/${id}`;
|
|
|
|
|
|
2024-04-10 07:51:00 +02:00
|
|
|
export const objectToInboxRequest = async (
|
2024-05-15 02:35:13 +02:00
|
|
|
object: typeof EntityValidator.$Entity,
|
2024-04-10 07:51:00 +02:00
|
|
|
author: User,
|
|
|
|
|
userToSendTo: User,
|
|
|
|
|
): Promise<Request> => {
|
2024-04-25 05:40:27 +02:00
|
|
|
if (userToSendTo.isLocal() || !userToSendTo.getUser().endpoints?.inbox) {
|
2024-04-10 09:13:45 +02:00
|
|
|
throw new Error("UserToSendTo has no inbox or is a local user");
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-25 05:40:27 +02:00
|
|
|
if (author.isRemote()) {
|
2024-04-10 09:13:45 +02:00
|
|
|
throw new Error("Author is a remote user");
|
2024-04-10 07:51:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const privateKey = await crypto.subtle.importKey(
|
|
|
|
|
"pkcs8",
|
2024-04-25 05:40:27 +02:00
|
|
|
Buffer.from(author.getUser().privateKey ?? "", "base64"),
|
2024-04-10 07:51:00 +02:00
|
|
|
"Ed25519",
|
|
|
|
|
false,
|
|
|
|
|
["sign"],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const digest = await crypto.subtle.digest(
|
|
|
|
|
"SHA-256",
|
|
|
|
|
new TextEncoder().encode(JSON.stringify(object)),
|
|
|
|
|
);
|
|
|
|
|
|
2024-04-25 05:40:27 +02:00
|
|
|
const userInbox = new URL(userToSendTo.getUser().endpoints?.inbox ?? "");
|
2024-04-10 07:51:00 +02:00
|
|
|
|
|
|
|
|
const date = new Date();
|
|
|
|
|
|
|
|
|
|
const signature = await crypto.subtle.sign(
|
|
|
|
|
"Ed25519",
|
|
|
|
|
privateKey,
|
|
|
|
|
new TextEncoder().encode(
|
|
|
|
|
`(request-target): post ${userInbox.pathname}\n` +
|
|
|
|
|
`host: ${userInbox.host}\n` +
|
|
|
|
|
`date: ${date.toISOString()}\n` +
|
2024-04-25 05:40:27 +02:00
|
|
|
`digest: SHA-256=${Buffer.from(new Uint8Array(digest)).toString(
|
|
|
|
|
"base64",
|
2024-04-10 07:51:00 +02:00
|
|
|
)}\n`,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
2024-04-25 05:40:27 +02:00
|
|
|
const signatureBase64 = Buffer.from(new Uint8Array(signature)).toString(
|
|
|
|
|
"base64",
|
2024-04-10 07:51:00 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return new Request(userInbox, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Date: date.toISOString(),
|
2024-04-10 08:37:38 +02:00
|
|
|
Origin: new URL(config.http.base_url).host,
|
2024-04-25 05:40:27 +02:00
|
|
|
Signature: `keyId="${author.getUri()}",algorithm="ed25519",headers="(request-target) host date digest",signature="${signatureBase64}"`,
|
2024-04-10 07:51:00 +02:00
|
|
|
},
|
|
|
|
|
body: JSON.stringify(object),
|
|
|
|
|
});
|
|
|
|
|
};
|