X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=server%2Fmiddlewares%2Factivitypub.ts;h=12d5c22c54acb15965538cd99bb9573d27e632d5;hb=99492dbc0d87ef54d0dab7d8d44f8d0de5722bdd;hp=8e8a3961b3c5f70c7ef077dac7b54b546cad5022;hpb=9a27cdc27c900feaae5f6db4315c4ccdfc0c4493;p=oweals%2Fpeertube.git diff --git a/server/middlewares/activitypub.ts b/server/middlewares/activitypub.ts index 8e8a3961b..12d5c22c5 100644 --- a/server/middlewares/activitypub.ts +++ b/server/middlewares/activitypub.ts @@ -1,36 +1,32 @@ import { eachSeries } from 'async' import { NextFunction, Request, RequestHandler, Response } from 'express' import { ActivityPubSignature } from '../../shared' -import { isSignatureVerified, logger } from '../helpers' -import { fetchRemoteAccountAndCreateServer } from '../helpers/activitypub' -import { database as db } from '../initializers' -import { ACTIVITY_PUB } from '../initializers/constants' +import { logger } from '../helpers/logger' +import { isSignatureVerified } from '../helpers/peertube-crypto' +import { ACCEPT_HEADERS, ACTIVITY_PUB } from '../initializers' +import { getOrCreateActorAndServerAndModel } from '../lib/activitypub' +import { ActorModel } from '../models/activitypub/actor' async function checkSignature (req: Request, res: Response, next: NextFunction) { const signatureObject: ActivityPubSignature = req.body.signature - logger.debug('Checking signature of account %s...', signatureObject.creator) + const [ creator ] = signatureObject.creator.split('#') - let account = await db.Account.loadByUrl(signatureObject.creator) + logger.debug('Checking signature of actor %s...', creator) - // We don't have this account in our database, fetch it on remote - if (!account) { - const accountResult = await fetchRemoteAccountAndCreateServer(signatureObject.creator) - - if (!accountResult) { - return res.sendStatus(403) - } - - // Save our new account in database - account = accountResult.account - await account.save() + let actor: ActorModel + try { + actor = await getOrCreateActorAndServerAndModel(creator) + } catch (err) { + logger.error('Cannot create remote actor and check signature.', { err }) + return res.sendStatus(403) } - const verified = await isSignatureVerified(account, req.body) + const verified = await isSignatureVerified(actor, req.body) if (verified === false) return res.sendStatus(403) res.locals.signature = { - account + actor } return next() @@ -38,10 +34,13 @@ async function checkSignature (req: Request, res: Response, next: NextFunction) function executeIfActivityPub (fun: RequestHandler | RequestHandler[]) { return (req: Request, res: Response, next: NextFunction) => { - if (req.header('Accept') !== ACTIVITY_PUB.ACCEPT_HEADER) { + const accepted = req.accepts(ACCEPT_HEADERS) + if (accepted === false || ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS.indexOf(accepted) === -1) { return next() } + logger.debug('ActivityPub request for %s.', req.url) + if (Array.isArray(fun) === true) { return eachSeries(fun as RequestHandler[], (f, cb) => { f(req, res, cb)