Optional HTTP Basic Auth (#8586)

* add ability to add http basic auth to the website

* debug

* remove console.log
This commit is contained in:
Matteo Pagliazzi 2017-03-20 15:02:48 +01:00 committed by GitHub
parent d9d7c69432
commit 258742f6b7
4 changed files with 24 additions and 0 deletions

View file

@ -76,6 +76,11 @@
"APN_ENABLED": "false",
"FCM_SERVER_API_KEY": ""
},
"SITE_HTTP_AUTH": {
"ENABLED": "false",
"USERNAME": "admin",
"PASSWORD": "password"
},
"PUSHER": {
"ENABLED": "false",
"APP_ID": "appId",

5
npm-shrinkwrap.json generated
View file

@ -3733,6 +3733,11 @@
}
}
},
"express-basic-auth": {
"version": "1.0.1",
"from": "express-basic-auth@>=1.0.1 <2.0.0",
"resolved": "https://registry.npmjs.org/express-basic-auth/-/express-basic-auth-1.0.1.tgz"
},
"express-csv": {
"version": "0.6.0",
"from": "express-csv@>=0.6.0 <0.7.0",

View file

@ -41,6 +41,7 @@
"domain-middleware": "~0.1.0",
"estraverse": "^4.1.1",
"express": "~4.14.0",
"express-basic-auth": "^1.0.1",
"express-csv": "~0.6.0",
"express-validator": "^2.18.0",
"extract-text-webpack-plugin": "^2.0.0-rc.3",

View file

@ -26,9 +26,11 @@ import responseHandler from './response';
import {
attachTranslateFunction,
} from './language';
import basicAuth from 'express-basic-auth';
const IS_PROD = nconf.get('IS_PROD');
const DISABLE_LOGGING = nconf.get('DISABLE_REQUEST_LOGGING') === 'true';
const ENABLE_HTTP_AUTH = nconf.get('SITE_HTTP_AUTH:ENABLED') === 'true';
const PUBLIC_DIR = path.join(__dirname, '/../../client-old');
const SESSION_SECRET = nconf.get('SESSION_SECRET');
@ -74,6 +76,17 @@ module.exports = function attachMiddlewares (app, server) {
app.use(passport.initialize());
app.use(passport.session());
// The site can require basic HTTP authentication to be accessed
if (ENABLE_HTTP_AUTH) {
const httpBasicAuthUsers = {};
httpBasicAuthUsers[nconf.get('SITE_HTTP_AUTH:USERNAME')] = nconf.get('SITE_HTTP_AUTH:PASSWORD');
app.use(basicAuth({
users: httpBasicAuthUsers,
challenge: true,
realm: 'Habitica',
}));
}
app.use('/api/v2', v2);
app.use('/api/v1', v1);
app.use(v3); // the main app, also setup top-level routes