]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/activitypub.ts
Activity Pub improvements
[github/Chocobozzz/PeerTube.git] / server / middlewares / activitypub.ts
index 6cf8eea6f4297dd08f72bff6ef8340806d4b2d96..c7102b6bf7782733330c1c7e29aada3b85bfb5ce 100644 (file)
@@ -1,51 +1,54 @@
-import { Request, Response, NextFunction } from 'express'
-
-import { database as db } from '../initializers'
-import {
-  logger,
-  getAccountFromWebfinger,
-  isSignatureVerified
-} from '../helpers'
+import { eachSeries } from 'async'
+import { NextFunction, Request, RequestHandler, Response } from 'express'
 import { ActivityPubSignature } from '../../shared'
+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 getAccountFromWebfinger(signatureObject.creator)
-
-    if (!account) {
-      return res.sendStatus(403)
-    }
-
-    // Save our new account in database
-    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 = account
+  res.locals.signature = {
+    actor
+  }
 
   return next()
 }
 
-function executeIfActivityPub (fun: any | any[]) {
+function executeIfActivityPub (fun: RequestHandler | RequestHandler[]) {
   return (req: Request, res: Response, next: NextFunction) => {
-    if (req.header('Accept') !== 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"') {
+    const accepted = req.accepts(ACCEPT_HEADERS)
+    console.log(accepted)
+    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) {
-      fun[0](req, res, next) // FIXME: doesn't work
+      return eachSeries(fun as RequestHandler[], (f, cb) => {
+        f(req, res, cb)
+      }, next)
     }
 
-    return fun(req, res, next)
+    return (fun as RequestHandler)(req, res, next)
   }
 }