mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
enforce x-client header (#15476)
This commit is contained in:
@@ -4,6 +4,7 @@ import url from 'url';
|
||||
import {
|
||||
InvalidCredentialsError,
|
||||
NotAuthorized,
|
||||
BadRequest,
|
||||
} from '../libs/errors';
|
||||
import {
|
||||
model as User,
|
||||
@@ -12,6 +13,8 @@ import gcpStackdriverTracer from '../libs/gcpTraceAgent';
|
||||
import common from '../../common';
|
||||
import { getLanguageFromUser } from '../libs/language';
|
||||
|
||||
const ENFORCE_CLIENT_HEADER = nconf.get('ENFORCE_CLIENT_HEADER') === 'true';
|
||||
|
||||
const OFFICIAL_PLATFORMS = ['habitica-web', 'habitica-ios', 'habitica-android'];
|
||||
const COMMUNITY_MANAGER_EMAIL = nconf.get('EMAILS_COMMUNITY_MANAGER_EMAIL');
|
||||
const USER_FIELDS_ALWAYS_LOADED = ['_id', '_v', 'notifications', 'preferences', 'auth', 'flags', 'permissions'];
|
||||
@@ -63,6 +66,10 @@ export function authWithHeaders (options = {}) {
|
||||
const client = req.header('x-client');
|
||||
const optional = options.optional || false;
|
||||
|
||||
if (ENFORCE_CLIENT_HEADER && !client) {
|
||||
return next(new BadRequest(res.t('missingClientHeader')));
|
||||
}
|
||||
|
||||
if (!userId || !apiToken) {
|
||||
if (optional) return next();
|
||||
return next(new NotAuthorized(res.t('missingAuthHeaders')));
|
||||
|
||||
Reference in New Issue
Block a user