1 import * as Bluebird from 'bluebird'
2 import * as express from 'express'
3 import 'express-validator'
4 import { body, param } from 'express-validator/check'
5 import { omit } from 'lodash'
6 import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
8 isUserAutoPlayVideoValid, isUserBlockedReasonValid,
9 isUserDescriptionValid,
10 isUserDisplayNameValid,
11 isUserNSFWPolicyValid,
15 isUserVideoQuotaValid,
16 isUserVideoQuotaDailyValid
17 } from '../../helpers/custom-validators/users'
18 import { isVideoExist } from '../../helpers/custom-validators/videos'
19 import { logger } from '../../helpers/logger'
20 import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/signup'
21 import { Redis } from '../../lib/redis'
22 import { UserModel } from '../../models/account/user'
23 import { areValidationErrors } from './utils'
24 import { ActorModel } from '../../models/activitypub/actor'
25 import { comparePassword } from '../../helpers/peertube-crypto'
27 const usersAddValidator = [
28 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'),
29 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
30 body('email').isEmail().withMessage('Should have a valid email'),
31 body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
32 body('videoQuotaDaily').custom(isUserVideoQuotaDailyValid).withMessage('Should have a valid daily user quota'),
33 body('role').custom(isUserRoleValid).withMessage('Should have a valid role'),
35 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
36 logger.debug('Checking usersAdd parameters', { parameters: omit(req.body, 'password') })
38 if (areValidationErrors(req, res)) return
39 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
45 const usersRegisterValidator = [
46 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'),
47 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
48 body('email').isEmail().withMessage('Should have a valid email'),
50 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
51 logger.debug('Checking usersRegister parameters', { parameters: omit(req.body, 'password') })
53 if (areValidationErrors(req, res)) return
54 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
60 const usersRemoveValidator = [
61 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
63 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
64 logger.debug('Checking usersRemove parameters', { parameters: req.params })
66 if (areValidationErrors(req, res)) return
67 if (!await checkUserIdExist(req.params.id, res)) return
69 const user = res.locals.user
70 if (user.username === 'root') {
71 return res.status(400)
72 .send({ error: 'Cannot remove the root user' })
80 const usersBlockingValidator = [
81 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
82 body('reason').optional().custom(isUserBlockedReasonValid).withMessage('Should have a valid blocking reason'),
84 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
85 logger.debug('Checking usersBlocking parameters', { parameters: req.params })
87 if (areValidationErrors(req, res)) return
88 if (!await checkUserIdExist(req.params.id, res)) return
90 const user = res.locals.user
91 if (user.username === 'root') {
92 return res.status(400)
93 .send({ error: 'Cannot block the root user' })
101 const deleteMeValidator = [
102 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
103 const user: UserModel = res.locals.oauth.token.User
104 if (user.username === 'root') {
105 return res.status(400)
106 .send({ error: 'You cannot delete your root account.' })
114 const usersUpdateValidator = [
115 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
116 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
117 body('videoQuota').optional().custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
118 body('videoQuotaDaily').optional().custom(isUserVideoQuotaDailyValid).withMessage('Should have a valid daily user quota'),
119 body('role').optional().custom(isUserRoleValid).withMessage('Should have a valid role'),
121 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
122 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
124 if (areValidationErrors(req, res)) return
125 if (!await checkUserIdExist(req.params.id, res)) return
127 const user = res.locals.user
128 if (user.username === 'root' && req.body.role !== undefined && user.role !== req.body.role) {
129 return res.status(400)
130 .send({ error: 'Cannot change root role.' })
138 const usersUpdateMeValidator = [
139 body('displayName').optional().custom(isUserDisplayNameValid).withMessage('Should have a valid display name'),
140 body('description').optional().custom(isUserDescriptionValid).withMessage('Should have a valid description'),
141 body('currentPassword').optional().custom(isUserPasswordValid).withMessage('Should have a valid current password'),
142 body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'),
143 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
144 body('nsfwPolicy').optional().custom(isUserNSFWPolicyValid).withMessage('Should have a valid display Not Safe For Work policy'),
145 body('autoPlayVideo').optional().custom(isUserAutoPlayVideoValid).withMessage('Should have a valid automatically plays video attribute'),
147 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
148 logger.debug('Checking usersUpdateMe parameters', { parameters: omit(req.body, 'password') })
150 if (req.body.password) {
151 if (!req.body.currentPassword) {
152 return res.status(400)
153 .send({ error: 'currentPassword parameter is missing.' })
157 const user: UserModel = res.locals.oauth.token.User
159 if (await user.isPasswordMatch(req.body.currentPassword) !== true) {
160 return res.status(401)
161 .send({ error: 'currentPassword is invalid.' })
166 if (areValidationErrors(req, res)) return
172 const usersGetValidator = [
173 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
175 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
176 logger.debug('Checking usersGet parameters', { parameters: req.params })
178 if (areValidationErrors(req, res)) return
179 if (!await checkUserIdExist(req.params.id, res)) return
185 const usersVideoRatingValidator = [
186 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
188 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
189 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
191 if (areValidationErrors(req, res)) return
192 if (!await isVideoExist(req.params.videoId, res, 'id')) return
198 const ensureUserRegistrationAllowed = [
199 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
200 const allowed = await isSignupAllowed()
201 if (allowed === false) {
202 return res.status(403)
203 .send({ error: 'User registration is not enabled or user limit is reached.' })
211 const ensureUserRegistrationAllowedForIP = [
212 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
213 const allowed = isSignupAllowedForCurrentIP(req.ip)
215 if (allowed === false) {
216 return res.status(403)
217 .send({ error: 'You are not on a network authorized for registration.' })
225 const usersAskResetPasswordValidator = [
226 body('email').isEmail().not().isEmpty().withMessage('Should have a valid email'),
228 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
229 logger.debug('Checking usersAskResetPassword parameters', { parameters: req.body })
231 if (areValidationErrors(req, res)) return
232 const exists = await checkUserEmailExist(req.body.email, res, false)
234 logger.debug('User with email %s does not exist (asking reset password).', req.body.email)
235 // Do not leak our emails
236 return res.status(204).end()
243 const usersResetPasswordValidator = [
244 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
245 body('verificationString').not().isEmpty().withMessage('Should have a valid verification string'),
246 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
248 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
249 logger.debug('Checking usersResetPassword parameters', { parameters: req.params })
251 if (areValidationErrors(req, res)) return
252 if (!await checkUserIdExist(req.params.id, res)) return
254 const user = res.locals.user as UserModel
255 const redisVerificationString = await Redis.Instance.getResetPasswordLink(user.id)
257 if (redisVerificationString !== req.body.verificationString) {
260 .send({ error: 'Invalid verification string.' })
268 const usersAskSendVerifyEmailValidator = [
269 body('email').isEmail().not().isEmpty().withMessage('Should have a valid email'),
271 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
272 logger.debug('Checking askUsersSendVerifyEmail parameters', { parameters: req.body })
274 if (areValidationErrors(req, res)) return
275 const exists = await checkUserEmailExist(req.body.email, res, false)
277 logger.debug('User with email %s does not exist (asking verify email).', req.body.email)
278 // Do not leak our emails
279 return res.status(204).end()
286 const usersVerifyEmailValidator = [
287 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
288 body('verificationString').not().isEmpty().withMessage('Should have a valid verification string'),
290 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
291 logger.debug('Checking usersVerifyEmail parameters', { parameters: req.params })
293 if (areValidationErrors(req, res)) return
294 if (!await checkUserIdExist(req.params.id, res)) return
296 const user = res.locals.user as UserModel
297 const redisVerificationString = await Redis.Instance.getVerifyEmailLink(user.id)
299 if (redisVerificationString !== req.body.verificationString) {
302 .send({ error: 'Invalid verification string.' })
310 const userAutocompleteValidator = [
311 param('search').isString().not().isEmpty().withMessage('Should have a search parameter')
314 // ---------------------------------------------------------------------------
319 usersRegisterValidator,
320 usersBlockingValidator,
321 usersRemoveValidator,
322 usersUpdateValidator,
323 usersUpdateMeValidator,
324 usersVideoRatingValidator,
325 ensureUserRegistrationAllowed,
326 ensureUserRegistrationAllowedForIP,
328 usersAskResetPasswordValidator,
329 usersResetPasswordValidator,
330 usersAskSendVerifyEmailValidator,
331 usersVerifyEmailValidator,
332 userAutocompleteValidator
335 // ---------------------------------------------------------------------------
337 function checkUserIdExist (id: number, res: express.Response) {
338 return checkUserExist(() => UserModel.loadById(id), res)
341 function checkUserEmailExist (email: string, res: express.Response, abortResponse = true) {
342 return checkUserExist(() => UserModel.loadByEmail(email), res, abortResponse)
345 async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) {
346 const user = await UserModel.loadByUsernameOrEmail(username, email)
350 .send({ error: 'User with this username or email already exists.' })
355 const actor = await ActorModel.loadLocalByName(username)
358 .send({ error: 'Another actor (account/channel) with this name on this instance already exists or has already existed.' })
366 async function checkUserExist (finder: () => Bluebird<UserModel>, res: express.Response, abortResponse = true) {
367 const user = await finder()
370 if (abortResponse === true) {
372 .send({ error: 'User not found' })
379 res.locals.user = user