From a701fed9e5c945261cb31d7eb80502d30ca8c445 Mon Sep 17 00:00:00 2001 From: tamaina Date: Tue, 5 Mar 2024 16:50:26 +0000 Subject: [PATCH] fix ap-request --- packages/backend/test/unit/ap-request.ts | 41 ++++++++++++++++-------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/packages/backend/test/unit/ap-request.ts b/packages/backend/test/unit/ap-request.ts index 1c0159ed95..8406cb7ec2 100644 --- a/packages/backend/test/unit/ap-request.ts +++ b/packages/backend/test/unit/ap-request.ts @@ -4,7 +4,7 @@ */ import * as assert from 'assert'; -import { verifyDraftSignature, parseRequestSignature, genEd25519KeyPair, genRsaKeyPair } from '@misskey-dev/node-http-message-signatures'; +import { verifyDraftSignature, parseRequestSignature, genEd25519KeyPair, genRsaKeyPair, importPrivateKey } from '@misskey-dev/node-http-message-signatures'; import { createSignedGet, createSignedPost } from '@/core/activitypub/ApRequestService.js'; export const buildParsedSignature = (signingString: string, signature: string, algorithm: string) => { @@ -31,32 +31,47 @@ async function getKeyPair(level: string) { throw new Error('Invalid level'); } -describe('ap-request', () => { +describe('ap-request post', () => { + const url = 'https://example.com/inbox'; + const activity = { a: 1 }; + const body = JSON.stringify(activity); + const headers = { + 'User-Agent': 'UA', + }; + describe.each(['00', '01'])('createSignedPost with verify', (level) => { - test('pass', async () => { + test('pem', async () => { const keypair = await getKeyPair(level); - const key = { keyId: 'x', 'privateKey': keypair.privateKey }; - const url = 'https://example.com/inbox'; - const activity = { a: 1 }; - const body = JSON.stringify(activity); - const headers = { - 'User-Agent': 'UA', - }; + 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'); + 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) }; + + 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); }); }); +}); +describe('ap-request get', () => { describe.each(['00', '01'])('createSignedGet with verify', (level) => { test('pass', async () => { const keypair = await getKeyPair(level); - const key = { keyId: 'x', 'privateKey': keypair.privateKey }; + const key = { keyId: 'x', 'privateKeyPem': keypair.privateKey }; const url = 'https://example.com/outbox'; const headers = { 'User-Agent': 'UA', @@ -65,7 +80,7 @@ describe('ap-request', () => { const req = await createSignedGet({ level, key, url, additionalHeaders: headers }); const parsed = parseRequestSignature(req.request); - expect(parsed?.version).toBe('draft'); + expect(parsed.version).toBe('draft'); if (!parsed) return; const verify = await verifyDraftSignature(parsed.value, keypair.publicKey); assert.deepStrictEqual(verify, true);