make authentication function async

This commit is contained in:
Alex Kocharin 2014-06-26 19:23:21 +04:00
parent 5cc0187b67
commit 81486f412f
3 changed files with 17 additions and 14 deletions

View File

@ -146,9 +146,9 @@ Config.prototype.get_package_setting = function(package, setting) {
return undefined return undefined
} }
Config.prototype.authenticate = function(user, password) { Config.prototype.authenticate = function(user, password, cb) {
if (this.users[user] == null) return false if (this.users[user] == null) return cb(null, false)
return crypto.createHash('sha1').update(password).digest('hex') === this.users[user].password return cb(null, crypto.createHash('sha1').update(password).digest('hex') === this.users[user].password)
} }
module.exports = Config module.exports = Config

View File

@ -89,8 +89,8 @@ module.exports = function(config_hash) {
next() next()
}) })
app.use(Cats.middleware) app.use(Cats.middleware)
app.use(basic_auth(function(user, pass) { app.use(basic_auth(function(user, pass, cb) {
return config.authenticate(user, pass) config.authenticate(user, pass, cb)
})) }))
app.use(express.json({strict: false, limit: config.max_body_size || '10mb'})) app.use(express.json({strict: false, limit: config.max_body_size || '10mb'}))
app.use(express.compress()) app.use(express.compress())

View File

@ -76,7 +76,9 @@ module.exports.basic_auth = function basic_auth(callback) {
var user = credentials.slice(0, index) var user = credentials.slice(0, index)
, pass = credentials.slice(index + 1) , pass = credentials.slice(index + 1)
if (callback(user, pass)) { callback(user, pass, function(err, is_ok) {
if (err) return next(err)
if (is_ok) {
req.remoteUser = user req.remoteUser = user
next() next()
} else { } else {
@ -85,6 +87,7 @@ module.exports.basic_auth = function basic_auth(callback) {
msg: 'bad username/password, access denied', msg: 'bad username/password, access denied',
}) })
} }
})
} }
} }