Created
July 6, 2017 16:54
-
-
Save nomoney4me/65af6c1370fb5c104e6c0f38011e124e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('dotenv').config() | |
var ldap = require('ldapjs') | |
, Promise = require('bluebird') | |
, express = require('express') | |
, app = express() | |
//process.env.NOD_TLS_REJECT_UNAUTHORIZED = "0"; | |
var ldapClient = ldap.createClient({ | |
url: process.env.ldaphost | |
}); | |
const client = Promise.promisifyAll(ldapClient) | |
const searchToPromise = function(res) { | |
return new Promise((resolve, reject) => { | |
const results = []; | |
res.on('searchEntry', (entry) => { | |
results.push(entry.object) | |
}); | |
res.on('error', (err) => { | |
reject(err) | |
}); | |
res.on('end', (result) => { | |
resolve(results[0]) | |
}); | |
}) | |
} | |
function encodePassword(password) { | |
var newPassword = ''; | |
password = "\"" + password + "\""; | |
for(var i = 0; i < password.length; i++){ | |
newPassword += String.fromCharCode( password.charCodeAt(i) & 0xFF,(password.charCodeAt(i) >>> 8) & 0xFF); | |
} | |
return newPassword; | |
} | |
app.get('/', (req, res) => { | |
if(req.query.username) { | |
Promise.try(() => { | |
return client.bindAsync(process.env.ldapuser+'@mydomain.org', process.env.ldappass) | |
}).then(() => { | |
const opts = { | |
filter: '(sAMAccountName='+req.query.username+')', | |
scope: 'sub' | |
} | |
return client.searchAsync('ou=Accounts, dc=mydomain, dc=org', opts) | |
}).then((res) => { | |
return searchToPromise(res) | |
}).then((data) => { // this is where I have my user info | |
let change = { | |
operation:'replace', | |
modification: { | |
//unicodePwd: encodePassword("mypassiscool") | |
name:'this is a user test' | |
} | |
} | |
return data.dn | |
//return client.modifyAsync(data.dn, change) | |
}).then((result) => { | |
res.json(result) | |
}).catch((err) => { | |
console.warn(err) | |
}) | |
}else { | |
res.send('invalid username') | |
} | |
}) | |
app.listen(6000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment