Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nomoney4me/65af6c1370fb5c104e6c0f38011e124e to your computer and use it in GitHub Desktop.
Save nomoney4me/65af6c1370fb5c104e6c0f38011e124e to your computer and use it in GitHub Desktop.
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