mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-19 09:24:51 +00:00
feature(auth) express -> auth
This commit is contained in:
parent
c745add934
commit
07ea970eed
3 changed files with 73 additions and 52 deletions
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue