I'm trying to search in ldapjs using method 'search', but it is not working for me.
Here is response from terminal :
> Result is: SearchResponse {
messageID: 2,
protocolOp: 101,
controls: [],
log:
Logger {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
_isSimpleChild: true,
_level: 30,
streams: [ [Object] ],
serializers:
{ req: [Function: req],
res: [Function: res],
err: [Function: err] },
src: false,
fields:
{ name: 'ldapjs',
ponent: 'client',
hostname: 'nichita-Lenovo-ideapad-500-15ISK',
pid: 29307,
clazz: 'Client' } },
status: 0,
matchedDN: '',
errorMessage: '',
referrals: [],
connection:
TLSSocket {
_tlsOptions:
{ pipe: undefined,
secureContext: [Object],
isServer: false,
requestCert: true,
rejectUnauthorized: true,
session: undefined,
NPNProtocols: undefined,
requestOCSP: undefined },
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: null,
npnProtocol: undefined,
authorized: true,
authorizationError: null,
encrypted: true,
_events:
{ finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
secure: [Function],
data: [Function: onData],
close: [Object],
end: [Function: onEnd],
error: [Function: onSocketError],
timeout: [Function: onTimeout] },
_eventsCount: 8,
_connecting: false,
_hadError: false,
_handle:
TLSWrap {
_externalStream: {},
fd: 13,
_parent: [Object],
_parentWrap: undefined,
_secureContext: [Object],
reading: true,
owner: [Circular],
onread: [Function: onread],
writeQueueSize: 1,
onhandshakestart: [Function],
onhandshakedone: [Function: bound ],
onocspresponse: [Function: bound onocspresponse],
onerror: [Function] },
_parent: null,
_host: 'ldap.titanium-soft',
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: [],
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: false,
endEmitted: false,
reading: false,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null,
resumeScheduled: false },
readable: true,
domain: null,
_maxListeners: undefined,
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
needDrain: false,
ending: false,
ended: false,
finished: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: false,
bufferProcessing: false,
onwrite: [Function],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false },
writable: true,
allowHalfOpen: false,
destroyed: false,
bytesRead: 28,
_bytesDispatched: 134,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
ssl:
TLSWrap {
_externalStream: {},
fd: 13,
_parent: [Object],
_parentWrap: undefined,
_secureContext: [Object],
reading: true,
owner: [Circular],
onread: [Function: onread],
writeQueueSize: 1,
onhandshakestart: [Function],
onhandshakedone: [Function: bound ],
onocspresponse: [Function: bound onocspresponse],
onerror: [Function] },
server: undefined,
_requestCert: true,
_rejectUnauthorized: true,
read: [Function],
_consuming: true },
attributes: [],
notAttributes: [],
sentEntries: 0 }
Here is code:
const ldap = require('ldapjs');
const ldapOptions = {
url: 'ldaps://example',
port: '363'
};
const client_admin = ldap.createClient(ldapOptions);
let searchUser = () => {
client_admin
.bind('cn=admin,dc=example,dc=', 'secret', (err) => {
if (err) return console.log('Error is', err);
});
let opts = {
filter: 'cn=admin'
}
client_admin.search('dc=example,dc=', opts, (err, res) => {
res.on('searchEntry', (entry) => {
console.log('Entry', JSON.stringify(entry.object));
});
res.on('searchReference', (referral) => {
// console.log('Referral', referral);
});
res.on('error', (err) => {
// console.log('Error is', err);
});
res.on('end', (result) => {
console.log('Result is', result);
})
})
};
Tried to add new user, successfully. But i didn't got any response. How can i get any data using this method?
I'm trying to search in ldapjs using method 'search', but it is not working for me.
Here is response from terminal :
> Result is: SearchResponse {
messageID: 2,
protocolOp: 101,
controls: [],
log:
Logger {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
_isSimpleChild: true,
_level: 30,
streams: [ [Object] ],
serializers:
{ req: [Function: req],
res: [Function: res],
err: [Function: err] },
src: false,
fields:
{ name: 'ldapjs',
ponent: 'client',
hostname: 'nichita-Lenovo-ideapad-500-15ISK',
pid: 29307,
clazz: 'Client' } },
status: 0,
matchedDN: '',
errorMessage: '',
referrals: [],
connection:
TLSSocket {
_tlsOptions:
{ pipe: undefined,
secureContext: [Object],
isServer: false,
requestCert: true,
rejectUnauthorized: true,
session: undefined,
NPNProtocols: undefined,
requestOCSP: undefined },
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: null,
npnProtocol: undefined,
authorized: true,
authorizationError: null,
encrypted: true,
_events:
{ finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
secure: [Function],
data: [Function: onData],
close: [Object],
end: [Function: onEnd],
error: [Function: onSocketError],
timeout: [Function: onTimeout] },
_eventsCount: 8,
_connecting: false,
_hadError: false,
_handle:
TLSWrap {
_externalStream: {},
fd: 13,
_parent: [Object],
_parentWrap: undefined,
_secureContext: [Object],
reading: true,
owner: [Circular],
onread: [Function: onread],
writeQueueSize: 1,
onhandshakestart: [Function],
onhandshakedone: [Function: bound ],
onocspresponse: [Function: bound onocspresponse],
onerror: [Function] },
_parent: null,
_host: 'ldap.titanium-soft.',
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: [],
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: false,
endEmitted: false,
reading: false,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null,
resumeScheduled: false },
readable: true,
domain: null,
_maxListeners: undefined,
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
needDrain: false,
ending: false,
ended: false,
finished: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: false,
bufferProcessing: false,
onwrite: [Function],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false },
writable: true,
allowHalfOpen: false,
destroyed: false,
bytesRead: 28,
_bytesDispatched: 134,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
ssl:
TLSWrap {
_externalStream: {},
fd: 13,
_parent: [Object],
_parentWrap: undefined,
_secureContext: [Object],
reading: true,
owner: [Circular],
onread: [Function: onread],
writeQueueSize: 1,
onhandshakestart: [Function],
onhandshakedone: [Function: bound ],
onocspresponse: [Function: bound onocspresponse],
onerror: [Function] },
server: undefined,
_requestCert: true,
_rejectUnauthorized: true,
read: [Function],
_consuming: true },
attributes: [],
notAttributes: [],
sentEntries: 0 }
Here is code:
const ldap = require('ldapjs');
const ldapOptions = {
url: 'ldaps://example.',
port: '363'
};
const client_admin = ldap.createClient(ldapOptions);
let searchUser = () => {
client_admin
.bind('cn=admin,dc=example,dc=', 'secret', (err) => {
if (err) return console.log('Error is', err);
});
let opts = {
filter: 'cn=admin'
}
client_admin.search('dc=example,dc=', opts, (err, res) => {
res.on('searchEntry', (entry) => {
console.log('Entry', JSON.stringify(entry.object));
});
res.on('searchReference', (referral) => {
// console.log('Referral', referral);
});
res.on('error', (err) => {
// console.log('Error is', err);
});
res.on('end', (result) => {
console.log('Result is', result);
})
})
};
Tried to add new user, successfully. But i didn't got any response. How can i get any data using this method?
Share Improve this question asked Mar 21, 2017 at 12:48 Никита УрсуНикита Урсу 611 gold badge1 silver badge5 bronze badges 2- So which is it, are you searching for users or adding? Which part of your code is failing? Expected behaviour and actual behaviour? – Arg0n Commented Mar 21, 2017 at 12:52
- I am searching. I tried to add new user in ldap, it was successfully . But when i trying to search , it's wrong – Никита Урсу Commented Mar 21, 2017 at 12:55
3 Answers
Reset to default 6At the very first step after creating the LDAP client, you need to do the binding using the username and password. So, I guess the username you have used is not correct.
This works for me: (The values shown here are of course different):
var ldap = require('ldapjs');
var tlsOptions = {
host: 'example.',
port: '636',
ca: [fs.readFileSync('/path/to/cert.pem')]
};
var client = ldap.createClient({
url: 'ldaps://example.:636',
tlsOptions: tlsOptions
});
client.bind(username, password, function (err) {
if (err) {
console.log('Error occurred while binding');
} else {
var base = 'cn=admin,dc=example,dc=';
var search_options = {
scope: 'sub',
filter: '(&(objectClass=*)(CN=' + username + '))',
attrs: 'memberOf'
};
client.search(base, search_options, function (err, res) {
if (err) {
console.log('Error occurred while ldap search');
} else {
res.on('searchEntry', function (entry) {
console.log('Entry', JSON.stringify(entry.object));
});
res.on('searchReference', function (referral) {
console.log('Referral', referral);
});
res.on('error', function (err) {
console.log('Error is', err);
});
res.on('end', function (result) {
console.log('Result is', result);
});
}
});
}
});
//here is how I get the result: see the entries variable
// SEARCH OPTIONS
let searchAllUsersOptions = {
attributes: [
"dc",
"createTimestamp",
"modifyTimestamp",
"pwdPolicySubentry"
],
scope: "sub",
filter: "(objectClass=person)"
};
// SET DN
let searchAllUsersDN = "dc=example,dc=";
//THIS IS HOW YOU GET THE RESULTS OUT OF THE CLIENT SEARC
let entries = [];
client.search(searchAllUsersDN, searchAllUsersOptions, (err, res) => {
if (err) return reject(err);
res.on('searchEntry', function (entry) {
var r = entry.object;
entries.push(r);
//console.log('search find one user');
//console.log(entries);
});
res.on('error', function (err) {
reject(err);
});
res.on('end', function (result) {
//resolve(entries);
});
});
router.get('/', (req, res) => {
res.send(JSON.stringify(entries) );
});
So is there anyway to search whole LDAP three without binding user with login and password? code below returns error: Operations Error in console.
let searchAllUsersOptions = {
attributes: [
"dc",
"createTimestamp",
"modifyTimestamp",
"pwdPolicySubentry"
],
scope: "sub",
filter: "(objectClass=person)"
};
// SET DN
let searchAllUsersDN = "dc=example,dc=";
//THIS IS HOW YOU GET THE RESULTS OUT OF THE CLIENT SEARC
let entries = [];
client.search(searchAllUsersDN, searchAllUsersOptions, (err, res) => {
if (err) return reject(err);
res.on('searchEntry', function (entry) {
var r = entry.object;
entries.push(r);
//console.log('search find one user');
//console.log(entries);
});
res.on('error', function (err) {
reject(err);
});
res.on('end', function (result) {
//resolve(entries);
});
});
router.get('/', (req, res) => {
res.send(JSON.stringify(entries) );
});