feature(auth) add ability to enable/disable without restart

This commit is contained in:
coderaiser 2015-06-15 03:35:00 -04:00
parent 8df7a8a019
commit b9a0e4e409
2 changed files with 27 additions and 32 deletions

View file

@ -96,8 +96,6 @@
isDiff = isOption.bind(null, 'diff'),
isZip = isOption.bind(null, 'zip'),
authFunc = config('auth') ? auth() : emptyFunc,
ponseStatic = ponse.static(DIR_ROOT, {
cache: isCache
}),
@ -105,7 +103,7 @@
funcs = [
logout,
setUrl(prefix),
authFunc,
auth(),
config(),
restafary({
prefix : cloudfunc.apiURL + '/fs',

View file

@ -3,7 +3,7 @@
var DIR = './',
tryRequire = require('tryrequire'),
httpAuth = require('http-auth'),
config = require(DIR + 'config'),
cryptPassword = require(DIR + 'password'),
@ -11,47 +11,44 @@
oldName;
module.exports = function() {
var type, httpAuth,
middle = function(req, res, next) {
next();
};
httpAuth = tryRequire('http-auth', {log: true});
if (httpAuth) {
type = init(httpAuth, config);
middle = httpAuth.connect(type);
}
var middle, type;
type = init(config);
middle = httpAuth.connect(type);
return middle;
};
function init(httpAuth, config) {
function init(config) {
var auth = httpAuth.basic({
realm: 'Cloud Commander'
}, function (username, password, callback) {
var hash,
is = config('auth'),
name = config('username'),
pass = config('password'),
equal = username === name,
algo = config('algo');
if (!oldPass)
oldPass = pass;
if (!oldName)
oldName = name;
if (!equal)
username === oldName;
hash = cryptPassword(algo, password);
equal = pass === hash && equal;
if (!equal) {
hash = cryptPassword(algo, oldPass);
equal = pass === hash && equal;
if (!is) {
equal = true;
} else {
if (!oldPass)
oldPass = pass;
if (!oldName)
oldName = name;
if (!equal)
username === oldName;
hash = cryptPassword(algo, password);
equal = pass === hash && equal;
if (!equal) {
hash = cryptPassword(algo, oldPass);
equal = pass === hash && equal;
}
}
callback(equal);