trashposs/packages/backend/test/ap-request.ts

76 lines
1.9 KiB
TypeScript
Raw Permalink Normal View History

2023-04-07 03:56:46 +02:00
import * as assert from "assert";
import httpSignature from "@peertube/http-signature";
import { genRsaKeyPair } from "../src/misc/gen-key-pair.js";
import {
createSignedPost,
createSignedGet,
} from "../src/remote/activitypub/ap-request.js";
2023-04-07 03:56:46 +02:00
export const buildParsedSignature = (
signingString: string,
signature: string,
algorithm: string,
) => {
return {
2023-04-07 03:56:46 +02:00
scheme: "Signature",
params: {
2023-04-07 03:56:46 +02:00
keyId: "KeyID", // dummy, not used for verify
algorithm: algorithm,
2023-04-07 03:56:46 +02:00
headers: ["(request-target)", "date", "host", "digest"], // dummy, not used for verify
signature: signature,
},
signingString: signingString,
2022-05-21 15:21:41 +02:00
algorithm: algorithm.toUpperCase(),
2023-04-07 03:56:46 +02:00
keyId: "KeyID", // dummy, not used for verify
};
};
2023-04-07 03:56:46 +02:00
describe("ap-request", () => {
it("createSignedPost with verify", async () => {
const keypair = await genRsaKeyPair();
2023-04-07 03:56:46 +02:00
const key = { keyId: "x", privateKeyPem: keypair.privateKey };
const url = "https://example.com/inbox";
const activity = { a: 1 };
const body = JSON.stringify(activity);
const headers = {
2023-04-07 03:56:46 +02:00
"User-Agent": "UA",
};
2023-04-07 03:56:46 +02:00
const req = createSignedPost({
key,
url,
body,
additionalHeaders: headers,
});
2023-04-07 03:56:46 +02:00
const parsed = buildParsedSignature(
req.signingString,
req.signature,
"rsa-sha256",
);
const result = httpSignature.verifySignature(parsed, keypair.publicKey);
assert.deepStrictEqual(result, true);
});
2023-04-07 03:56:46 +02:00
it("createSignedGet with verify", async () => {
const keypair = await genRsaKeyPair();
2023-04-07 03:56:46 +02:00
const key = { keyId: "x", privateKeyPem: keypair.privateKey };
const url = "https://example.com/outbox";
const headers = {
2023-04-07 03:56:46 +02:00
"User-Agent": "UA",
};
const req = createSignedGet({ key, url, additionalHeaders: headers });
2023-04-07 03:56:46 +02:00
const parsed = buildParsedSignature(
req.signingString,
req.signature,
"rsa-sha256",
);
const result = httpSignature.verifySignature(parsed, keypair.publicKey);
assert.deepStrictEqual(result, true);
});
});