From a219c9100b3ce8774d454497d46be87465bf664e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 23 Mar 2022 16:14:33 +0100 Subject: Refactor AP context builder --- server/tests/api/activitypub/helpers.ts | 6 +++--- server/tests/api/activitypub/security.ts | 23 +++++++++++------------ 2 files changed, 14 insertions(+), 15 deletions(-) (limited to 'server/tests/api/activitypub') diff --git a/server/tests/api/activitypub/helpers.ts b/server/tests/api/activitypub/helpers.ts index e516cf49e..bc1de35d1 100644 --- a/server/tests/api/activitypub/helpers.ts +++ b/server/tests/api/activitypub/helpers.ts @@ -3,10 +3,10 @@ import 'mocha' import { expect } from 'chai' import { cloneDeep } from 'lodash' +import { signAndContextify } from '@server/lib/activitypub/send' import { buildRequestStub } from '@server/tests/shared' import { buildAbsoluteFixturePath } from '@shared/core-utils' import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../../../helpers/peertube-crypto' -import { buildSignedActivity } from '../../../lib/activitypub/activity' describe('Test activity pub helpers', function () { describe('When checking the Linked Signature', function () { @@ -46,7 +46,7 @@ describe('Test activity pub helpers', function () { const body = require(buildAbsoluteFixturePath('./ap-json/peertube/announce-without-context.json')) const actorSignature = { url: 'http://localhost:9002/accounts/peertube', privateKey: keys.privateKey } - const signedBody = await buildSignedActivity(actorSignature as any, body) + const signedBody = await signAndContextify(actorSignature as any, body, 'Announce') const fromActor = { publicKey: keys.publicKey, url: 'http://localhost:9002/accounts/peertube' } const result = await isJsonLDSignatureVerified(fromActor as any, signedBody) @@ -59,7 +59,7 @@ describe('Test activity pub helpers', function () { const body = require(buildAbsoluteFixturePath('./ap-json/peertube/announce-without-context.json')) const actorSignature = { url: 'http://localhost:9002/accounts/peertube', privateKey: keys.privateKey } - const signedBody = await buildSignedActivity(actorSignature as any, body) + const signedBody = await signAndContextify(actorSignature as any, body, 'Announce') const fromActor = { publicKey: keys.publicKey, url: 'http://localhost:9002/accounts/peertube' } const result = await isJsonLDSignatureVerified(fromActor as any, signedBody) diff --git a/server/tests/api/activitypub/security.ts b/server/tests/api/activitypub/security.ts index da9880d7d..a070517b8 100644 --- a/server/tests/api/activitypub/security.ts +++ b/server/tests/api/activitypub/security.ts @@ -4,9 +4,8 @@ import 'mocha' import * as chai from 'chai' import { buildDigest } from '@server/helpers/peertube-crypto' import { HTTP_SIGNATURE } from '@server/initializers/constants' -import { buildSignedActivity } from '@server/lib/activitypub/activity' import { activityPubContextify } from '@server/lib/activitypub/context' -import { buildGlobalHeaders } from '@server/lib/job-queue/handlers/utils/activitypub-http-utils' +import { buildGlobalHeaders, signAndContextify } from '@server/lib/activitypub/send' import { makeFollowRequest, makePOSTAPRequest } from '@server/tests/shared' import { buildAbsoluteFixturePath, wait } from '@shared/core-utils' import { HttpStatusCode } from '@shared/models' @@ -81,7 +80,7 @@ describe('Test ActivityPub security', function () { describe('When checking HTTP signature', function () { it('Should fail with an invalid digest', async function () { - const body = activityPubContextify(getAnnounceWithoutContext(servers[1])) + const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce') const headers = { Digest: buildDigest({ hello: 'coucou' }) } @@ -95,7 +94,7 @@ describe('Test ActivityPub security', function () { }) it('Should fail with an invalid date', async function () { - const body = activityPubContextify(getAnnounceWithoutContext(servers[1])) + const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce') const headers = buildGlobalHeaders(body) headers['date'] = 'Wed, 21 Oct 2015 07:28:00 GMT' @@ -111,7 +110,7 @@ describe('Test ActivityPub security', function () { await setKeysOfServer(servers[0], servers[1], invalidKeys.publicKey, invalidKeys.privateKey) await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey) - const body = activityPubContextify(getAnnounceWithoutContext(servers[1])) + const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce') const headers = buildGlobalHeaders(body) try { @@ -126,7 +125,7 @@ describe('Test ActivityPub security', function () { await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey) await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey) - const body = activityPubContextify(getAnnounceWithoutContext(servers[1])) + const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce') const headers = buildGlobalHeaders(body) const signatureOptions = baseHttpSignature() @@ -149,7 +148,7 @@ describe('Test ActivityPub security', function () { }) it('Should succeed with a valid HTTP signature', async function () { - const body = activityPubContextify(getAnnounceWithoutContext(servers[1])) + const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce') const headers = buildGlobalHeaders(body) const { statusCode } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers) @@ -168,7 +167,7 @@ describe('Test ActivityPub security', function () { await killallServers([ servers[1] ]) await servers[1].run() - const body = activityPubContextify(getAnnounceWithoutContext(servers[1])) + const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce') const headers = buildGlobalHeaders(body) try { @@ -204,7 +203,7 @@ describe('Test ActivityPub security', function () { body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube' const signer: any = { privateKey: invalidKeys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' } - const signedBody = await buildSignedActivity(signer, body) + const signedBody = await signAndContextify(signer, body, 'Announce') const headers = buildGlobalHeaders(signedBody) @@ -226,7 +225,7 @@ describe('Test ActivityPub security', function () { body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube' const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' } - const signedBody = await buildSignedActivity(signer, body) + const signedBody = await signAndContextify(signer, body, 'Announce') signedBody.actor = 'http://localhost:' + servers[2].port + '/account/peertube' @@ -247,7 +246,7 @@ describe('Test ActivityPub security', function () { body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube' const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' } - const signedBody = await buildSignedActivity(signer, body) + const signedBody = await signAndContextify(signer, body, 'Announce') const headers = buildGlobalHeaders(signedBody) @@ -269,7 +268,7 @@ describe('Test ActivityPub security', function () { body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube' const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' } - const signedBody = await buildSignedActivity(signer, body) + const signedBody = await signAndContextify(signer, body, 'Announce') const headers = buildGlobalHeaders(signedBody) -- cgit v1.2.3