aboutsummaryrefslogtreecommitdiffhomepage
path: root/middlewares/reqValidators
diff options
context:
space:
mode:
Diffstat (limited to 'middlewares/reqValidators')
-rw-r--r--middlewares/reqValidators/index.js18
-rw-r--r--middlewares/reqValidators/pods.js80
-rw-r--r--middlewares/reqValidators/remote.js62
-rw-r--r--middlewares/reqValidators/utils.js36
-rw-r--r--middlewares/reqValidators/videos.js106
5 files changed, 146 insertions, 156 deletions
diff --git a/middlewares/reqValidators/index.js b/middlewares/reqValidators/index.js
index 34d34013c..344387a80 100644
--- a/middlewares/reqValidators/index.js
+++ b/middlewares/reqValidators/index.js
@@ -1,13 +1,11 @@
1;(function () { 1'use strict'
2 'use strict'
3 2
4 var reqValidators = { 3var reqValidators = {
5 videos: require('./videos'), 4 videos: require('./videos'),
6 pods: require('./pods'), 5 pods: require('./pods'),
7 remote: require('./remote') 6 remote: require('./remote')
8 } 7}
9 8
10 // --------------------------------------------------------------------------- 9// ---------------------------------------------------------------------------
11 10
12 module.exports = reqValidators 11module.exports = reqValidators
13})()
diff --git a/middlewares/reqValidators/pods.js b/middlewares/reqValidators/pods.js
index 4d649b486..7d1612dde 100644
--- a/middlewares/reqValidators/pods.js
+++ b/middlewares/reqValidators/pods.js
@@ -1,41 +1,39 @@
1;(function () { 1'use strict'
2 'use strict' 2
3 3var checkErrors = require('./utils').checkErrors
4 var checkErrors = require('./utils').checkErrors 4var friends = require('../../lib/friends')
5 var friends = require('../../lib/friends') 5var logger = require('../../helpers/logger')
6 var logger = require('../../helpers/logger') 6
7 7var reqValidatorsPod = {
8 var reqValidatorsPod = { 8 makeFriends: makeFriends,
9 makeFriends: makeFriends, 9 podsAdd: podsAdd
10 podsAdd: podsAdd 10}
11 } 11
12 12function makeFriends (req, res, next) {
13 function makeFriends (req, res, next) { 13 friends.hasFriends(function (err, has_friends) {
14 friends.hasFriends(function (err, has_friends) { 14 if (err) {
15 if (err) { 15 logger.error('Cannot know if we have friends.', { error: err })
16 logger.error('Cannot know if we have friends.', { error: err }) 16 res.sendStatus(500)
17 res.sendStatus(500) 17 }
18 } 18
19 19 if (has_friends === true) {
20 if (has_friends === true) { 20 // We need to quit our friends before make new ones
21 // We need to quit our friends before make new ones 21 res.sendStatus(409)
22 res.sendStatus(409) 22 } else {
23 } else { 23 next()
24 next() 24 }
25 } 25 })
26 }) 26}
27 } 27
28 28function podsAdd (req, res, next) {
29 function podsAdd (req, res, next) { 29 req.checkBody('data.url', 'Should have an url').notEmpty().isURL({ require_protocol: true })
30 req.checkBody('data.url', 'Should have an url').notEmpty().isURL({ require_protocol: true }) 30 req.checkBody('data.publicKey', 'Should have a public key').notEmpty()
31 req.checkBody('data.publicKey', 'Should have a public key').notEmpty() 31
32 32 logger.debug('Checking podsAdd parameters', { parameters: req.body })
33 logger.debug('Checking podsAdd parameters', { parameters: req.body }) 33
34 34 checkErrors(req, res, next)
35 checkErrors(req, res, next) 35}
36 } 36
37 37// ---------------------------------------------------------------------------
38 // --------------------------------------------------------------------------- 38
39 39module.exports = reqValidatorsPod
40 module.exports = reqValidatorsPod
41})()
diff --git a/middlewares/reqValidators/remote.js b/middlewares/reqValidators/remote.js
index 9b61481ad..88de16b49 100644
--- a/middlewares/reqValidators/remote.js
+++ b/middlewares/reqValidators/remote.js
@@ -1,45 +1,43 @@
1;(function () { 1'use strict'
2 'use strict'
3 2
4 var checkErrors = require('./utils').checkErrors 3var checkErrors = require('./utils').checkErrors
5 var logger = require('../../helpers/logger') 4var logger = require('../../helpers/logger')
6 5
7 var reqValidatorsRemote = { 6var reqValidatorsRemote = {
8 remoteVideosAdd: remoteVideosAdd, 7 remoteVideosAdd: remoteVideosAdd,
9 remoteVideosRemove: remoteVideosRemove, 8 remoteVideosRemove: remoteVideosRemove,
10 secureRequest: secureRequest 9 secureRequest: secureRequest
11 } 10}
12 11
13 function remoteVideosAdd (req, res, next) { 12function remoteVideosAdd (req, res, next) {
14 req.checkBody('data').isArray() 13 req.checkBody('data').isArray()
15 req.checkBody('data').eachIsRemoteVideosAddValid() 14 req.checkBody('data').eachIsRemoteVideosAddValid()
16 15
17 logger.debug('Checking remoteVideosAdd parameters', { parameters: req.body }) 16 logger.debug('Checking remoteVideosAdd parameters', { parameters: req.body })
18 17
19 checkErrors(req, res, next) 18 checkErrors(req, res, next)
20 } 19}
21 20
22 function remoteVideosRemove (req, res, next) { 21function remoteVideosRemove (req, res, next) {
23 req.checkBody('data').isArray() 22 req.checkBody('data').isArray()
24 req.checkBody('data').eachIsRemoteVideosRemoveValid() 23 req.checkBody('data').eachIsRemoteVideosRemoveValid()
25 24
26 logger.debug('Checking remoteVideosRemove parameters', { parameters: req.body }) 25 logger.debug('Checking remoteVideosRemove parameters', { parameters: req.body })
27 26
28 checkErrors(req, res, next) 27 checkErrors(req, res, next)
29 } 28}
30 29
31 function secureRequest (req, res, next) { 30function secureRequest (req, res, next) {
32 req.checkBody('signature.url', 'Should have a signature url').isURL() 31 req.checkBody('signature.url', 'Should have a signature url').isURL()
33 req.checkBody('signature.signature', 'Should have a signature').notEmpty() 32 req.checkBody('signature.signature', 'Should have a signature').notEmpty()
34 req.checkBody('key', 'Should have a key').notEmpty() 33 req.checkBody('key', 'Should have a key').notEmpty()
35 req.checkBody('data', 'Should have data').notEmpty() 34 req.checkBody('data', 'Should have data').notEmpty()
36 35
37 logger.debug('Checking secureRequest parameters', { parameters: { data: req.body.data, keyLength: req.body.key.length } }) 36 logger.debug('Checking secureRequest parameters', { parameters: { data: req.body.data, keyLength: req.body.key.length } })
38 37
39 checkErrors(req, res, next) 38 checkErrors(req, res, next)
40 } 39}
41 40
42 // --------------------------------------------------------------------------- 41// ---------------------------------------------------------------------------
43 42
44 module.exports = reqValidatorsRemote 43module.exports = reqValidatorsRemote
45})()
diff --git a/middlewares/reqValidators/utils.js b/middlewares/reqValidators/utils.js
index c88f6df2e..46c982571 100644
--- a/middlewares/reqValidators/utils.js
+++ b/middlewares/reqValidators/utils.js
@@ -1,27 +1,25 @@
1;(function () { 1'use strict'
2 'use strict'
3 2
4 var util = require('util') 3var util = require('util')
5 4
6 var logger = require('../../helpers/logger') 5var logger = require('../../helpers/logger')
7 6
8 var reqValidatorsUtils = { 7var reqValidatorsUtils = {
9 checkErrors: checkErrors 8 checkErrors: checkErrors
10 } 9}
11
12 function checkErrors (req, res, next, status_code) {
13 if (status_code === undefined) status_code = 400
14 var errors = req.validationErrors()
15 10
16 if (errors) { 11function checkErrors (req, res, next, status_code) {
17 logger.warn('Incorrect request parameters', { path: req.originalUrl, err: errors }) 12 if (status_code === undefined) status_code = 400
18 return res.status(status_code).send('There have been validation errors: ' + util.inspect(errors)) 13 var errors = req.validationErrors()
19 }
20 14
21 return next() 15 if (errors) {
16 logger.warn('Incorrect request parameters', { path: req.originalUrl, err: errors })
17 return res.status(status_code).send('There have been validation errors: ' + util.inspect(errors))
22 } 18 }
23 19
24 // --------------------------------------------------------------------------- 20 return next()
21}
22
23// ---------------------------------------------------------------------------
25 24
26 module.exports = reqValidatorsUtils 25module.exports = reqValidatorsUtils
27})()
diff --git a/middlewares/reqValidators/videos.js b/middlewares/reqValidators/videos.js
index f7bd24658..4e5f4391f 100644
--- a/middlewares/reqValidators/videos.js
+++ b/middlewares/reqValidators/videos.js
@@ -1,76 +1,74 @@
1;(function () { 1'use strict'
2 'use strict'
3 2
4 var checkErrors = require('./utils').checkErrors 3var checkErrors = require('./utils').checkErrors
5 var logger = require('../../helpers/logger') 4var logger = require('../../helpers/logger')
6 var Videos = require('../../models/videos') 5var Videos = require('../../models/videos')
7 6
8 var reqValidatorsVideos = { 7var reqValidatorsVideos = {
9 videosAdd: videosAdd, 8 videosAdd: videosAdd,
10 videosGet: videosGet, 9 videosGet: videosGet,
11 videosRemove: videosRemove, 10 videosRemove: videosRemove,
12 videosSearch: videosSearch 11 videosSearch: videosSearch
13 } 12}
14 13
15 function videosAdd (req, res, next) { 14function videosAdd (req, res, next) {
16 req.checkFiles('input_video[0].originalname', 'Should have an input video').notEmpty() 15 req.checkFiles('input_video[0].originalname', 'Should have an input video').notEmpty()
17 req.checkFiles('input_video[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i) 16 req.checkFiles('input_video[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i)
18 req.checkBody('name', 'Should have a name').isLength(1, 50) 17 req.checkBody('name', 'Should have a name').isLength(1, 50)
19 req.checkBody('description', 'Should have a description').isLength(1, 250) 18 req.checkBody('description', 'Should have a description').isLength(1, 250)
20 19
21 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files }) 20 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
22 21
23 checkErrors(req, res, next) 22 checkErrors(req, res, next)
24 } 23}
25 24
26 function videosGet (req, res, next) { 25function videosGet (req, res, next) {
27 req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId() 26 req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId()
28 27
29 logger.debug('Checking videosGet parameters', { parameters: req.params }) 28 logger.debug('Checking videosGet parameters', { parameters: req.params })
30 29
31 checkErrors(req, res, function () { 30 checkErrors(req, res, function () {
32 Videos.getVideoState(req.params.id, function (err, state) { 31 Videos.getVideoState(req.params.id, function (err, state) {
33 if (err) { 32 if (err) {
34 logger.error('Error in videosGet request validator.', { error: err }) 33 logger.error('Error in videosGet request validator.', { error: err })
35 res.sendStatus(500) 34 res.sendStatus(500)
36 } 35 }
37 36
38 if (state.exist === false) return res.status(404).send('Video not found') 37 if (state.exist === false) return res.status(404).send('Video not found')
39 38
40 next() 39 next()
41 })
42 }) 40 })
43 } 41 })
42}
44 43
45 function videosRemove (req, res, next) { 44function videosRemove (req, res, next) {
46 req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId() 45 req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId()
47 46
48 logger.debug('Checking videosRemove parameters', { parameters: req.params }) 47 logger.debug('Checking videosRemove parameters', { parameters: req.params })
49 48
50 checkErrors(req, res, function () { 49 checkErrors(req, res, function () {
51 Videos.getVideoState(req.params.id, function (err, state) { 50 Videos.getVideoState(req.params.id, function (err, state) {
52 if (err) { 51 if (err) {
53 logger.error('Error in videosRemove request validator.', { error: err }) 52 logger.error('Error in videosRemove request validator.', { error: err })
54 res.sendStatus(500) 53 res.sendStatus(500)
55 } 54 }
56 55
57 if (state.exist === false) return res.status(404).send('Video not found') 56 if (state.exist === false) return res.status(404).send('Video not found')
58 else if (state.owned === false) return res.status(403).send('Cannot remove video of another pod') 57 else if (state.owned === false) return res.status(403).send('Cannot remove video of another pod')
59 58
60 next() 59 next()
61 })
62 }) 60 })
63 } 61 })
62}
64 63
65 function videosSearch (req, res, next) { 64function videosSearch (req, res, next) {
66 req.checkParams('name', 'Should have a name').notEmpty() 65 req.checkParams('name', 'Should have a name').notEmpty()
67 66
68 logger.debug('Checking videosSearch parameters', { parameters: req.params }) 67 logger.debug('Checking videosSearch parameters', { parameters: req.params })
69 68
70 checkErrors(req, res, next) 69 checkErrors(req, res, next)
71 } 70}
72 71
73 // --------------------------------------------------------------------------- 72// ---------------------------------------------------------------------------
74 73
75 module.exports = reqValidatorsVideos 74module.exports = reqValidatorsVideos
76})()