diff --git a/lib/cloudcmd.js b/lib/cloudcmd.js index 60e9c0c6..4abbfc9c 100644 --- a/lib/cloudcmd.js +++ b/lib/cloudcmd.js @@ -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', diff --git a/lib/server/auth.js b/lib/server/auth.js index 1dcf357d..98713909 100644 --- a/lib/server/auth.js +++ b/lib/server/auth.js @@ -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);