diff --git a/lib/cloudcmd.js b/lib/cloudcmd.js index 2b88e9dd..f3aae816 100644 --- a/lib/cloudcmd.js +++ b/lib/cloudcmd.js @@ -7,6 +7,7 @@ Util = require(DIR + 'util'), + auth = require(DIR_SERVER + 'auth'), config = require(DIR_SERVER + 'config'), minify = require(DIR_SERVER + 'minify'), rest = require(DIR_SERVER + 'rest'), @@ -74,6 +75,7 @@ }), funcs = [ + auth(), rest, route, diff --git a/lib/server/auth.js b/lib/server/auth.js new file mode 100644 index 00000000..924689be --- /dev/null +++ b/lib/server/auth.js @@ -0,0 +1,69 @@ +(function() { + 'use strict'; + + var crypto = require('crypto'), + + tryRequire = require('./tryRequire'), + oldPass, + oldName; + + module.exports = function(config) { + var type, httpAuth, + + middle = function(req, res, next) { + next(); + }; + + if (!config) + config = { + auth: false + }; + + if (config.auth) { + httpAuth = tryRequire('http-auth'); + + if (httpAuth) { + type = init(httpAuth, config); + middle = httpAuth.connect(type); + } + } + + return middle; + }; + + function init(httpAuth, config) { + var auth = httpAuth.basic({ + realm: 'Cloud Commander' + }, function (username, password, callback) { // Custom authentication method. + var hash, + name = config.username, + passwd = config.password, + equal = username === name, + sha = crypto.createHash('sha1'); + + if (!oldPass) + oldPass = passwd; + + if (!oldName) + oldName = name; + + if (!equal) + username === oldName; + + sha.update(password); + hash = sha.digest('hex'); + equal = passwd === hash && equal; + + if (!equal) { + sha = crypto.createHash('sha1'); + sha.update(oldPass); + hash = sha.digest('hex'); + equal = passwd === hash && equal; + } + + callback(equal); + }); + + return auth; + } +})(); diff --git a/lib/server/express.js b/lib/server/express.js index fc4651a7..8a1bb0d9 100644 --- a/lib/server/express.js +++ b/lib/server/express.js @@ -5,36 +5,21 @@ DIR_LIB = DIR + 'lib/', Util = require(DIR_LIB + 'util'), - crypto = require('crypto'), - tryRequire = require('./tryRequire'), express = tryRequire('express'), - httpAuth = tryRequire('http-auth'), + logger = tryRequire('morgan'), - basic, - oldPass, - oldName, app = express && express(); - exports.getApp = function(middleware, config) { + exports.getApp = function(middleware) { var isArray = Util.isArray(middleware); - if (!config) - config = { - auth: false - }; - if (app) { if (logger) app.use(logger('dev')); - if (config.auth && httpAuth) { - initAuth(config); - app.use(httpAuth.connect(basic)); - } - if (isArray) middleware.forEach(function(middle) { app.use(middle); @@ -45,39 +30,4 @@ return app; }; - - - function initAuth(config) { - basic = httpAuth.basic({ - realm: 'Cloud Commander' - }, function (username, password, callback) { // Custom authentication method. - var hash, - name = config.username, - passwd = config.password, - equal = username === name, - sha = crypto.createHash('sha1'); - - if (!oldPass) - oldPass = passwd; - - if (!oldName) - oldName = name; - - if (!equal) - username === oldName; - - sha.update(password); - hash = sha.digest('hex'); - equal = passwd === hash && equal; - - if (!equal) { - sha = crypto.createHash('sha1'); - sha.update(oldPass); - hash = sha.digest('hex'); - equal = passwd === hash && equal; - } - - callback(equal); - }); - } })();