aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators
diff options
context:
space:
mode:
Diffstat (limited to 'server/middlewares/validators')
-rw-r--r--server/middlewares/validators/index.js2
-rw-r--r--server/middlewares/validators/pagination.js2
-rw-r--r--server/middlewares/validators/pods.js34
-rw-r--r--server/middlewares/validators/remote.js7
-rw-r--r--server/middlewares/validators/sort.js13
-rw-r--r--server/middlewares/validators/users.js67
-rw-r--r--server/middlewares/validators/utils.js2
-rw-r--r--server/middlewares/validators/videos.js16
8 files changed, 115 insertions, 28 deletions
diff --git a/server/middlewares/validators/index.js b/server/middlewares/validators/index.js
index 0471b3f92..6c3a9c2b4 100644
--- a/server/middlewares/validators/index.js
+++ b/server/middlewares/validators/index.js
@@ -4,6 +4,7 @@ const paginationValidators = require('./pagination')
4const podsValidators = require('./pods') 4const podsValidators = require('./pods')
5const remoteValidators = require('./remote') 5const remoteValidators = require('./remote')
6const sortValidators = require('./sort') 6const sortValidators = require('./sort')
7const usersValidators = require('./users')
7const videosValidators = require('./videos') 8const videosValidators = require('./videos')
8 9
9const validators = { 10const validators = {
@@ -11,6 +12,7 @@ const validators = {
11 pods: podsValidators, 12 pods: podsValidators,
12 remote: remoteValidators, 13 remote: remoteValidators,
13 sort: sortValidators, 14 sort: sortValidators,
15 users: usersValidators,
14 videos: videosValidators 16 videos: videosValidators
15} 17}
16 18
diff --git a/server/middlewares/validators/pagination.js b/server/middlewares/validators/pagination.js
index 8e9a01053..16682696e 100644
--- a/server/middlewares/validators/pagination.js
+++ b/server/middlewares/validators/pagination.js
@@ -4,7 +4,7 @@ const checkErrors = require('./utils').checkErrors
4const logger = require('../../helpers/logger') 4const logger = require('../../helpers/logger')
5 5
6const validatorsPagination = { 6const validatorsPagination = {
7 pagination: pagination 7 pagination
8} 8}
9 9
10function pagination (req, res, next) { 10function pagination (req, res, next) {
diff --git a/server/middlewares/validators/pods.js b/server/middlewares/validators/pods.js
index fda2e865f..fd3d1e2f2 100644
--- a/server/middlewares/validators/pods.js
+++ b/server/middlewares/validators/pods.js
@@ -5,23 +5,29 @@ const friends = require('../../lib/friends')
5const logger = require('../../helpers/logger') 5const logger = require('../../helpers/logger')
6 6
7const validatorsPod = { 7const validatorsPod = {
8 makeFriends: makeFriends, 8 makeFriends,
9 podsAdd: podsAdd 9 podsAdd
10} 10}
11 11
12function makeFriends (req, res, next) { 12function makeFriends (req, res, next) {
13 friends.hasFriends(function (err, hasFriends) { 13 req.checkBody('urls', 'Should have an array of unique urls').isEachUniqueUrlValid()
14 if (err) { 14
15 logger.error('Cannot know if we have friends.', { error: err }) 15 logger.debug('Checking makeFriends parameters', { parameters: req.body })
16 res.sendStatus(500) 16
17 } 17 checkErrors(req, res, function () {
18 18 friends.hasFriends(function (err, hasFriends) {
19 if (hasFriends === true) { 19 if (err) {
20 // We need to quit our friends before make new ones 20 logger.error('Cannot know if we have friends.', { error: err })
21 res.sendStatus(409) 21 res.sendStatus(500)
22 } else { 22 }
23 return next() 23
24 } 24 if (hasFriends === true) {
25 // We need to quit our friends before make new ones
26 res.sendStatus(409)
27 } else {
28 return next()
29 }
30 })
25 }) 31 })
26} 32}
27 33
diff --git a/server/middlewares/validators/remote.js b/server/middlewares/validators/remote.js
index 1be119458..8c29ef8ca 100644
--- a/server/middlewares/validators/remote.js
+++ b/server/middlewares/validators/remote.js
@@ -4,9 +4,9 @@ const checkErrors = require('./utils').checkErrors
4const logger = require('../../helpers/logger') 4const logger = require('../../helpers/logger')
5 5
6const validatorsRemote = { 6const validatorsRemote = {
7 dataToDecrypt: dataToDecrypt, 7 dataToDecrypt,
8 remoteVideos: remoteVideos, 8 remoteVideos,
9 signature: signature 9 signature
10} 10}
11 11
12function dataToDecrypt (req, res, next) { 12function dataToDecrypt (req, res, next) {
@@ -19,7 +19,6 @@ function dataToDecrypt (req, res, next) {
19} 19}
20 20
21function remoteVideos (req, res, next) { 21function remoteVideos (req, res, next) {
22 req.checkBody('data').isArray()
23 req.checkBody('data').isEachRemoteVideosValid() 22 req.checkBody('data').isEachRemoteVideosValid()
24 23
25 logger.debug('Checking remoteVideos parameters', { parameters: req.body }) 24 logger.debug('Checking remoteVideos parameters', { parameters: req.body })
diff --git a/server/middlewares/validators/sort.js b/server/middlewares/validators/sort.js
index 56b63cc8b..431d3fffd 100644
--- a/server/middlewares/validators/sort.js
+++ b/server/middlewares/validators/sort.js
@@ -5,7 +5,18 @@ const constants = require('../../initializers/constants')
5const logger = require('../../helpers/logger') 5const logger = require('../../helpers/logger')
6 6
7const validatorsSort = { 7const validatorsSort = {
8 videosSort: videosSort 8 usersSort,
9 videosSort
10}
11
12function usersSort (req, res, next) {
13 const sortableColumns = constants.SORTABLE_COLUMNS.USERS
14
15 req.checkQuery('sort', 'Should have correct sortable column').optional().isIn(sortableColumns)
16
17 logger.debug('Checking sort parameters', { parameters: req.query })
18
19 checkErrors(req, res, next)
9} 20}
10 21
11function videosSort (req, res, next) { 22function videosSort (req, res, next) {
diff --git a/server/middlewares/validators/users.js b/server/middlewares/validators/users.js
new file mode 100644
index 000000000..d541e9124
--- /dev/null
+++ b/server/middlewares/validators/users.js
@@ -0,0 +1,67 @@
1'use strict'
2
3const mongoose = require('mongoose')
4
5const checkErrors = require('./utils').checkErrors
6const logger = require('../../helpers/logger')
7
8const User = mongoose.model('User')
9
10const validatorsUsers = {
11 usersAdd,
12 usersRemove,
13 usersUpdate
14}
15
16function usersAdd (req, res, next) {
17 req.checkBody('username', 'Should have a valid username').isUserUsernameValid()
18 req.checkBody('password', 'Should have a valid password').isUserPasswordValid()
19
20 logger.debug('Checking usersAdd parameters', { parameters: req.body })
21
22 checkErrors(req, res, function () {
23 User.loadByUsername(req.body.username, function (err, user) {
24 if (err) {
25 logger.error('Error in usersAdd request validator.', { error: err })
26 return res.sendStatus(500)
27 }
28
29 if (user) return res.status(409).send('User already exists.')
30
31 next()
32 })
33 })
34}
35
36function usersRemove (req, res, next) {
37 req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId()
38
39 logger.debug('Checking usersRemove parameters', { parameters: req.params })
40
41 checkErrors(req, res, function () {
42 User.loadById(req.params.id, function (err, user) {
43 if (err) {
44 logger.error('Error in usersRemove request validator.', { error: err })
45 return res.sendStatus(500)
46 }
47
48 if (!user) return res.status(404).send('User not found')
49
50 next()
51 })
52 })
53}
54
55function usersUpdate (req, res, next) {
56 req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId()
57 // Add old password verification
58 req.checkBody('password', 'Should have a valid password').isUserPasswordValid()
59
60 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
61
62 checkErrors(req, res, next)
63}
64
65// ---------------------------------------------------------------------------
66
67module.exports = validatorsUsers
diff --git a/server/middlewares/validators/utils.js b/server/middlewares/validators/utils.js
index f6e5b2b38..3741b84c6 100644
--- a/server/middlewares/validators/utils.js
+++ b/server/middlewares/validators/utils.js
@@ -5,7 +5,7 @@ const util = require('util')
5const logger = require('../../helpers/logger') 5const logger = require('../../helpers/logger')
6 6
7const validatorsUtils = { 7const validatorsUtils = {
8 checkErrors: checkErrors 8 checkErrors
9} 9}
10 10
11function checkErrors (req, res, next, statusCode) { 11function checkErrors (req, res, next, statusCode) {
diff --git a/server/middlewares/validators/videos.js b/server/middlewares/validators/videos.js
index 3e2af06fb..76e943e77 100644
--- a/server/middlewares/validators/videos.js
+++ b/server/middlewares/validators/videos.js
@@ -4,20 +4,21 @@ const mongoose = require('mongoose')
4 4
5const checkErrors = require('./utils').checkErrors 5const checkErrors = require('./utils').checkErrors
6const constants = require('../../initializers/constants') 6const constants = require('../../initializers/constants')
7const customValidators = require('../../helpers/custom-validators') 7const customVideosValidators = require('../../helpers/custom-validators').videos
8const logger = require('../../helpers/logger') 8const logger = require('../../helpers/logger')
9 9
10const Video = mongoose.model('Video') 10const Video = mongoose.model('Video')
11 11
12const validatorsVideos = { 12const validatorsVideos = {
13 videosAdd: videosAdd, 13 videosAdd,
14 videosGet: videosGet, 14 videosGet,
15 videosRemove: videosRemove, 15 videosRemove,
16 videosSearch: videosSearch 16 videosSearch
17} 17}
18 18
19function videosAdd (req, res, next) { 19function videosAdd (req, res, next) {
20 req.checkFiles('videofile[0].originalname', 'Should have an input video').notEmpty() 20 req.checkFiles('videofile[0].originalname', 'Should have an input video').notEmpty()
21 // TODO: move to constants and function
21 req.checkFiles('videofile[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i) 22 req.checkFiles('videofile[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i)
22 req.checkBody('name', 'Should have a valid name').isVideoNameValid() 23 req.checkBody('name', 'Should have a valid name').isVideoNameValid()
23 req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid() 24 req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid()
@@ -33,8 +34,8 @@ function videosAdd (req, res, next) {
33 return res.status(400).send('Cannot retrieve metadata of the file.') 34 return res.status(400).send('Cannot retrieve metadata of the file.')
34 } 35 }
35 36
36 if (!customValidators.isVideoDurationValid(duration)) { 37 if (!customVideosValidators.isVideoDurationValid(duration)) {
37 return res.status(400).send('Duration of the video file is too big (max: ' + constants.VIDEOS_CONSTRAINTS_FIELDS.DURATION.max + 's).') 38 return res.status(400).send('Duration of the video file is too big (max: ' + constants.CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).')
38 } 39 }
39 40
40 videoFile.duration = duration 41 videoFile.duration = duration
@@ -76,6 +77,7 @@ function videosRemove (req, res, next) {
76 77
77 if (!video) return res.status(404).send('Video not found') 78 if (!video) return res.status(404).send('Video not found')
78 else if (video.isOwned() === false) return res.status(403).send('Cannot remove video of another pod') 79 else if (video.isOwned() === false) return res.status(403).send('Cannot remove video of another pod')
80 else if (video.author !== res.locals.oauth.token.user.username) return res.status(403).send('Cannot remove video of another user')
79 81
80 next() 82 next()
81 }) 83 })