mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 10:45:47 +00:00
feature(auth) express -> auth
This commit is contained in:
parent
c745add934
commit
07ea970eed
3 changed files with 73 additions and 52 deletions
69
lib/server/auth.js
Normal file
69
lib/server/auth.js
Normal file
|
|
@ -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;
|
||||
}
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue