UNPKG

1.54 kBJavaScriptView Raw
1'use strict'
2const utils = require('./utils')
3
4module.exports = function ({ adminDn, adminPassword, filter, attributes, searchDn }) {
5 return new Promise((resolve, reject) => {
6 // validate input
7 if (
8 (!adminDn || adminDn === '') &&
9 (!adminPassword || adminPassword === '')
10 ) {
11 // inform user the error of their ways
12 return reject('adminDn, and adminPassword are required')
13 }
14 // create client connection
15 const client = this.getClient()
16 // login to LDAP
17 client.bind(adminDn, adminPassword, async (err) => {
18 // console.log('ldap client bind')
19 if (err) {
20 // console.log(err)
21 client.destroy()
22 reject(err)
23 }
24 // figure out which filter to use to identify the user
25 // const filter = '(&(objectClass=user)(OU=CXDemo Users))'
26 // find user by filter
27 const opts = {
28 filter,
29 scope: 'sub',
30 attributes
31 }
32
33 try {
34 let users = []
35 // find the users
36 client.search(searchDn, opts, (err, search) => {
37 if (err) {
38 console.log(err)
39 client.destroy()
40 reject(err)
41 }
42 search.on('searchEntry', (entry) => {
43 users.push(entry.object)
44 })
45 search.on('end', (result) => {
46 // console.log('search.on end result = ', result)
47 client.destroy()
48 resolve(users)
49 })
50 })
51 } catch (e) {
52 client.destroy()
53 reject(e)
54 }
55 })
56 })
57}