From f33cb443069c73256d2e92d8a54999a6473addd1 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Wed, 17 Jun 2015 08:33:49 -0400 Subject: [PATCH] fix(auth) always show auth dialog --- lib/server/auth.js | 83 +++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/lib/server/auth.js b/lib/server/auth.js index 98713909..52bc49de 100644 --- a/lib/server/auth.js +++ b/lib/server/auth.js @@ -11,49 +11,50 @@ oldName; module.exports = function() { - var middle, type; - - type = init(config); - middle = httpAuth.connect(type); - - return middle; - }; - - 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 (!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); - }); + }, check); - return auth; + 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 hash, + 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; + } + + callback(equal); } })();