Add basic auth

This commit is contained in:
Jake Howard 2017-01-21 21:06:21 +00:00
parent 35fbefef03
commit 7fd8bd3071
4 changed files with 25 additions and 4 deletions

View file

@ -29,6 +29,7 @@
"compression": "=1.6.2",
"connect-static-file": "=1.1.2",
"express": "=4.14.0",
"express-basic-auth": "=0.2.3",
"express-winston": "=2.1.2",
"helmet": "=3.4.0",
"opbeat": "=4.7.0",

17
src/basic-auth.js Normal file
View file

@ -0,0 +1,17 @@
const basicAuth = require('express-basic-auth');
const { BASIC_AUTH_ENABLED } = require('./consts');
function basicAuthHandler(username, password) {
return process.env.BASIC_AUTH_USERNAME === username && process.env.BASIC_AUTH_PASSWORD === password;
}
if (BASIC_AUTH_ENABLED) {
module.exports = basicAuth({
authorizer: basicAuthHandler,
challenge: true
});
} else {
module.exports = (req, res, next) => next();
}

View file

@ -4,5 +4,6 @@ module.exports = {
SERVE_DIR: IN_TEST ? 'site/' : process.argv[process.argv.length - 1],
PORT: process.env.PORT || 5000,
IN_TEST,
IN_PRODUCTION: process.env.NODE_ENV === 'production'
IN_PRODUCTION: process.env.NODE_ENV === 'production',
BASIC_AUTH_ENABLED: process.env.BASIC_AUTH_USERNAME && process.env.BASIC_AUTH_PASSWORD
};

View file

@ -3,20 +3,22 @@
console.log('Starting Server...');
const app = require('express')();
const utils = require('./consts');
const consts = require('./consts');
const compression = require('compression');
const helmet = require('helmet');
const opbeat = require('opbeat').start({
active: utils.IN_PRODUCTION
active: consts.IN_PRODUCTION
});
const logging = require('./logging');
const staticFiles = require('./static-files');
const handle404 = require('./404');
const basicAuth = require('./basic-auth');
// Custom Middleware
app.use(logging);
app.use(basicAuth);
app.use(staticFiles.indexHandle);
app.use(staticFiles.static);
app.use(handle404);
@ -26,7 +28,7 @@ app.use(compression({ level: 9 }));
app.use(helmet());
app.use(opbeat.middleware.express());
const server = app.listen(utils.PORT, function () {
const server = app.listen(consts.PORT, function () {
console.log('Server started on ' + server.address().port);
});