From c7b8efa4dc8219c3954d482020ed6c94eb63d423 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 22 Sep 2014 02:44:36 -0400 Subject: [PATCH] feature(auth) add config --- lib/cloudcmd.js | 13 +++++-------- lib/server/auth.js | 33 +++++++++++++++++---------------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/lib/cloudcmd.js b/lib/cloudcmd.js index 9c4f7a58..02bb51a9 100644 --- a/lib/cloudcmd.js +++ b/lib/cloudcmd.js @@ -22,7 +22,7 @@ webconsole, emptyFunc = function(req, res, next) { - Util.exec(next); + next(); }; emptyFunc.middle = function() { @@ -34,7 +34,6 @@ Util.log(error.message); }) || emptyFunc; - module.exports = function(params) { var keys, p = params || {}, @@ -68,21 +67,19 @@ var isOption = function(name) { return config(name); }, - + isMinify = isOption.bind(null, 'minify'), isOnline = isOption.bind(null, 'online'), isCache = isOption.bind(null, 'cache'), + authFunc = config('auth') ? auth() : emptyFunc, + ponseStatic = ponse.static(DIR_ROOT, { cache: isCache }), funcs = [ - auth({ - auth: config('auth'), - username: config('username'), - password: config('password') - }), + authFunc, rest, route, diff --git a/lib/server/auth.js b/lib/server/auth.js index 924689be..b0de6da1 100644 --- a/lib/server/auth.js +++ b/lib/server/auth.js @@ -1,31 +1,32 @@ (function() { 'use strict'; - var crypto = require('crypto'), + var DIR = './', + DIR_LIB = DIR + '../', + crypto = require('crypto'), + + tryRequire = require(DIR + 'tryRequire'), + config = require(DIR + 'config'), + Util = require(DIR_LIB + 'util'), - tryRequire = require('./tryRequire'), oldPass, oldName; - module.exports = function(config) { + module.exports = function() { var type, httpAuth, middle = function(req, res, next) { next(); }; - if (!config) - config = { - auth: false - }; + httpAuth = tryRequire('http-auth', function(error) { + if (error) + Util.log(error.message); + }); - if (config.auth) { - httpAuth = tryRequire('http-auth'); - - if (httpAuth) { - type = init(httpAuth, config); - middle = httpAuth.connect(type); - } + if (httpAuth) { + type = init(httpAuth, config); + middle = httpAuth.connect(type); } return middle; @@ -36,8 +37,8 @@ realm: 'Cloud Commander' }, function (username, password, callback) { // Custom authentication method. var hash, - name = config.username, - passwd = config.password, + name = config('username'), + passwd = config('password'), equal = username === name, sha = crypto.createHash('sha1');