mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 18:55:26 +00:00
81 lines
2.5 KiB
JavaScript
81 lines
2.5 KiB
JavaScript
(function() {
|
|
'use strict';
|
|
|
|
if (!global.cloudcmd)
|
|
return console.log(
|
|
'# express.js' + '\n' +
|
|
'# -----------' + '\n' +
|
|
'# Module is part of Cloud Commander,' + '\n' +
|
|
'# easy to use web server.' + '\n' +
|
|
'# http://cloudcmd.io' + '\n');
|
|
|
|
var main = global.cloudcmd.main,
|
|
express = main.require('express'),
|
|
httpAuth = main.require('http-auth'),
|
|
logger = main.require('morgan'),
|
|
Util = main.util,
|
|
crypto = require('crypto'),
|
|
basic,
|
|
oldPass,
|
|
oldName,
|
|
app = express && express();
|
|
|
|
exports.getApp = function(middleware) {
|
|
var isArray = Util.isArray(middleware),
|
|
config = main.config,
|
|
auth = config.auth;
|
|
|
|
if (app) {
|
|
if (logger)
|
|
app.use(logger('dev'));
|
|
|
|
if (auth && httpAuth) {
|
|
initAuth();
|
|
app.use(httpAuth.connect(basic));
|
|
}
|
|
|
|
if (isArray)
|
|
middleware.forEach(function(middle) {
|
|
app.use(middle);
|
|
});
|
|
}
|
|
|
|
return app;
|
|
};
|
|
|
|
|
|
function initAuth() {
|
|
basic = httpAuth.basic({
|
|
realm: 'Cloud Commander'
|
|
}, function (username, password, callback) { // Custom authentication method.
|
|
var hash,
|
|
config = main.config,
|
|
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);
|
|
});
|
|
}
|
|
})();
|