Ldapjs搜索不起作用

时间:2017-03-21 12:48:33

标签: javascript ldap ldapjs

我正在尝试使用'搜索'方法搜索ldapjs,但它对我不起作用。

以下是来自终端的回复:

<router-link :to={name: 'ProductDetail', params: {id: some_id, slug: some_slug}}></router-link>

这是代码:

> 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',
        component: '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.com',
     _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 }

尝试成功添加新用户。但我没有得到任何回应。 如何使用此方法获取任何数据?

2 个答案:

答案 0 :(得分:2)

At 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.com',
    port: '636',
    ca: [fs.readFileSync('/path/to/cert.pem')]
};
var client = ldap.createClient({
    url: 'ldaps://example.com: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=com';
        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);
                });
            }
        });
    }
});

答案 1 :(得分:0)

//这是我得到结果的方式:请参阅项目变量

//搜索选项

let searchAllUsersOptions = {
    attributes: [
      "dc",
      "createTimestamp",
      "modifyTimestamp",
      "pwdPolicySubentry"
    ],
    scope: "sub",
    filter: "(objectClass=person)"
  };

  // SET DN
  let searchAllUsersDN = "dc=example,dc=com";

  //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)  );
  });