From e65c0c5b1fab9c3d93f51721b2458cf5cf471f20 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 19 Mar 2019 09:34:29 +0100 Subject: Better AP route checker --- server/controllers/activitypub/client.ts | 112 ++++++++++++++++++------------- server/middlewares/activitypub.ts | 26 +++---- 2 files changed, 76 insertions(+), 62 deletions(-) (limited to 'server') diff --git a/server/controllers/activitypub/client.ts b/server/controllers/activitypub/client.ts index f1217f6db..cc2671fc1 100644 --- a/server/controllers/activitypub/client.ts +++ b/server/controllers/activitypub/client.ts @@ -45,97 +45,119 @@ import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/vid const activityPubClientRouter = express.Router() activityPubClientRouter.get('/accounts?/:name', - executeIfActivityPub(asyncMiddleware(localAccountValidator)), - executeIfActivityPub(accountController) + executeIfActivityPub, + asyncMiddleware(localAccountValidator), + accountController ) activityPubClientRouter.get('/accounts?/:name/followers', - executeIfActivityPub(asyncMiddleware(localAccountValidator)), - executeIfActivityPub(asyncMiddleware(accountFollowersController)) + executeIfActivityPub, + asyncMiddleware(localAccountValidator), + asyncMiddleware(accountFollowersController) ) activityPubClientRouter.get('/accounts?/:name/following', - executeIfActivityPub(asyncMiddleware(localAccountValidator)), - executeIfActivityPub(asyncMiddleware(accountFollowingController)) + executeIfActivityPub, + asyncMiddleware(localAccountValidator), + asyncMiddleware(accountFollowingController) ) activityPubClientRouter.get('/accounts?/:name/playlists', - executeIfActivityPub(asyncMiddleware(localAccountValidator)), - executeIfActivityPub(asyncMiddleware(accountPlaylistsController)) + executeIfActivityPub, + asyncMiddleware(localAccountValidator), + asyncMiddleware(accountPlaylistsController) ) activityPubClientRouter.get('/accounts?/:name/likes/:videoId', - executeIfActivityPub(asyncMiddleware(getAccountVideoRateValidator('like'))), - executeIfActivityPub(getAccountVideoRate('like')) + executeIfActivityPub, + asyncMiddleware(getAccountVideoRateValidator('like')), + getAccountVideoRate('like') ) activityPubClientRouter.get('/accounts?/:name/dislikes/:videoId', - executeIfActivityPub(asyncMiddleware(getAccountVideoRateValidator('dislike'))), - executeIfActivityPub(getAccountVideoRate('dislike')) + executeIfActivityPub, + asyncMiddleware(getAccountVideoRateValidator('dislike')), + getAccountVideoRate('dislike') ) activityPubClientRouter.get('/videos/watch/:id', - executeIfActivityPub(asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))), - executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video-with-rights'))), - executeIfActivityPub(asyncMiddleware(videoController)) + executeIfActivityPub, + asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS)), + asyncMiddleware(videosCustomGetValidator('only-video-with-rights')), + asyncMiddleware(videoController) ) activityPubClientRouter.get('/videos/watch/:id/activity', - executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video-with-rights'))), - executeIfActivityPub(asyncMiddleware(videoController)) + executeIfActivityPub, + asyncMiddleware(videosCustomGetValidator('only-video-with-rights')), + asyncMiddleware(videoController) ) activityPubClientRouter.get('/videos/watch/:id/announces', - executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))), - executeIfActivityPub(asyncMiddleware(videoAnnouncesController)) + executeIfActivityPub, + asyncMiddleware(videosCustomGetValidator('only-video')), + asyncMiddleware(videoAnnouncesController) ) activityPubClientRouter.get('/videos/watch/:id/announces/:actorId', - executeIfActivityPub(asyncMiddleware(videosShareValidator)), - executeIfActivityPub(asyncMiddleware(videoAnnounceController)) + executeIfActivityPub, + asyncMiddleware(videosShareValidator), + asyncMiddleware(videoAnnounceController) ) activityPubClientRouter.get('/videos/watch/:id/likes', - executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))), - executeIfActivityPub(asyncMiddleware(videoLikesController)) + executeIfActivityPub, + asyncMiddleware(videosCustomGetValidator('only-video')), + asyncMiddleware(videoLikesController) ) activityPubClientRouter.get('/videos/watch/:id/dislikes', - executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))), - executeIfActivityPub(asyncMiddleware(videoDislikesController)) + executeIfActivityPub, + asyncMiddleware(videosCustomGetValidator('only-video')), + asyncMiddleware(videoDislikesController) ) activityPubClientRouter.get('/videos/watch/:id/comments', - executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))), - executeIfActivityPub(asyncMiddleware(videoCommentsController)) + executeIfActivityPub, + asyncMiddleware(videosCustomGetValidator('only-video')), + asyncMiddleware(videoCommentsController) ) activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId', - executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)), - executeIfActivityPub(asyncMiddleware(videoCommentController)) + executeIfActivityPub, + asyncMiddleware(videoCommentGetValidator), + asyncMiddleware(videoCommentController) ) activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity', - executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)), - executeIfActivityPub(asyncMiddleware(videoCommentController)) + executeIfActivityPub, + asyncMiddleware(videoCommentGetValidator), + asyncMiddleware(videoCommentController) ) activityPubClientRouter.get('/video-channels/:name', - executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)), - executeIfActivityPub(asyncMiddleware(videoChannelController)) + executeIfActivityPub, + asyncMiddleware(localVideoChannelValidator), + asyncMiddleware(videoChannelController) ) activityPubClientRouter.get('/video-channels/:name/followers', - executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)), - executeIfActivityPub(asyncMiddleware(videoChannelFollowersController)) + executeIfActivityPub, + asyncMiddleware(localVideoChannelValidator), + asyncMiddleware(videoChannelFollowersController) ) activityPubClientRouter.get('/video-channels/:name/following', - executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)), - executeIfActivityPub(asyncMiddleware(videoChannelFollowingController)) + executeIfActivityPub, + asyncMiddleware(localVideoChannelValidator), + asyncMiddleware(videoChannelFollowingController) ) activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?', - executeIfActivityPub(asyncMiddleware(videoFileRedundancyGetValidator)), - executeIfActivityPub(asyncMiddleware(videoRedundancyController)) + executeIfActivityPub, + asyncMiddleware(videoFileRedundancyGetValidator), + asyncMiddleware(videoRedundancyController) ) activityPubClientRouter.get('/redundancy/streaming-playlists/:streamingPlaylistType/:videoId', - executeIfActivityPub(asyncMiddleware(videoPlaylistRedundancyGetValidator)), - executeIfActivityPub(asyncMiddleware(videoRedundancyController)) + executeIfActivityPub, + asyncMiddleware(videoPlaylistRedundancyGetValidator), + asyncMiddleware(videoRedundancyController) ) activityPubClientRouter.get('/video-playlists/:playlistId', - executeIfActivityPub(asyncMiddleware(videoPlaylistsGetValidator)), - executeIfActivityPub(asyncMiddleware(videoPlaylistController)) + executeIfActivityPub, + asyncMiddleware(videoPlaylistsGetValidator), + asyncMiddleware(videoPlaylistController) ) activityPubClientRouter.get('/video-playlists/:playlistId/:videoId', - executeIfActivityPub(asyncMiddleware(videoPlaylistElementAPGetValidator)), - executeIfActivityPub(asyncMiddleware(videoPlaylistElementController)) + executeIfActivityPub, + asyncMiddleware(videoPlaylistElementAPGetValidator), + asyncMiddleware(videoPlaylistElementController) ) // --------------------------------------------------------------------------- diff --git a/server/middlewares/activitypub.ts b/server/middlewares/activitypub.ts index 01e5dd24e..5fa10cbfd 100644 --- a/server/middlewares/activitypub.ts +++ b/server/middlewares/activitypub.ts @@ -1,5 +1,4 @@ -import { eachSeries } from 'async' -import { NextFunction, Request, RequestHandler, Response } from 'express' +import { NextFunction, Request, Response } from 'express' import { ActivityPubSignature } from '../../shared' import { logger } from '../helpers/logger' import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../helpers/peertube-crypto' @@ -30,23 +29,16 @@ async function checkSignature (req: Request, res: Response, next: NextFunction) } } -function executeIfActivityPub (fun: RequestHandler | RequestHandler[]) { - return (req: Request, res: Response, next: NextFunction) => { - const accepted = req.accepts(ACCEPT_HEADERS) - if (accepted === false || ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS.indexOf(accepted) === -1) { - return next() - } +function executeIfActivityPub (req: Request, res: Response, next: NextFunction) { + const accepted = req.accepts(ACCEPT_HEADERS) + if (accepted === false || ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS.indexOf(accepted) === -1) { + // Bypass this route + return next('route') + } - logger.debug('ActivityPub request for %s.', req.url) + logger.debug('ActivityPub request for %s.', req.url) - if (Array.isArray(fun) === true) { - return eachSeries(fun as RequestHandler[], (f, cb) => { - f(req, res, cb) - }, next) - } - - return (fun as RequestHandler)(req, res, next) - } + return next() } // --------------------------------------------------------------------------- -- cgit v1.2.3