From e4f97babf701481b55cc10fb3448feab5f97c867 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 9 Nov 2017 17:51:58 +0100 Subject: Begin activitypub --- server/middlewares/validators/account.ts | 53 +++++++++++++++++++ server/middlewares/validators/activitypub/index.ts | 3 ++ server/middlewares/validators/activitypub/pods.ts | 38 ++++++++++++++ .../validators/activitypub/signature.ts | 30 +++++++++++ .../middlewares/validators/activitypub/videos.ts | 61 ++++++++++++++++++++++ server/middlewares/validators/index.ts | 3 +- server/middlewares/validators/remote/index.ts | 3 -- server/middlewares/validators/remote/pods.ts | 38 -------------- server/middlewares/validators/remote/signature.ts | 22 -------- server/middlewares/validators/remote/videos.ts | 61 ---------------------- 10 files changed, 187 insertions(+), 125 deletions(-) create mode 100644 server/middlewares/validators/account.ts create mode 100644 server/middlewares/validators/activitypub/index.ts create mode 100644 server/middlewares/validators/activitypub/pods.ts create mode 100644 server/middlewares/validators/activitypub/signature.ts create mode 100644 server/middlewares/validators/activitypub/videos.ts delete mode 100644 server/middlewares/validators/remote/index.ts delete mode 100644 server/middlewares/validators/remote/pods.ts delete mode 100644 server/middlewares/validators/remote/signature.ts delete mode 100644 server/middlewares/validators/remote/videos.ts (limited to 'server/middlewares/validators') diff --git a/server/middlewares/validators/account.ts b/server/middlewares/validators/account.ts new file mode 100644 index 000000000..5abe942d6 --- /dev/null +++ b/server/middlewares/validators/account.ts @@ -0,0 +1,53 @@ +import { param } from 'express-validator/check' +import * as express from 'express' + +import { database as db } from '../../initializers/database' +import { checkErrors } from './utils' +import { + logger, + isUserUsernameValid, + isUserPasswordValid, + isUserVideoQuotaValid, + isUserDisplayNSFWValid, + isUserRoleValid, + isAccountNameValid +} from '../../helpers' +import { AccountInstance } from '../../models' + +const localAccountValidator = [ + param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking localAccountValidator parameters', { parameters: req.params }) + + checkErrors(req, res, () => { + checkLocalAccountExists(req.params.name, res, next) + }) + } +] + +// --------------------------------------------------------------------------- + +export { + localAccountValidator +} + +// --------------------------------------------------------------------------- + +function checkLocalAccountExists (name: string, res: express.Response, callback: (err: Error, account: AccountInstance) => void) { + db.Account.loadLocalAccountByName(name) + .then(account => { + if (!account) { + return res.status(404) + .send({ error: 'Account not found' }) + .end() + } + + res.locals.account = account + return callback(null, account) + }) + .catch(err => { + logger.error('Error in account request validator.', err) + return res.sendStatus(500) + }) +} diff --git a/server/middlewares/validators/activitypub/index.ts b/server/middlewares/validators/activitypub/index.ts new file mode 100644 index 000000000..f1f26043e --- /dev/null +++ b/server/middlewares/validators/activitypub/index.ts @@ -0,0 +1,3 @@ +export * from './pods' +export * from './signature' +export * from './videos' diff --git a/server/middlewares/validators/activitypub/pods.ts b/server/middlewares/validators/activitypub/pods.ts new file mode 100644 index 000000000..f917b61ee --- /dev/null +++ b/server/middlewares/validators/activitypub/pods.ts @@ -0,0 +1,38 @@ +import { body } from 'express-validator/check' +import * as express from 'express' + +import { database as db } from '../../../initializers' +import { isHostValid, logger } from '../../../helpers' +import { checkErrors } from '../utils' + +const remotePodsAddValidator = [ + body('host').custom(isHostValid).withMessage('Should have a host'), + body('email').isEmail().withMessage('Should have an email'), + body('publicKey').not().isEmpty().withMessage('Should have a public key'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking podsAdd parameters', { parameters: req.body }) + + checkErrors(req, res, () => { + db.Pod.loadByHost(req.body.host) + .then(pod => { + // Pod with this host already exists + if (pod) { + return res.sendStatus(409) + } + + return next() + }) + .catch(err => { + logger.error('Cannot load pod by host.', err) + res.sendStatus(500) + }) + }) + } +] + +// --------------------------------------------------------------------------- + +export { + remotePodsAddValidator +} diff --git a/server/middlewares/validators/activitypub/signature.ts b/server/middlewares/validators/activitypub/signature.ts new file mode 100644 index 000000000..0ce15c1f6 --- /dev/null +++ b/server/middlewares/validators/activitypub/signature.ts @@ -0,0 +1,30 @@ +import { body } from 'express-validator/check' +import * as express from 'express' + +import { + logger, + isDateValid, + isSignatureTypeValid, + isSignatureCreatorValid, + isSignatureValueValid +} from '../../../helpers' +import { checkErrors } from '../utils' + +const signatureValidator = [ + body('signature.type').custom(isSignatureTypeValid).withMessage('Should have a valid signature type'), + body('signature.created').custom(isDateValid).withMessage('Should have a valid signature created date'), + body('signature.creator').custom(isSignatureCreatorValid).withMessage('Should have a valid signature creator'), + body('signature.signatureValue').custom(isSignatureValueValid).withMessage('Should have a valid signature value'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking activitypub signature parameter', { parameters: { signature: req.body.signature } }) + + checkErrors(req, res, next) + } +] + +// --------------------------------------------------------------------------- + +export { + signatureValidator +} diff --git a/server/middlewares/validators/activitypub/videos.ts b/server/middlewares/validators/activitypub/videos.ts new file mode 100644 index 000000000..497320cc1 --- /dev/null +++ b/server/middlewares/validators/activitypub/videos.ts @@ -0,0 +1,61 @@ +import { body } from 'express-validator/check' +import * as express from 'express' + +import { + logger, + isArray, + removeBadRequestVideos, + removeBadRequestVideosQadu, + removeBadRequestVideosEvents +} from '../../../helpers' +import { checkErrors } from '../utils' + +const remoteVideosValidator = [ + body('data').custom(isArray), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking remoteVideos parameters', { parameters: req.body }) + + checkErrors(req, res, () => { + removeBadRequestVideos(req.body.data) + + return next() + }) + } +] + +const remoteQaduVideosValidator = [ + body('data').custom(isArray), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking remoteQaduVideos parameters', { parameters: req.body }) + + checkErrors(req, res, () => { + removeBadRequestVideosQadu(req.body.data) + + return next() + }) + } +] + +const remoteEventsVideosValidator = [ + body('data').custom(isArray), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking remoteEventsVideos parameters', { parameters: req.body }) + + checkErrors(req, res, () => { + removeBadRequestVideosEvents(req.body.data) + + return next() + }) + } +] + +// --------------------------------------------------------------------------- + +export { + remoteVideosValidator, + remoteQaduVideosValidator, + remoteEventsVideosValidator +} diff --git a/server/middlewares/validators/index.ts b/server/middlewares/validators/index.ts index 247f6039e..46c00d679 100644 --- a/server/middlewares/validators/index.ts +++ b/server/middlewares/validators/index.ts @@ -1,5 +1,6 @@ +export * from './account' export * from './oembed' -export * from './remote' +export * from './activitypub' export * from './pagination' export * from './pods' export * from './sort' diff --git a/server/middlewares/validators/remote/index.ts b/server/middlewares/validators/remote/index.ts deleted file mode 100644 index f1f26043e..000000000 --- a/server/middlewares/validators/remote/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from './pods' -export * from './signature' -export * from './videos' diff --git a/server/middlewares/validators/remote/pods.ts b/server/middlewares/validators/remote/pods.ts deleted file mode 100644 index f917b61ee..000000000 --- a/server/middlewares/validators/remote/pods.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { body } from 'express-validator/check' -import * as express from 'express' - -import { database as db } from '../../../initializers' -import { isHostValid, logger } from '../../../helpers' -import { checkErrors } from '../utils' - -const remotePodsAddValidator = [ - body('host').custom(isHostValid).withMessage('Should have a host'), - body('email').isEmail().withMessage('Should have an email'), - body('publicKey').not().isEmpty().withMessage('Should have a public key'), - - (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking podsAdd parameters', { parameters: req.body }) - - checkErrors(req, res, () => { - db.Pod.loadByHost(req.body.host) - .then(pod => { - // Pod with this host already exists - if (pod) { - return res.sendStatus(409) - } - - return next() - }) - .catch(err => { - logger.error('Cannot load pod by host.', err) - res.sendStatus(500) - }) - }) - } -] - -// --------------------------------------------------------------------------- - -export { - remotePodsAddValidator -} diff --git a/server/middlewares/validators/remote/signature.ts b/server/middlewares/validators/remote/signature.ts deleted file mode 100644 index d3937b515..000000000 --- a/server/middlewares/validators/remote/signature.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { body } from 'express-validator/check' -import * as express from 'express' - -import { logger, isHostValid } from '../../../helpers' -import { checkErrors } from '../utils' - -const signatureValidator = [ - body('signature.host').custom(isHostValid).withMessage('Should have a signature host'), - body('signature.signature').not().isEmpty().withMessage('Should have a signature'), - - (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking signature parameters', { parameters: { signature: req.body.signature } }) - - checkErrors(req, res, next) - } -] - -// --------------------------------------------------------------------------- - -export { - signatureValidator -} diff --git a/server/middlewares/validators/remote/videos.ts b/server/middlewares/validators/remote/videos.ts deleted file mode 100644 index 497320cc1..000000000 --- a/server/middlewares/validators/remote/videos.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { body } from 'express-validator/check' -import * as express from 'express' - -import { - logger, - isArray, - removeBadRequestVideos, - removeBadRequestVideosQadu, - removeBadRequestVideosEvents -} from '../../../helpers' -import { checkErrors } from '../utils' - -const remoteVideosValidator = [ - body('data').custom(isArray), - - (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking remoteVideos parameters', { parameters: req.body }) - - checkErrors(req, res, () => { - removeBadRequestVideos(req.body.data) - - return next() - }) - } -] - -const remoteQaduVideosValidator = [ - body('data').custom(isArray), - - (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking remoteQaduVideos parameters', { parameters: req.body }) - - checkErrors(req, res, () => { - removeBadRequestVideosQadu(req.body.data) - - return next() - }) - } -] - -const remoteEventsVideosValidator = [ - body('data').custom(isArray), - - (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking remoteEventsVideos parameters', { parameters: req.body }) - - checkErrors(req, res, () => { - removeBadRequestVideosEvents(req.body.data) - - return next() - }) - } -] - -// --------------------------------------------------------------------------- - -export { - remoteVideosValidator, - remoteQaduVideosValidator, - remoteEventsVideosValidator -} -- cgit v1.2.3