Files
servicedock/nodejs/authentication/index.js

79 lines
2.3 KiB
JavaScript
Raw Normal View History

module.exports = function(config) {
2016-09-20 22:29:33 +00:00
var authentication;
if (config) {
2016-09-20 22:29:33 +00:00
authentication = function (username, password, success, fail) {
console.log("...try: ", username);
2016-09-20 15:00:00 +00:00
const crypto = require('crypto');
if (config.passwords && config.passwords[username]) {
2016-09-20 22:29:33 +00:00
console.log("...check hash");
2016-09-20 15:00:00 +00:00
if (crypto.getHashes().indexOf(config.passwords[username][0])>=0) {
if (crypto.createHash(config.passwords[username][0]).update(password, 'utf8').digest('hex') === config.passwords[username][1]) {
2016-09-20 22:29:33 +00:00
success(username);
2016-09-20 15:00:00 +00:00
return;
} else {
2016-09-20 22:29:33 +00:00
fail(username);
2016-09-20 15:00:00 +00:00
return;
}
} else {
console.log("**** HASH NOT FOUND ****");
2016-09-20 15:00:00 +00:00
console.log(config.passwords[username][0]);
console.log(crypto.getHashes());
2016-09-20 22:29:33 +00:00
fail(username);
2016-09-20 15:00:00 +00:00
return;
}
}
if (config.ldap) try {
2016-09-20 22:29:33 +00:00
console.log("...check ldap");
var LdapAuth = require('ldapauth');
var auth = new LdapAuth(config.ldap);
2016-09-20 22:29:33 +00:00
auth.once('connect', function () {
try {
auth.authenticate(username, password, function(err, usr) {
auth.close(function(err) {})
if (err) {
console.log("**** ERROR: LDAP Authentication failed:", err);
fail(username);
return;
}
console.log("**** SUCCESS: LDAP Authentication:");
success(username);
return;
});
} catch (e) {
console.log("**** Error: LDAP failed: ", e, e.stack);
fail(username);
}
2016-09-20 22:29:33 +00:00
return; // need to block here!
});
} catch (e) {
console.log("**** Error: LDAP failed: ", e, e.stack);
2016-09-20 22:29:33 +00:00
fail(username);
2016-09-20 15:00:00 +00:00
return;
}
2016-09-20 22:29:33 +00:00
if (config.unrestricted)
success(username);
else
fail(username);
return;
2016-09-20 15:00:00 +00:00
}
2016-09-20 22:29:33 +00:00
} else {
authentication = function (username, password, success, fail) {
console.log('**** Error: no access configuraion. To allow any user, add:')
console.log(' "restrict": {');
console.log(' "unrestricted": true');
console.log(' }');
fail(username);
}
}
return authentication;
}