module.exports = function(config) { authentication = function (username, password, success, fail) { if (config) { const crypto = require('crypto'); if (config.passwords && config.passwords[username]) { 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]) { success(); return; } else { fail(); return; } } else { console.log("**** HASH NOT FOUND ****"); console.log(config.passwords[username][0]); console.log(crypto.getHashes()); fail(); return; } } if (config.ldap) try { var LdapAuth = require('ldapauth'); var auth = new LdapAuth(config.ldap); auth.authenticate(username, password, function(err, usr) { auth.close(function(err) {}) if (err) { console.log("**** ERROR: LDAP Authentication failed:", err); fail(); return; } console.log("**** SUCCESS: LDAP Authentication:"); success(); return; }); return; // need to block here! } catch (e) { console.log("**** Error: LDAP failed: ", e, e.stack); fail(); return; } } fail(); return; } return authentication; }