feature: IP filtering on signup page
[oweals/peertube.git] / server / middlewares / validators / account.ts
1 import * as express from 'express'
2 import { param } from 'express-validator/check'
3 import {
4   isAccountIdExist,
5   isAccountNameValid,
6   isAccountNameWithHostExist,
7   isLocalAccountNameExist
8 } from '../../helpers/custom-validators/accounts'
9 import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
10 import { logger } from '../../helpers/logger'
11 import { areValidationErrors } from './utils'
12
13 const localAccountValidator = [
14   param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
15
16   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
17     logger.debug('Checking localAccountValidator parameters', { parameters: req.params })
18
19     if (areValidationErrors(req, res)) return
20     if (!await isLocalAccountNameExist(req.params.name, res)) return
21
22     return next()
23   }
24 ]
25
26 const accountsGetValidator = [
27   param('id').custom(isIdOrUUIDValid).withMessage('Should have a valid id'),
28
29   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
30     logger.debug('Checking accountsGetValidator parameters', { parameters: req.params })
31
32     if (areValidationErrors(req, res)) return
33     if (!await isAccountIdExist(req.params.id, res)) return
34
35     return next()
36   }
37 ]
38
39 const accountsNameWithHostGetValidator = [
40   param('nameWithHost').exists().withMessage('Should have an account name with host'),
41
42   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
43     logger.debug('Checking accountsNameWithHostGetValidator parameters', { parameters: req.params })
44
45     if (areValidationErrors(req, res)) return
46     if (!await isAccountNameWithHostExist(req.params.nameWithHost, res)) return
47
48     return next()
49   }
50 ]
51
52 // ---------------------------------------------------------------------------
53
54 export {
55   localAccountValidator,
56   accountsGetValidator,
57   accountsNameWithHostGetValidator
58 }