From 2166c058f34dff6f91566930d12448805d829de7 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 7 Oct 2022 14:23:42 +0200 Subject: Allow admins to disable two factor auth --- server/middlewares/validators/users.ts | 47 +++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 15 deletions(-) (limited to 'server/middlewares') diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index 046029547..055af3b64 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts @@ -506,23 +506,40 @@ const usersVerifyEmailValidator = [ } ] -const usersCheckCurrentPassword = [ - body('currentPassword').custom(exists), +const usersCheckCurrentPasswordFactory = (targetUserIdGetter: (req: express.Request) => number | string) => { + return [ + body('currentPassword').optional().custom(exists), - async (req: express.Request, res: express.Response, next: express.NextFunction) => { - if (areValidationErrors(req, res)) return + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + if (areValidationErrors(req, res)) return - const user = res.locals.oauth.token.User - if (await user.isPasswordMatch(req.body.currentPassword) !== true) { - return res.fail({ - status: HttpStatusCode.FORBIDDEN_403, - message: 'currentPassword is invalid.' - }) - } + const user = res.locals.oauth.token.User + const isAdminOrModerator = user.role === UserRole.ADMINISTRATOR || user.role === UserRole.MODERATOR + const targetUserId = parseInt(targetUserIdGetter(req) + '') - return next() - } -] + // Admin/moderator action on another user, skip the password check + if (isAdminOrModerator && targetUserId !== user.id) { + return next() + } + + if (!req.body.currentPassword) { + return res.fail({ + status: HttpStatusCode.BAD_REQUEST_400, + message: 'currentPassword is missing' + }) + } + + if (await user.isPasswordMatch(req.body.currentPassword) !== true) { + return res.fail({ + status: HttpStatusCode.FORBIDDEN_403, + message: 'currentPassword is invalid.' + }) + } + + return next() + } + ] +} const userAutocompleteValidator = [ param('search') @@ -591,7 +608,7 @@ export { usersUpdateValidator, usersUpdateMeValidator, usersVideoRatingValidator, - usersCheckCurrentPassword, + usersCheckCurrentPasswordFactory, ensureUserRegistrationAllowed, ensureUserRegistrationAllowedForIP, usersGetValidator, -- cgit v1.2.3