Skip to content
Snippets Groups Projects
Commit c2cd7d48 authored by rofl256's avatar rofl256
Browse files

init commit

parent 32644a5f
No related branches found
No related tags found
No related merge requests found
var ldap = require('ldapjs');
var server = ldap.createServer();
var checkPassword = null;
var errorCallback = null;
var warningCallback = null;
var ldapServerPassword = "asdg";
var serverPort = 389;
var cids = {};
module.exports = {
init : function(options) {
if(options["ldapServerPassword"]) {
ldapServerPassword = options["ldapServerPassword"];
} else {
console.log("Warning: LdapAuth Serverpassword not defined!");
}
if(options["checkPassword"]) {
checkPassword = options["checkPassword"];
} else {
console.log("Error: LdapAuth checkPassword function not defined! Auth will not work!");
}
if(options["serverPort"]) {
serverPort = options["serverPort"];
}
if(options["warningCallback"]) {
warningCallback = options["warningCallback"];
}
if(options["errorCallback"]) {
errorCallback = options["errorCallback"];
}
loadServer();
}
}
function loadServer() {
//1. First connection and login to ldap Server
server.bind('cn=auth', function(req, res, next) {
if (req.dn.toString() !== 'cn=auth' || req.credentials !== ldapServerPassword) {
if(warningCallback) {
warningCallback("Someone with invaild ldapServerPassword wants to login.")
}
return next(new ldap.InvalidCredentialsError());
}
res.end();
return next();
});
//2. Searching for user (We just save it for now and return a success)
server.search('ou=Users', function(req, res, next) {
var cId = req["connection"]["ldap"]["id"];
if(!req["filter"] || !req["filter"]["filters"] || !req["filter"]["filters"][0] || !req["filter"]["filters"][0]["raw"]) {
if(errorCallback) {
errorCallback("Invaild filterParameters...!");
}
return next(new ldap.InvalidCredentialsError());
}
var userName = req["filter"]["filters"][0]["raw"].toString();
cids[cId] = userName;
var dn = req.dn.toString();
res.send({
dn: dn,
status : 0,
attributes: {status:0}
});
res.end();
});
//3. Getting password. Then check user an password combination...
server.bind('ou=Users', function(req, res, next) {
var dn = req.dn.toString();
var cId = req["connection"]["ldap"]["id"];
var userPassword = req["credentials"];
var userName = cids[cId];
console.log("userName:",userName);
console.log("userPassword:",userPassword);
delete cids[cId];
if(checkPassword) {
checkPassword({"userName" : userName, "userPassword": userPassword}, function(isCorrectPw) {
if(isCorrectPw) {
return res.end();
} else {
return next(new ldap.InvalidCredentialsError());
}
});
} else {
return next(new ldap.InvalidCredentialsError());
}
});
server.listen(serverPort, function() {
console.log('Ldap Auth Server listening at ' + server.url);
});
}
{
"name": "LdapAuth",
"version": "1.0.0",
"description": "LDAP Auth bridge",
"main": "server.js",
"directories": {
},
"scripts": {
"test": "echo \"No tests needed!\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/cracker0dks/ldapAuth"
},
"keywords": [
"LDAP",
"Auth",
"bridge"
],
"dependencies": {
"ldapjs": "*"
},
"author": "Cracker0dks",
"license": "MIT"
}
var ldapAuth = require('./ldapAuth');
ldapAuth.init({
ldapServerPassword : "asdg",
warningCallback : function(warning) {
console.log("warning",warning);
},
errorCallback : function(error) {
console.log("error",error);
},
checkPassword : function(auth, isPasswordCorrectCallback) {
var userName = auth["userName"];
var userPassword = auth["userPassword"];
if(userName=="test" && userPassword=="test") {
isPasswordCorrectCallback(true);
} else {
isPasswordCorrectCallback(false);
}
}
});
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment