allow array of basic auth users

This commit is contained in:
Moritz Schmidt 2017-09-30 14:13:59 +02:00 committed by Björn Dahlgren
parent f486fa1047
commit 9326ee4c73
3 changed files with 32 additions and 10 deletions

11
app.js
View File

@ -1,11 +1,11 @@
var express = require('express') var express = require('express')
var basicAuth = require('express-basic-auth')
var bodyParser = require('body-parser') var bodyParser = require('body-parser')
var morgan = require('morgan') var morgan = require('morgan')
var path = require('path') var path = require('path')
var serveStatic = require('serve-static') var serveStatic = require('serve-static')
var config = require('./config') var config = require('./config')
var setupBasicAuth = require('./lib/setup-basic-auth')
var Manager = require('./lib/manager') var Manager = require('./lib/manager')
var Missions = require('./lib/missions') var Missions = require('./lib/missions')
var Mods = require('./lib/mods') var Mods = require('./lib/mods')
@ -15,14 +15,7 @@ var app = express()
var server = require('http').Server(app) var server = require('http').Server(app)
var io = require('socket.io')(server) var io = require('socket.io')(server)
if (config.auth && config.auth.username && config.auth.password) { setupBasicAuth(config, app)
var basicAuthUsers = {}
basicAuthUsers[config.auth.username] = config.auth.password
app.use(basicAuth({
challenge: true,
users: basicAuthUsers
}))
}
app.use(bodyParser.json()) app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false })) app.use(bodyParser.urlencoded({ extended: false }))

View File

@ -12,7 +12,7 @@ module.exports = {
'@mod1', '@mod1',
'@mod2', '@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 username: '', // Username for HTTP Basic Auth
password: '', // Password for HTTP Basic Auth password: '', // Password for HTTP Basic Auth
}, },

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)
}))
}