Merge pull request #73 from gruppe-adler/multiple-users

allow multiple basic auth users, add morgan logFormat config option
This commit is contained in:
Björn Dahlgren 2017-10-01 03:30:54 +02:00 committed by GitHub
commit 5f72b805c1
3 changed files with 37 additions and 11 deletions

16
app.js
View File

@ -1,11 +1,11 @@
var express = require('express')
var basicAuth = require('express-basic-auth')
var bodyParser = require('body-parser')
var morgan = require('morgan')
var path = require('path')
var serveStatic = require('serve-static')
var config = require('./config')
var setupBasicAuth = require('./lib/setup-basic-auth')
var Manager = require('./lib/manager')
var Missions = require('./lib/missions')
var Mods = require('./lib/mods')
@ -15,18 +15,14 @@ var app = express()
var server = require('http').Server(app)
var io = require('socket.io')(server)
if (config.auth && config.auth.username && config.auth.password) {
var basicAuthUsers = {}
basicAuthUsers[config.auth.username] = config.auth.password
app.use(basicAuth({
challenge: true,
users: basicAuthUsers
}))
}
setupBasicAuth(config, app)
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(morgan('dev'))
morgan.token('user', function (req) { return req.auth ? req.auth.user : 'anon' })
app.use(morgan(config.logFormat || 'dev'))
app.use(serveStatic(path.join(__dirname, 'public')))
var logs = new Logs(config)

View File

@ -12,10 +12,11 @@ module.exports = {
'@mod1',
'@mod2',
],
auth: { // If both username and password is set, HTTP Basic Auth will be used
auth: { // If both username and password is set, HTTP Basic Auth will be used. You may use an array to specify more than one user.
username: '', // Username for HTTP Basic Auth
password: '', // Password for HTTP Basic Auth
},
prefix: "", // Prefix to all server names
suffix: "", // Suffix to all server names
logFormat: "dev", // expressjs/morgan log format
};

29
lib/setup-basic-auth.js Normal file
View File

@ -0,0 +1,29 @@
var basicAuth = require('express-basic-auth')
function getBasicAuthUsers (configAuth) {
var basicAuthUsers = {}
if (configAuth.username && configAuth.password) {
configAuth = [configAuth]
}
configAuth.forEach(function (user) {
basicAuthUsers[user.username] = user.password
})
return basicAuthUsers
}
module.exports = function (config, app) {
if (!config.auth) {
return
}
if (!config.auth.username && !config.auth.password && !Array.isArray(config.auth)) {
return
}
app.use(basicAuth({
challenge: true,
users: getBasicAuthUsers(config.auth)
}))
}