From c2cd7d48bc68f86d34aa9564059f7eaeb8e3d8f1 Mon Sep 17 00:00:00 2001 From: rofl256 <rofl256@gmail.com> Date: Mon, 26 Feb 2018 22:08:00 +0100 Subject: [PATCH] init commit --- ldapAuth.js | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 25 +++++++++++++ server.js | 21 +++++++++++ 3 files changed, 150 insertions(+) create mode 100644 ldapAuth.js create mode 100644 package.json create mode 100644 server.js diff --git a/ldapAuth.js b/ldapAuth.js new file mode 100644 index 0000000..06fc7ff --- /dev/null +++ b/ldapAuth.js @@ -0,0 +1,104 @@ +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); + }); +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..fa27ae6 --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "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" +} diff --git a/server.js b/server.js new file mode 100644 index 0000000..4ad5f4e --- /dev/null +++ b/server.js @@ -0,0 +1,21 @@ +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 -- GitLab