]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/users.ts
Add more info logging
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / users.ts
CommitLineData
ecb4e35f 1import * as Bluebird from 'bluebird'
69818c93 2import * as express from 'express'
a2431b7d
C
3import 'express-validator'
4import { body, param } from 'express-validator/check'
ecb4e35f 5import { omit } from 'lodash'
3fd3ab2d 6import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
b60e5f38 7import {
ecb4e35f 8 isUserAutoPlayVideoValid,
4bbfc6c6
C
9 isUserDescriptionValid,
10 isUserDisplayNameValid,
0883b324 11 isUserNSFWPolicyValid,
ecb4e35f
C
12 isUserPasswordValid,
13 isUserRoleValid,
14 isUserUsernameValid,
3fd3ab2d
C
15 isUserVideoQuotaValid
16} from '../../helpers/custom-validators/users'
47564bbe 17import { isVideoExist } from '../../helpers/custom-validators/videos'
da854ddd 18import { logger } from '../../helpers/logger'
ff2c1fe8 19import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/utils'
ecb4e35f 20import { Redis } from '../../lib/redis'
3fd3ab2d 21import { UserModel } from '../../models/account/user'
a2431b7d 22import { areValidationErrors } from './utils'
2ef6a063 23import { ActorModel } from '../../models/activitypub/actor'
9bd26629 24
b60e5f38 25const usersAddValidator = [
563d032e 26 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'),
b60e5f38
C
27 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
28 body('email').isEmail().withMessage('Should have a valid email'),
29 body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
954605a8 30 body('role').custom(isUserRoleValid).withMessage('Should have a valid role'),
9bd26629 31
a2431b7d 32 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
ce97fe36 33 logger.debug('Checking usersAdd parameters', { parameters: omit(req.body, 'password') })
9bd26629 34
a2431b7d
C
35 if (areValidationErrors(req, res)) return
36 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
37
38 return next()
b60e5f38
C
39 }
40]
6fcd19ba 41
b60e5f38
C
42const usersRegisterValidator = [
43 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'),
44 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
45 body('email').isEmail().withMessage('Should have a valid email'),
77a5501f 46
a2431b7d 47 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
ce97fe36 48 logger.debug('Checking usersRegister parameters', { parameters: omit(req.body, 'password') })
77a5501f 49
a2431b7d
C
50 if (areValidationErrors(req, res)) return
51 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
52
53 return next()
b60e5f38
C
54 }
55]
9bd26629 56
b60e5f38
C
57const usersRemoveValidator = [
58 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
9bd26629 59
a2431b7d 60 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
b60e5f38 61 logger.debug('Checking usersRemove parameters', { parameters: req.params })
9bd26629 62
a2431b7d
C
63 if (areValidationErrors(req, res)) return
64 if (!await checkUserIdExist(req.params.id, res)) return
65
66 const user = res.locals.user
67 if (user.username === 'root') {
68 return res.status(400)
69 .send({ error: 'Cannot remove the root user' })
70 .end()
71 }
72
73 return next()
b60e5f38
C
74 }
75]
8094a898 76
b60e5f38
C
77const usersUpdateValidator = [
78 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
79 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
80 body('videoQuota').optional().custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
954605a8 81 body('role').optional().custom(isUserRoleValid).withMessage('Should have a valid role'),
8094a898 82
a2431b7d 83 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
b60e5f38 84 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
9bd26629 85
a2431b7d
C
86 if (areValidationErrors(req, res)) return
87 if (!await checkUserIdExist(req.params.id, res)) return
88
f8b8c36b
C
89 const user = res.locals.user
90 if (user.username === 'root' && req.body.role !== undefined && user.role !== req.body.role) {
91 return res.status(400)
92 .send({ error: 'Cannot change root role.' })
93 .end()
94 }
95
a2431b7d 96 return next()
b60e5f38
C
97 }
98]
9bd26629 99
b60e5f38 100const usersUpdateMeValidator = [
ed56ad11 101 body('displayName').optional().custom(isUserDisplayNameValid).withMessage('Should have a valid display name'),
2422c46b 102 body('description').optional().custom(isUserDescriptionValid).withMessage('Should have a valid description'),
b60e5f38
C
103 body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'),
104 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
0883b324 105 body('nsfwPolicy').optional().custom(isUserNSFWPolicyValid).withMessage('Should have a valid display Not Safe For Work policy'),
7efe153b 106 body('autoPlayVideo').optional().custom(isUserAutoPlayVideoValid).withMessage('Should have a valid automatically plays video attribute'),
9bd26629 107
b60e5f38
C
108 (req: express.Request, res: express.Response, next: express.NextFunction) => {
109 // TODO: Add old password verification
ce97fe36 110 logger.debug('Checking usersUpdateMe parameters', { parameters: omit(req.body, 'password') })
8094a898 111
a2431b7d
C
112 if (areValidationErrors(req, res)) return
113
114 return next()
b60e5f38
C
115 }
116]
8094a898 117
b60e5f38
C
118const usersGetValidator = [
119 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
d38b8281 120
a2431b7d 121 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
ce97fe36 122 logger.debug('Checking usersGet parameters', { parameters: req.params })
a2431b7d
C
123
124 if (areValidationErrors(req, res)) return
125 if (!await checkUserIdExist(req.params.id, res)) return
126
127 return next()
b60e5f38
C
128 }
129]
d38b8281 130
b60e5f38 131const usersVideoRatingValidator = [
72c7248b 132 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
0a6658fd 133
a2431b7d 134 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
b60e5f38 135 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
0a6658fd 136
a2431b7d
C
137 if (areValidationErrors(req, res)) return
138 if (!await isVideoExist(req.params.videoId, res)) return
139
140 return next()
b60e5f38
C
141 }
142]
143
144const ensureUserRegistrationAllowed = [
a2431b7d
C
145 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
146 const allowed = await isSignupAllowed()
147 if (allowed === false) {
148 return res.status(403)
149 .send({ error: 'User registration is not enabled or user limit is reached.' })
150 .end()
151 }
152
153 return next()
b60e5f38
C
154 }
155]
291e8d3e 156
ff2c1fe8
RK
157const ensureUserRegistrationAllowedForIP = [
158 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
159 const allowed = isSignupAllowedForCurrentIP(req.ip)
160
161 if (allowed === false) {
162 return res.status(403)
163 .send({ error: 'You are not on a network authorized for registration.' })
164 .end()
165 }
166
167 return next()
168 }
169]
170
ecb4e35f
C
171const usersAskResetPasswordValidator = [
172 body('email').isEmail().not().isEmpty().withMessage('Should have a valid email'),
173
174 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
175 logger.debug('Checking usersAskResetPassword parameters', { parameters: req.body })
176
177 if (areValidationErrors(req, res)) return
178 const exists = await checkUserEmailExist(req.body.email, res, false)
179 if (!exists) {
180 logger.debug('User with email %s does not exist (asking reset password).', req.body.email)
181 // Do not leak our emails
182 return res.status(204).end()
183 }
184
185 return next()
186 }
187]
188
189const usersResetPasswordValidator = [
190 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
191 body('verificationString').not().isEmpty().withMessage('Should have a valid verification string'),
192 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
193
194 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
195 logger.debug('Checking usersResetPassword parameters', { parameters: req.params })
196
197 if (areValidationErrors(req, res)) return
198 if (!await checkUserIdExist(req.params.id, res)) return
199
200 const user = res.locals.user as UserModel
201 const redisVerificationString = await Redis.Instance.getResetPasswordLink(user.id)
202
203 if (redisVerificationString !== req.body.verificationString) {
204 return res
205 .status(403)
206 .send({ error: 'Invalid verification string.' })
f076daa7 207 .end()
ecb4e35f
C
208 }
209
210 return next()
211 }
212]
213
9bd26629
C
214// ---------------------------------------------------------------------------
215
65fcc311
C
216export {
217 usersAddValidator,
77a5501f 218 usersRegisterValidator,
65fcc311
C
219 usersRemoveValidator,
220 usersUpdateValidator,
8094a898 221 usersUpdateMeValidator,
291e8d3e 222 usersVideoRatingValidator,
8094a898 223 ensureUserRegistrationAllowed,
ff2c1fe8 224 ensureUserRegistrationAllowedForIP,
c5911fd3 225 usersGetValidator,
ecb4e35f
C
226 usersAskResetPasswordValidator,
227 usersResetPasswordValidator
8094a898
C
228}
229
230// ---------------------------------------------------------------------------
231
ecb4e35f
C
232function checkUserIdExist (id: number, res: express.Response) {
233 return checkUserExist(() => UserModel.loadById(id), res)
234}
a2431b7d 235
ecb4e35f
C
236function checkUserEmailExist (email: string, res: express.Response, abortResponse = true) {
237 return checkUserExist(() => UserModel.loadByEmail(email), res, abortResponse)
65fcc311 238}
77a5501f 239
a2431b7d 240async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) {
3fd3ab2d 241 const user = await UserModel.loadByUsernameOrEmail(username, email)
a2431b7d
C
242
243 if (user) {
244 res.status(409)
edf7f40a 245 .send({ error: 'User with this username or email already exists.' })
a2431b7d
C
246 .end()
247 return false
248 }
249
2ef6a063
C
250 const actor = await ActorModel.loadLocalByName(username)
251 if (actor) {
252 res.status(409)
253 .send({ error: 'Another actor (account/channel) with this name already exists.' })
254 .end()
255 return false
256 }
257
a2431b7d 258 return true
77a5501f 259}
ecb4e35f
C
260
261async function checkUserExist (finder: () => Bluebird<UserModel>, res: express.Response, abortResponse = true) {
262 const user = await finder()
263
264 if (!user) {
265 if (abortResponse === true) {
266 res.status(404)
267 .send({ error: 'User not found' })
268 .end()
269 }
270
271 return false
272 }
273
274 res.locals.user = user
275
276 return true
277}