diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-09 17:51:58 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:51 +0100 |
commit | e4f97babf701481b55cc10fb3448feab5f97c867 (patch) | |
tree | af37402a594dc5ff09f71ecb0687e8cfe4cdb471 /server/middlewares/validators/activitypub | |
parent | 343ad675f2a26c15b86150a9a3552e619d5d44f4 (diff) | |
download | PeerTube-e4f97babf701481b55cc10fb3448feab5f97c867.tar.gz PeerTube-e4f97babf701481b55cc10fb3448feab5f97c867.tar.zst PeerTube-e4f97babf701481b55cc10fb3448feab5f97c867.zip |
Begin activitypub
Diffstat (limited to 'server/middlewares/validators/activitypub')
4 files changed, 132 insertions, 0 deletions
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 @@ | |||
1 | export * from './pods' | ||
2 | export * from './signature' | ||
3 | 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 @@ | |||
1 | import { body } from 'express-validator/check' | ||
2 | import * as express from 'express' | ||
3 | |||
4 | import { database as db } from '../../../initializers' | ||
5 | import { isHostValid, logger } from '../../../helpers' | ||
6 | import { checkErrors } from '../utils' | ||
7 | |||
8 | const remotePodsAddValidator = [ | ||
9 | body('host').custom(isHostValid).withMessage('Should have a host'), | ||
10 | body('email').isEmail().withMessage('Should have an email'), | ||
11 | body('publicKey').not().isEmpty().withMessage('Should have a public key'), | ||
12 | |||
13 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
14 | logger.debug('Checking podsAdd parameters', { parameters: req.body }) | ||
15 | |||
16 | checkErrors(req, res, () => { | ||
17 | db.Pod.loadByHost(req.body.host) | ||
18 | .then(pod => { | ||
19 | // Pod with this host already exists | ||
20 | if (pod) { | ||
21 | return res.sendStatus(409) | ||
22 | } | ||
23 | |||
24 | return next() | ||
25 | }) | ||
26 | .catch(err => { | ||
27 | logger.error('Cannot load pod by host.', err) | ||
28 | res.sendStatus(500) | ||
29 | }) | ||
30 | }) | ||
31 | } | ||
32 | ] | ||
33 | |||
34 | // --------------------------------------------------------------------------- | ||
35 | |||
36 | export { | ||
37 | remotePodsAddValidator | ||
38 | } | ||
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 @@ | |||
1 | import { body } from 'express-validator/check' | ||
2 | import * as express from 'express' | ||
3 | |||
4 | import { | ||
5 | logger, | ||
6 | isDateValid, | ||
7 | isSignatureTypeValid, | ||
8 | isSignatureCreatorValid, | ||
9 | isSignatureValueValid | ||
10 | } from '../../../helpers' | ||
11 | import { checkErrors } from '../utils' | ||
12 | |||
13 | const signatureValidator = [ | ||
14 | body('signature.type').custom(isSignatureTypeValid).withMessage('Should have a valid signature type'), | ||
15 | body('signature.created').custom(isDateValid).withMessage('Should have a valid signature created date'), | ||
16 | body('signature.creator').custom(isSignatureCreatorValid).withMessage('Should have a valid signature creator'), | ||
17 | body('signature.signatureValue').custom(isSignatureValueValid).withMessage('Should have a valid signature value'), | ||
18 | |||
19 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
20 | logger.debug('Checking activitypub signature parameter', { parameters: { signature: req.body.signature } }) | ||
21 | |||
22 | checkErrors(req, res, next) | ||
23 | } | ||
24 | ] | ||
25 | |||
26 | // --------------------------------------------------------------------------- | ||
27 | |||
28 | export { | ||
29 | signatureValidator | ||
30 | } | ||
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 @@ | |||
1 | import { body } from 'express-validator/check' | ||
2 | import * as express from 'express' | ||
3 | |||
4 | import { | ||
5 | logger, | ||
6 | isArray, | ||
7 | removeBadRequestVideos, | ||
8 | removeBadRequestVideosQadu, | ||
9 | removeBadRequestVideosEvents | ||
10 | } from '../../../helpers' | ||
11 | import { checkErrors } from '../utils' | ||
12 | |||
13 | const remoteVideosValidator = [ | ||
14 | body('data').custom(isArray), | ||
15 | |||
16 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
17 | logger.debug('Checking remoteVideos parameters', { parameters: req.body }) | ||
18 | |||
19 | checkErrors(req, res, () => { | ||
20 | removeBadRequestVideos(req.body.data) | ||
21 | |||
22 | return next() | ||
23 | }) | ||
24 | } | ||
25 | ] | ||
26 | |||
27 | const remoteQaduVideosValidator = [ | ||
28 | body('data').custom(isArray), | ||
29 | |||
30 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
31 | logger.debug('Checking remoteQaduVideos parameters', { parameters: req.body }) | ||
32 | |||
33 | checkErrors(req, res, () => { | ||
34 | removeBadRequestVideosQadu(req.body.data) | ||
35 | |||
36 | return next() | ||
37 | }) | ||
38 | } | ||
39 | ] | ||
40 | |||
41 | const remoteEventsVideosValidator = [ | ||
42 | body('data').custom(isArray), | ||
43 | |||
44 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
45 | logger.debug('Checking remoteEventsVideos parameters', { parameters: req.body }) | ||
46 | |||
47 | checkErrors(req, res, () => { | ||
48 | removeBadRequestVideosEvents(req.body.data) | ||
49 | |||
50 | return next() | ||
51 | }) | ||
52 | } | ||
53 | ] | ||
54 | |||
55 | // --------------------------------------------------------------------------- | ||
56 | |||
57 | export { | ||
58 | remoteVideosValidator, | ||
59 | remoteQaduVideosValidator, | ||
60 | remoteEventsVideosValidator | ||
61 | } | ||