mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 10:45:47 +00:00
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.
47 lines
1.2 KiB
JavaScript
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);
|
|
}
|
|
})();
|