2023-07-27 05:31:52 +00:00
|
|
|
/*
|
2024-02-13 15:59:27 +00:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 05:31:52 +00:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2021-10-16 08:16:24 +00:00
|
|
|
import * as assert from 'assert';
|
2024-07-17 16:28:17 +00:00
|
|
|
import { verifyDraftSignature, parseRequestSignature, genEd25519KeyPair, genRsaKeyPair, importPrivateKey } from '@misskey-dev/node-http-message-signatures';
|
|
|
|
import { createSignedGet, createSignedPost } from '@/core/activitypub/ApRequestService.js';
|
2021-10-16 08:16:24 +00: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 07:10:48 +00:00
|
|
|
headers: ['(request-target)', 'date', 'host', 'digest'], // dummy, not used for verify
|
2021-10-16 08:16:24 +00:00
|
|
|
signature: signature,
|
|
|
|
},
|
|
|
|
signingString: signingString,
|
2022-05-21 13:21:41 +00:00
|
|
|
algorithm: algorithm.toUpperCase(),
|
2021-10-16 08:16:24 +00:00
|
|
|
keyId: 'KeyID', // dummy, not used for verify
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-07-17 16:28:17 +00:00
|
|
|
async function getKeyPair(level: string) {
|
|
|
|
if (level === '00') {
|
|
|
|
return await genRsaKeyPair();
|
|
|
|
} else if (level === '01') {
|
|
|
|
return await genEd25519KeyPair();
|
|
|
|
}
|
|
|
|
throw new Error('Invalid level');
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('ap-request post', () => {
|
|
|
|
const url = 'https://example.com/inbox';
|
|
|
|
const activity = { a: 1 };
|
|
|
|
const body = JSON.stringify(activity);
|
|
|
|
const headers = {
|
|
|
|
'User-Agent': 'UA',
|
|
|
|
};
|
2021-10-16 08:16:24 +00:00
|
|
|
|
2024-07-17 16:28:17 +00:00
|
|
|
describe.each(['00', '01'])('createSignedPost with verify', (level) => {
|
|
|
|
test('pem', async () => {
|
|
|
|
const keypair = await getKeyPair(level);
|
|
|
|
const key = { keyId: 'x', 'privateKeyPem': keypair.privateKey };
|
2021-10-16 08:16:24 +00:00
|
|
|
|
2024-07-17 16:28:17 +00:00
|
|
|
const req = await createSignedPost({ level, key, url, body, additionalHeaders: headers });
|
2021-10-16 08:16:24 +00:00
|
|
|
|
2024-07-17 16:28:17 +00:00
|
|
|
const parsed = parseRequestSignature(req.request);
|
|
|
|
expect(parsed.version).toBe('draft');
|
|
|
|
expect(Array.isArray(parsed.value)).toBe(false);
|
|
|
|
const verify = await verifyDraftSignature(parsed.value as any, keypair.publicKey);
|
|
|
|
assert.deepStrictEqual(verify, true);
|
|
|
|
});
|
|
|
|
test('imported', async () => {
|
|
|
|
const keypair = await getKeyPair(level);
|
|
|
|
const key = { keyId: 'x', 'privateKey': await importPrivateKey(keypair.privateKey) };
|
2021-10-16 08:16:24 +00:00
|
|
|
|
2024-07-17 16:28:17 +00:00
|
|
|
const req = await createSignedPost({ level, key, url, body, additionalHeaders: headers });
|
|
|
|
|
|
|
|
const parsed = parseRequestSignature(req.request);
|
|
|
|
expect(parsed.version).toBe('draft');
|
|
|
|
expect(Array.isArray(parsed.value)).toBe(false);
|
|
|
|
const verify = await verifyDraftSignature(parsed.value as any, keypair.publicKey);
|
|
|
|
assert.deepStrictEqual(verify, true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-10-16 08:16:24 +00:00
|
|
|
|
2024-07-17 16:28:17 +00:00
|
|
|
describe('ap-request get', () => {
|
|
|
|
describe.each(['00', '01'])('createSignedGet with verify', (level) => {
|
|
|
|
test('pass', async () => {
|
|
|
|
const keypair = await getKeyPair(level);
|
|
|
|
const key = { keyId: 'x', 'privateKeyPem': keypair.privateKey };
|
|
|
|
const url = 'https://example.com/outbox';
|
|
|
|
const headers = {
|
|
|
|
'User-Agent': 'UA',
|
|
|
|
};
|
2021-10-16 08:16:24 +00:00
|
|
|
|
2024-07-17 16:28:17 +00:00
|
|
|
const req = await createSignedGet({ level, key, url, additionalHeaders: headers });
|
2021-10-16 08:16:24 +00:00
|
|
|
|
2024-07-17 16:28:17 +00:00
|
|
|
const parsed = parseRequestSignature(req.request);
|
|
|
|
expect(parsed.version).toBe('draft');
|
|
|
|
expect(Array.isArray(parsed.value)).toBe(false);
|
|
|
|
const verify = await verifyDraftSignature(parsed.value as any, keypair.publicKey);
|
|
|
|
assert.deepStrictEqual(verify, true);
|
|
|
|
});
|
2021-10-16 08:16:24 +00:00
|
|
|
});
|
|
|
|
});
|