Created
November 5, 2019 17:17
-
-
Save karatechops/92dafa6a1bb55222785515a8d0cc1f5f 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
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