Skip to content

Instantly share code, notes, and snippets.

@karatechops
Created November 5, 2019 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karatechops/92dafa6a1bb55222785515a8d0cc1f5f to your computer and use it in GitHub Desktop.
Save karatechops/92dafa6a1bb55222785515a8d0cc1f5f to your computer and use it in GitHub Desktop.
import ldap from 'ldapjs';
import { debug } from './index';
let client;
let timer;
export const getLdapUser = uid =>
new Promise((resolve, reject) => {
client = ldap.createClient({
url: 'ldap://ldap.hp.com',
timeout: 5000,
connectTimeout: 5000
});
const options = {
scope: 'sub',
filter: '(&(hpStatus=Active)(uid=' + uid + '))',
attributes: [ 'hpPictureThumbnailURI', 'cn', 'hpBusinessUnit', 'uid' ],
sizeLimit: 1
};
client.on('error', (err) => reject(err));
client.search('ou=people,o=hp.com', options, (ldapErr, ldapRes) => { // eslint-disable-line consistent-return
const entries = [];
if (ldapErr) {
debug('ldap client error:', ldapErr);
if (client) {
client.unbind();
client = null;
}
return reject(ldapErr);
} else {
ldapRes.on('searchEntry', function (entry) {
entries.push(entry.object);
});
ldapRes.on('error', function (err) {
return reject(err);
});
ldapRes.on('end', function (result) {
return resolve(entries[0]);
});
}
});
// If we're idle for more than 60 seconds. Clean up and start fresh.
clearTimeout(timer);
timer = setTimeout(function () {
client.unbind();
client = null;
}, 60000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment