2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2021-10-16 10:16:24 +02:00
|
|
|
import * as assert from 'assert';
|
2024-07-20 14:33:20 +02:00
|
|
|
import httpSignature from '@peertube/http-signature';
|
|
|
|
|
|
|
|
import { genRsaKeyPair } from '@/misc/gen-key-pair.js';
|
|
|
|
import { ApRequestCreator } from '@/core/activitypub/ApRequestService.js';
|
2021-10-16 10:16:24 +02:00
|
|
|
|
|
|
|
export const buildParsedSignature = (signingString: string, signature: string, algorithm: string) => {
|
|
|
|
return {
|
|
|
|
scheme: 'Signature',
|
|
|
|
params: {
|
|
|
|
keyId: 'KeyID', // dummy, not used for verify
|
|
|
|
algorithm: algorithm,
|
2023-02-24 08:10:48 +01:00
|
|
|
headers: ['(request-target)', 'date', 'host', 'digest'], // dummy, not used for verify
|
2021-10-16 10:16:24 +02:00
|
|
|
signature: signature,
|
|
|
|
},
|
|
|
|
signingString: signingString,
|
2022-05-21 15:21:41 +02:00
|
|
|
algorithm: algorithm.toUpperCase(),
|
2021-10-16 10:16:24 +02:00
|
|
|
keyId: 'KeyID', // dummy, not used for verify
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-07-20 14:33:20 +02:00
|
|
|
describe('ap-request', () => {
|
|
|
|
test('createSignedPost with verify', async () => {
|
|
|
|
const keypair = await genRsaKeyPair();
|
|
|
|
const key = { keyId: 'x', 'privateKeyPem': keypair.privateKey };
|
|
|
|
const url = 'https://example.com/inbox';
|
|
|
|
const activity = { a: 1 };
|
|
|
|
const body = JSON.stringify(activity);
|
|
|
|
const headers = {
|
|
|
|
'User-Agent': 'UA',
|
|
|
|
};
|
2021-10-16 10:16:24 +02:00
|
|
|
|
2024-07-20 14:33:20 +02:00
|
|
|
const req = ApRequestCreator.createSignedPost({ key, url, body, additionalHeaders: headers });
|
2021-10-16 10:16:24 +02:00
|
|
|
|
2024-07-20 14:33:20 +02:00
|
|
|
const parsed = buildParsedSignature(req.signingString, req.signature, 'rsa-sha256');
|
2021-10-16 10:16:24 +02:00
|
|
|
|
2024-07-20 14:33:20 +02:00
|
|
|
const result = httpSignature.verifySignature(parsed, keypair.publicKey);
|
|
|
|
assert.deepStrictEqual(result, true);
|
2024-07-17 18:28:17 +02:00
|
|
|
});
|
2021-10-16 10:16:24 +02:00
|
|
|
|
2024-07-20 14:33:20 +02:00
|
|
|
test('createSignedGet with verify', async () => {
|
|
|
|
const keypair = await genRsaKeyPair();
|
|
|
|
const key = { keyId: 'x', 'privateKeyPem': keypair.privateKey };
|
|
|
|
const url = 'https://example.com/outbox';
|
|
|
|
const headers = {
|
|
|
|
'User-Agent': 'UA',
|
|
|
|
};
|
|
|
|
|
|
|
|
const req = ApRequestCreator.createSignedGet({ key, url, additionalHeaders: headers });
|
2021-10-16 10:16:24 +02:00
|
|
|
|
2024-07-20 14:33:20 +02:00
|
|
|
const parsed = buildParsedSignature(req.signingString, req.signature, 'rsa-sha256');
|
2021-10-16 10:16:24 +02:00
|
|
|
|
2024-07-20 14:33:20 +02:00
|
|
|
const result = httpSignature.verifySignature(parsed, keypair.publicKey);
|
|
|
|
assert.deepStrictEqual(result, true);
|
2021-10-16 10:16:24 +02:00
|
|
|
});
|
|
|
|
});
|