mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 18:55:26 +00:00
90 lines
2.5 KiB
JavaScript
90 lines
2.5 KiB
JavaScript
(function() {
|
|
'use strict';
|
|
|
|
var DIR = __dirname + '/../../',
|
|
DIR_LIB = DIR + 'lib/',
|
|
|
|
Util = require(DIR_LIB + 'util'),
|
|
crypto = require('crypto'),
|
|
main = global.cloudcmd.main,
|
|
|
|
express = tryRequire('express'),
|
|
httpAuth = tryRequire('http-auth'),
|
|
logger = tryRequire('morgan'),
|
|
|
|
basic,
|
|
oldPass,
|
|
oldName,
|
|
app = express && express();
|
|
|
|
exports.getApp = function(middleware, config) {
|
|
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);
|
|
});
|
|
|
|
app.use(express.static(DIR));
|
|
}
|
|
|
|
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);
|
|
});
|
|
}
|
|
|
|
function tryRequire(name) {
|
|
var module;
|
|
|
|
Util.exec.try(function() {
|
|
module = require(name);
|
|
});
|
|
|
|
return module;
|
|
}
|
|
})();
|