Better AP route checker

This commit is contained in:
Chocobozzz 2019-03-19 09:34:29 +01:00
parent 0f6acda116
commit e65c0c5b1f
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
2 changed files with 77 additions and 63 deletions

View File

@ -45,97 +45,119 @@ import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/vid
const activityPubClientRouter = express.Router() const activityPubClientRouter = express.Router()
activityPubClientRouter.get('/accounts?/:name', activityPubClientRouter.get('/accounts?/:name',
executeIfActivityPub(asyncMiddleware(localAccountValidator)), executeIfActivityPub,
executeIfActivityPub(accountController) asyncMiddleware(localAccountValidator),
accountController
) )
activityPubClientRouter.get('/accounts?/:name/followers', activityPubClientRouter.get('/accounts?/:name/followers',
executeIfActivityPub(asyncMiddleware(localAccountValidator)), executeIfActivityPub,
executeIfActivityPub(asyncMiddleware(accountFollowersController)) asyncMiddleware(localAccountValidator),
asyncMiddleware(accountFollowersController)
) )
activityPubClientRouter.get('/accounts?/:name/following', activityPubClientRouter.get('/accounts?/:name/following',
executeIfActivityPub(asyncMiddleware(localAccountValidator)), executeIfActivityPub,
executeIfActivityPub(asyncMiddleware(accountFollowingController)) asyncMiddleware(localAccountValidator),
asyncMiddleware(accountFollowingController)
) )
activityPubClientRouter.get('/accounts?/:name/playlists', activityPubClientRouter.get('/accounts?/:name/playlists',
executeIfActivityPub(asyncMiddleware(localAccountValidator)), executeIfActivityPub,
executeIfActivityPub(asyncMiddleware(accountPlaylistsController)) asyncMiddleware(localAccountValidator),
asyncMiddleware(accountPlaylistsController)
) )
activityPubClientRouter.get('/accounts?/:name/likes/:videoId', activityPubClientRouter.get('/accounts?/:name/likes/:videoId',
executeIfActivityPub(asyncMiddleware(getAccountVideoRateValidator('like'))), executeIfActivityPub,
executeIfActivityPub(getAccountVideoRate('like')) asyncMiddleware(getAccountVideoRateValidator('like')),
getAccountVideoRate('like')
) )
activityPubClientRouter.get('/accounts?/:name/dislikes/:videoId', activityPubClientRouter.get('/accounts?/:name/dislikes/:videoId',
executeIfActivityPub(asyncMiddleware(getAccountVideoRateValidator('dislike'))), executeIfActivityPub,
executeIfActivityPub(getAccountVideoRate('dislike')) asyncMiddleware(getAccountVideoRateValidator('dislike')),
getAccountVideoRate('dislike')
) )
activityPubClientRouter.get('/videos/watch/:id', activityPubClientRouter.get('/videos/watch/:id',
executeIfActivityPub(asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))), executeIfActivityPub,
executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video-with-rights'))), asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS)),
executeIfActivityPub(asyncMiddleware(videoController)) asyncMiddleware(videosCustomGetValidator('only-video-with-rights')),
asyncMiddleware(videoController)
) )
activityPubClientRouter.get('/videos/watch/:id/activity', activityPubClientRouter.get('/videos/watch/:id/activity',
executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video-with-rights'))), executeIfActivityPub,
executeIfActivityPub(asyncMiddleware(videoController)) asyncMiddleware(videosCustomGetValidator('only-video-with-rights')),
asyncMiddleware(videoController)
) )
activityPubClientRouter.get('/videos/watch/:id/announces', activityPubClientRouter.get('/videos/watch/:id/announces',
executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))), executeIfActivityPub,
executeIfActivityPub(asyncMiddleware(videoAnnouncesController)) asyncMiddleware(videosCustomGetValidator('only-video')),
asyncMiddleware(videoAnnouncesController)
) )
activityPubClientRouter.get('/videos/watch/:id/announces/:actorId', activityPubClientRouter.get('/videos/watch/:id/announces/:actorId',
executeIfActivityPub(asyncMiddleware(videosShareValidator)), executeIfActivityPub,
executeIfActivityPub(asyncMiddleware(videoAnnounceController)) asyncMiddleware(videosShareValidator),
asyncMiddleware(videoAnnounceController)
) )
activityPubClientRouter.get('/videos/watch/:id/likes', activityPubClientRouter.get('/videos/watch/:id/likes',
executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))), executeIfActivityPub,
executeIfActivityPub(asyncMiddleware(videoLikesController)) asyncMiddleware(videosCustomGetValidator('only-video')),
asyncMiddleware(videoLikesController)
) )
activityPubClientRouter.get('/videos/watch/:id/dislikes', activityPubClientRouter.get('/videos/watch/:id/dislikes',
executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))), executeIfActivityPub,
executeIfActivityPub(asyncMiddleware(videoDislikesController)) asyncMiddleware(videosCustomGetValidator('only-video')),
asyncMiddleware(videoDislikesController)
) )
activityPubClientRouter.get('/videos/watch/:id/comments', activityPubClientRouter.get('/videos/watch/:id/comments',
executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))), executeIfActivityPub,
executeIfActivityPub(asyncMiddleware(videoCommentsController)) asyncMiddleware(videosCustomGetValidator('only-video')),
asyncMiddleware(videoCommentsController)
) )
activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId', activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)), executeIfActivityPub,
executeIfActivityPub(asyncMiddleware(videoCommentController)) asyncMiddleware(videoCommentGetValidator),
asyncMiddleware(videoCommentController)
) )
activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity', activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)), executeIfActivityPub,
executeIfActivityPub(asyncMiddleware(videoCommentController)) asyncMiddleware(videoCommentGetValidator),
asyncMiddleware(videoCommentController)
) )
activityPubClientRouter.get('/video-channels/:name', activityPubClientRouter.get('/video-channels/:name',
executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)), executeIfActivityPub,
executeIfActivityPub(asyncMiddleware(videoChannelController)) asyncMiddleware(localVideoChannelValidator),
asyncMiddleware(videoChannelController)
) )
activityPubClientRouter.get('/video-channels/:name/followers', activityPubClientRouter.get('/video-channels/:name/followers',
executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)), executeIfActivityPub,
executeIfActivityPub(asyncMiddleware(videoChannelFollowersController)) asyncMiddleware(localVideoChannelValidator),
asyncMiddleware(videoChannelFollowersController)
) )
activityPubClientRouter.get('/video-channels/:name/following', activityPubClientRouter.get('/video-channels/:name/following',
executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)), executeIfActivityPub,
executeIfActivityPub(asyncMiddleware(videoChannelFollowingController)) asyncMiddleware(localVideoChannelValidator),
asyncMiddleware(videoChannelFollowingController)
) )
activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?', activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?',
executeIfActivityPub(asyncMiddleware(videoFileRedundancyGetValidator)), executeIfActivityPub,
executeIfActivityPub(asyncMiddleware(videoRedundancyController)) asyncMiddleware(videoFileRedundancyGetValidator),
asyncMiddleware(videoRedundancyController)
) )
activityPubClientRouter.get('/redundancy/streaming-playlists/:streamingPlaylistType/:videoId', activityPubClientRouter.get('/redundancy/streaming-playlists/:streamingPlaylistType/:videoId',
executeIfActivityPub(asyncMiddleware(videoPlaylistRedundancyGetValidator)), executeIfActivityPub,
executeIfActivityPub(asyncMiddleware(videoRedundancyController)) asyncMiddleware(videoPlaylistRedundancyGetValidator),
asyncMiddleware(videoRedundancyController)
) )
activityPubClientRouter.get('/video-playlists/:playlistId', activityPubClientRouter.get('/video-playlists/:playlistId',
executeIfActivityPub(asyncMiddleware(videoPlaylistsGetValidator)), executeIfActivityPub,
executeIfActivityPub(asyncMiddleware(videoPlaylistController)) asyncMiddleware(videoPlaylistsGetValidator),
asyncMiddleware(videoPlaylistController)
) )
activityPubClientRouter.get('/video-playlists/:playlistId/:videoId', activityPubClientRouter.get('/video-playlists/:playlistId/:videoId',
executeIfActivityPub(asyncMiddleware(videoPlaylistElementAPGetValidator)), executeIfActivityPub,
executeIfActivityPub(asyncMiddleware(videoPlaylistElementController)) asyncMiddleware(videoPlaylistElementAPGetValidator),
asyncMiddleware(videoPlaylistElementController)
) )
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------

View File

@ -1,5 +1,4 @@
import { eachSeries } from 'async' import { NextFunction, Request, Response } from 'express'
import { NextFunction, Request, RequestHandler, Response } from 'express'
import { ActivityPubSignature } from '../../shared' import { ActivityPubSignature } from '../../shared'
import { logger } from '../helpers/logger' import { logger } from '../helpers/logger'
import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../helpers/peertube-crypto' 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[]) { function executeIfActivityPub (req: Request, res: Response, next: NextFunction) {
return (req: Request, res: Response, next: NextFunction) => { const accepted = req.accepts(ACCEPT_HEADERS)
const accepted = req.accepts(ACCEPT_HEADERS) if (accepted === false || ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS.indexOf(accepted) === -1) {
if (accepted === false || ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS.indexOf(accepted) === -1) { // Bypass this route
return next() return next('route')
}
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)
} }
logger.debug('ActivityPub request for %s.', req.url)
return next()
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------