X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Factivitypub.ts;h=1488b42ab1164bd701db841182da7d92c7eb0613;hb=5de8a55abce53108bc1024f1194457c6528bd11e;hp=48564572018844996f4347982181be23dad65c38;hpb=1b5b10d13152d704d2396a1e53d56aba1a8e7e03;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/activitypub.ts b/server/middlewares/activitypub.ts index 485645720..1488b42ab 100644 --- a/server/middlewares/activitypub.ts +++ b/server/middlewares/activitypub.ts @@ -1,35 +1,32 @@ import { eachSeries } from 'async' import { NextFunction, Request, RequestHandler, Response } from 'express' import { ActivityPubSignature } from '../../shared' -import { isSignatureVerified, logger } from '../helpers' -import { database as db } from '../initializers' -import { ACTIVITY_PUB } from '../initializers/constants' -import { fetchRemoteAccount, saveAccountAndServerIfNotExist } from '../lib/activitypub/account' +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) { - account = await fetchRemoteAccount(signatureObject.creator) - - if (!account) { - return res.sendStatus(403) - } - - // Save our new account and its server in database - await saveAccountAndServerIfNotExist(account) + 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() @@ -37,7 +34,8 @@ async function checkSignature (req: Request, res: Response, next: NextFunction) function executeIfActivityPub (fun: RequestHandler | RequestHandler[]) { return (req: Request, res: Response, next: NextFunction) => { - if (!req.accepts(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)) { + const accepted = req.accepts(ACCEPT_HEADERS) + if (accepted === false || ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS.indexOf(accepted) === -1) { return next() }