2024-02-29 16:36:19 -06:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
import * as assert from 'assert';
|
2024-03-05 10:50:26 -06:00
|
|
|
import { verifyDraftSignature, parseRequestSignature, genEd25519KeyPair, genRsaKeyPair, importPrivateKey } from '@misskey-dev/node-http-message-signatures';
|
2024-02-29 16:36:19 -06:00
|
|
|
import { createSignedGet, createSignedPost } from '@/core/activitypub/ApRequestService.js';
|
|
|
|
|
|
|
|
export const buildParsedSignature = (signingString: string, signature: string, algorithm: string) => {
|
|
|
|
return {
|
|
|
|
scheme: 'Signature',
|
|
|
|
params: {
|
|
|
|
keyId: 'KeyID', // dummy, not used for verify
|
|
|
|
algorithm: algorithm,
|
|
|
|
headers: ['(request-target)', 'date', 'host', 'digest'], // dummy, not used for verify
|
|
|
|
signature: signature,
|
|
|
|
},
|
|
|
|
signingString: signingString,
|
|
|
|
algorithm: algorithm.toUpperCase(),
|
|
|
|
keyId: 'KeyID', // dummy, not used for verify
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
async function getKeyPair(level: string) {
|
|
|
|
if (level === '00') {
|
|
|
|
return await genRsaKeyPair();
|
|
|
|
} else if (level === '01') {
|
|
|
|
return await genEd25519KeyPair();
|
|
|
|
}
|
|
|
|
throw new Error('Invalid level');
|
|
|
|
}
|
|
|
|
|
2024-03-05 10:50:26 -06:00
|
|
|
describe('ap-request post', () => {
|
|
|
|
const url = 'https://example.com/inbox';
|
|
|
|
const activity = { a: 1 };
|
|
|
|
const body = JSON.stringify(activity);
|
|
|
|
const headers = {
|
|
|
|
'User-Agent': 'UA',
|
|
|
|
};
|
|
|
|
|
2024-02-29 16:57:10 -06:00
|
|
|
describe.each(['00', '01'])('createSignedPost with verify', (level) => {
|
2024-03-05 10:50:26 -06:00
|
|
|
test('pem', async () => {
|
2024-02-29 16:57:10 -06:00
|
|
|
const keypair = await getKeyPair(level);
|
2024-03-05 10:50:26 -06:00
|
|
|
const key = { keyId: 'x', 'privateKeyPem': keypair.privateKey };
|
|
|
|
|
|
|
|
const req = await createSignedPost({ level, key, url, body, additionalHeaders: headers });
|
|
|
|
|
|
|
|
const parsed = parseRequestSignature(req.request);
|
|
|
|
expect(parsed.version).toBe('draft');
|
|
|
|
if (!parsed) return;
|
|
|
|
const verify = await verifyDraftSignature(parsed.value, keypair.publicKey);
|
|
|
|
assert.deepStrictEqual(verify, true);
|
|
|
|
});
|
|
|
|
test('imported', async () => {
|
|
|
|
const keypair = await getKeyPair(level);
|
|
|
|
const key = { keyId: 'x', 'privateKey': await importPrivateKey(keypair.privateKey) };
|
2024-02-29 16:36:19 -06:00
|
|
|
|
2024-03-03 15:02:23 -06:00
|
|
|
const req = await createSignedPost({ level, key, url, body, additionalHeaders: headers });
|
2024-02-29 16:36:19 -06:00
|
|
|
|
2024-02-29 16:57:10 -06:00
|
|
|
const parsed = parseRequestSignature(req.request);
|
2024-03-05 10:50:26 -06:00
|
|
|
expect(parsed.version).toBe('draft');
|
2024-02-29 16:57:10 -06:00
|
|
|
if (!parsed) return;
|
2024-03-03 15:02:23 -06:00
|
|
|
const verify = await verifyDraftSignature(parsed.value, keypair.publicKey);
|
2024-02-29 16:57:10 -06:00
|
|
|
assert.deepStrictEqual(verify, true);
|
|
|
|
});
|
2024-02-29 16:36:19 -06:00
|
|
|
});
|
2024-03-05 10:50:26 -06:00
|
|
|
});
|
2024-02-29 16:36:19 -06:00
|
|
|
|
2024-03-05 10:50:26 -06:00
|
|
|
describe('ap-request get', () => {
|
2024-02-29 16:57:10 -06:00
|
|
|
describe.each(['00', '01'])('createSignedGet with verify', (level) => {
|
|
|
|
test('pass', async () => {
|
|
|
|
const keypair = await getKeyPair(level);
|
2024-03-05 10:50:26 -06:00
|
|
|
const key = { keyId: 'x', 'privateKeyPem': keypair.privateKey };
|
2024-02-29 16:57:10 -06:00
|
|
|
const url = 'https://example.com/outbox';
|
|
|
|
const headers = {
|
|
|
|
'User-Agent': 'UA',
|
|
|
|
};
|
2024-02-29 16:36:19 -06:00
|
|
|
|
2024-03-03 15:02:23 -06:00
|
|
|
const req = await createSignedGet({ level, key, url, additionalHeaders: headers });
|
2024-02-29 16:36:19 -06:00
|
|
|
|
2024-02-29 16:57:10 -06:00
|
|
|
const parsed = parseRequestSignature(req.request);
|
2024-03-05 10:50:26 -06:00
|
|
|
expect(parsed.version).toBe('draft');
|
2024-02-29 16:57:10 -06:00
|
|
|
if (!parsed) return;
|
2024-03-03 15:02:23 -06:00
|
|
|
const verify = await verifyDraftSignature(parsed.value, keypair.publicKey);
|
2024-02-29 16:57:10 -06:00
|
|
|
assert.deepStrictEqual(verify, true);
|
|
|
|
});
|
2024-02-29 16:36:19 -06:00
|
|
|
});
|
|
|
|
});
|