From 0883b3245bf0deb9106c4041e9afbd3521b79280 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 19 Apr 2018 11:01:34 +0200 Subject: Add ability to choose what policy we have for NSFW videos There is a global instance setting and a per user setting --- server/middlewares/oauth.ts | 10 ++++++++++ server/middlewares/validators/config.ts | 3 ++- server/middlewares/validators/users.ts | 4 ++-- 3 files changed, 14 insertions(+), 3 deletions(-) (limited to 'server/middlewares') diff --git a/server/middlewares/oauth.ts b/server/middlewares/oauth.ts index 41a3fb718..a6f28dd5b 100644 --- a/server/middlewares/oauth.ts +++ b/server/middlewares/oauth.ts @@ -2,6 +2,7 @@ import * as express from 'express' import * as OAuthServer from 'express-oauth-server' import 'express-validator' import { OAUTH_LIFETIME } from '../initializers' +import { logger } from '../helpers/logger' const oAuthServer = new OAuthServer({ useErrorHandler: true, @@ -13,6 +14,8 @@ const oAuthServer = new OAuthServer({ function authenticate (req: express.Request, res: express.Response, next: express.NextFunction) { oAuthServer.authenticate()(req, res, err => { if (err) { + logger.warn('Cannot authenticate.', { err }) + return res.status(err.status) .json({ error: 'Token is invalid.', @@ -25,6 +28,12 @@ function authenticate (req: express.Request, res: express.Response, next: expres }) } +function optionalAuthenticate (req: express.Request, res: express.Response, next: express.NextFunction) { + if (req.header('authorization')) return authenticate(req, res, next) + + return next() +} + function token (req: express.Request, res: express.Response, next: express.NextFunction) { return oAuthServer.token()(req, res, err => { if (err) { @@ -44,5 +53,6 @@ function token (req: express.Request, res: express.Response, next: express.NextF export { authenticate, + optionalAuthenticate, token } diff --git a/server/middlewares/validators/config.ts b/server/middlewares/validators/config.ts index ee6f6efa4..f58c0676c 100644 --- a/server/middlewares/validators/config.ts +++ b/server/middlewares/validators/config.ts @@ -1,6 +1,6 @@ import * as express from 'express' import { body } from 'express-validator/check' -import { isUserVideoQuotaValid } from '../../helpers/custom-validators/users' +import { isUserNSFWPolicyValid, isUserVideoQuotaValid } from '../../helpers/custom-validators/users' import { logger } from '../../helpers/logger' import { areValidationErrors } from './utils' @@ -9,6 +9,7 @@ const customConfigUpdateValidator = [ body('instance.description').exists().withMessage('Should have a valid instance description'), body('instance.terms').exists().withMessage('Should have a valid instance terms'), body('instance.defaultClientRoute').exists().withMessage('Should have a valid instance default client route'), + body('instance.defaultNSFWPolicy').custom(isUserNSFWPolicyValid).withMessage('Should have a valid NSFW policy'), body('instance.customizations.css').exists().withMessage('Should have a valid instance CSS customization'), body('instance.customizations.javascript').exists().withMessage('Should have a valid instance JavaScript customization'), body('cache.previews.size').isInt().withMessage('Should have a valid previews size'), diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index 6ea3d0b6c..5dd8caa3f 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts @@ -8,7 +8,7 @@ import { isAvatarFile, isUserAutoPlayVideoValid, isUserDescriptionValid, - isUserDisplayNSFWValid, + isUserNSFWPolicyValid, isUserPasswordValid, isUserRoleValid, isUserUsernameValid, @@ -101,7 +101,7 @@ const usersUpdateMeValidator = [ body('description').optional().custom(isUserDescriptionValid).withMessage('Should have a valid description'), body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'), body('email').optional().isEmail().withMessage('Should have a valid email attribute'), - body('displayNSFW').optional().custom(isUserDisplayNSFWValid).withMessage('Should have a valid display Not Safe For Work attribute'), + body('nsfwPolicy').optional().custom(isUserNSFWPolicyValid).withMessage('Should have a valid display Not Safe For Work policy'), body('autoPlayVideo').optional().custom(isUserAutoPlayVideoValid).withMessage('Should have a valid automatically plays video attribute'), (req: express.Request, res: express.Response, next: express.NextFunction) => { -- cgit v1.2.3