Server: remove encryption when seending requests to other pods
[oweals/peertube.git] / server / middlewares / validators / pods.js
1 'use strict'
2
3 const checkErrors = require('./utils').checkErrors
4 const constants = require('../../initializers/constants')
5 const friends = require('../../lib/friends')
6 const logger = require('../../helpers/logger')
7 const utils = require('../../helpers/utils')
8
9 const validatorsPod = {
10   makeFriends,
11   podsAdd
12 }
13
14 function makeFriends (req, res, next) {
15   // Force https if the administrator wants to make friends
16   if (utils.isTestInstance() === false && constants.CONFIG.WEBSERVER.SCHEME === 'http') {
17     return res.status(400).send('Cannot make friends with a non HTTPS webserver.')
18   }
19
20   req.checkBody('hosts', 'Should have an array of unique hosts').isEachUniqueHostValid()
21
22   logger.debug('Checking makeFriends parameters', { parameters: req.body })
23
24   checkErrors(req, res, function () {
25     friends.hasFriends(function (err, hasFriends) {
26       if (err) {
27         logger.error('Cannot know if we have friends.', { error: err })
28         res.sendStatus(500)
29       }
30
31       if (hasFriends === true) {
32         // We need to quit our friends before make new ones
33         res.sendStatus(409)
34       } else {
35         return next()
36       }
37     })
38   })
39 }
40
41 function podsAdd (req, res, next) {
42   req.checkBody('host', 'Should have an host').notEmpty().isURL()
43   req.checkBody('publicKey', 'Should have a public key').notEmpty()
44
45   // TODO: check we don't have it already
46
47   logger.debug('Checking podsAdd parameters', { parameters: req.body })
48
49   checkErrors(req, res, next)
50 }
51
52 // ---------------------------------------------------------------------------
53
54 module.exports = validatorsPod