cloudcmd/lib/server/auth.js
Eric Anderson eceb0ac2af fix(auth) blank password results in inability to authenticate
Found myself in a vicious loop if I failed to type in a password when prompted in Chrome. Every refresh of the page would fall into the criton check with a blank password. Seems Chrome wouldn't clear the headers until I closed the browser and/or switched to a new incognito tab. Probably a user error, but still, this avoided the criton throw.
2016-09-06 09:48:10 +03:00

47 lines
1.2 KiB
JavaScript

(function() {
'use strict';
var DIR = './',
httpAuth = require('http-auth'),
criton = require('criton'),
config = require(DIR + 'config');
module.exports = function() {
var auth = httpAuth.basic({
realm: 'Cloud Commander'
}, check);
return middle(auth);
};
function middle(authentication) {
return function(req, res, next) {
var is = config('auth');
if (!is)
next();
else
authentication.check(req, res, function(/* success */) {
next();
});
};
}
function check(username, password, callback) {
var BAD_CREDENTIALS = false,
sameName,
samePass,
name = config('username'),
pass = config('password'),
algo = config('algo');
if (!password)
return callback(BAD_CREDENTIALS);
sameName = username === name;
samePass = pass === criton(password, algo);
callback(sameName && samePass);
}
})();