ldapjs连接到LDAP(ldap.forumsys.com)失败

时间:2018-08-01 06:02:43

标签: ldap openldap ldapjs

这是在线LDPA测试服务器,http://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/

所以我做了一些简单的脚本来测试它,但是我总是收到不需要的响应。

这是我的代码:

const ldap = require('ldapjs');
const assert = require('assert');

// LDAP Connection Settings
const server = "ldap.forumsys.com";
const uid = "tesla"
const password = "password"; // User password

// Create client and bind to AD
const client = ldap.createClient({
    url: `ldap://${server}`
});

// Search AD for user
const searchOptions = {
  filter: '(uid=${uid})'
};

// client.bind(`uid=tesla,dc=example,dc=com`,password,err => {
//     assert.ifError(err);
// });

client.search(`cn=read-only-admin,dc=example,dc=com`,searchOptions,(err,res) => {
    assert.ifError(err);

    res.on('searchEntry', entry => {
        console.log(entry.object.name);
    });
    res.on('searchReference', referral => {
        console.log('referral: ' + referral.uris.join());
    });
    res.on('error', err => {
        console.error('error: ' + err.message);
    });
    res.on('end', result => {
        console.log(result);
    });
});


// Wrap up
client.unbind( err => {
    assert.ifError(err);
});

我正在通过运行app.js来恢复这一点

SearchResponse {
  messageID: 1,
  protocolOp: 101,
  controls: [],
  log: 
   Logger {
     domain: null,
     _events: {},
     _eventsCount: 0,
     _maxListeners: undefined,
     _isSimpleChild: true,
     _level: 30,
     streams: [ [Object] ],
     serializers: { req: [Function], res: [Function], err: [Function] },
     src: false,
     fields: 
      { name: 'ldapjs',
        component: 'client',
        hostname: 'will-ThinkPad-T440p',
        pid: 17485,
        clazz: 'Client' } },
  status: 0,
  matchedDN: '',
  errorMessage: '',
  referrals: [],
  connection: 
   Socket {
     connecting: false,
     _hadError: false,
     _handle: 
      TCP {
        reading: true,
        owner: [Circular],
        onread: [Function: onread],
        onconnection: null,
        writeQueueSize: 0 },
     _parent: null,
     _host: 'ldap.forumsys.com',
     _readableState: 
      ReadableState {
        objectMode: false,
        highWaterMark: 16384,
        buffer: [Object],
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: true,
        ended: false,
        endEmitted: false,
        reading: false,
        sync: false,
        needReadable: true,
        emittedReadable: false,
        readableListening: false,
        resumeScheduled: false,
        destroyed: false,
        defaultEncoding: 'utf8',
        awaitDrain: 0,
        readingMore: false,
        decoder: null,
        encoding: null },
     readable: true,
     domain: null,
     _events: 
      { finish: [Function: onSocketFinish],
        _socketEnd: [Function: onSocketEnd],
        data: [Function: onData],
        close: [Object],
        end: [Function: onEnd],
        error: [Function: onSocketError],
        timeout: [Function: onTimeout] },
     _eventsCount: 7,
     _maxListeners: undefined,
     _writableState: 
      WritableState {
        objectMode: false,
        highWaterMark: 16384,
        finalCalled: false,
        needDrain: false,
        ending: false,
        ended: false,
        finished: false,
        destroyed: false,
        decodeStrings: false,
        defaultEncoding: 'utf8',
        length: 0,
        writing: false,
        corked: 0,
        sync: false,
        bufferProcessing: false,
        onwrite: [Function: bound onwrite],
        writecb: null,
        writelen: 0,
        bufferedRequest: null,
        lastBufferedRequest: null,
        pendingcb: 0,
        prefinished: false,
        errorEmitted: false,
        bufferedRequestCount: 0,
        corkedRequestsFree: [Object] },
     writable: true,
     allowHalfOpen: false,
     _bytesDispatched: 79,
     _sockname: null,
     _pendingData: null,
     _pendingEncoding: '',
     server: null,
     _server: null,
     [Symbol(asyncId)]: 8,
     [Symbol(bytesRead)]: 0 },
  attributes: [],
  notAttributes: [],
  sentEntries: 0 }

其中不包含有关“特斯拉”的任何信息...

2 个答案:

答案 0 :(得分:0)

可能是这个部分:

// Search AD for user
const searchOptions = {
  filter: '(uid=${uid})'
};

需要像上面的 url 设置那样的反引号吗?如果这是一个动态值,您应该添加它们以将其转换为字符串文字,例如:

// Search AD for user
const searchOptions = {
  filter: `(uid=${uid})`
};

答案 1 :(得分:0)

const searchOptions = {
  filter: '(uid=${uid})'
};

以上部分不正确。 应该

const searchOptions = {
  filter: `(uid=${uid})`
};

我认为 Gustav 已经给出了正确答案。但除此之外,原因是使用 ' 时,变量替换不会像您预期的那样发生。要构建这样的字符串,您需要使用 `

无论如何,既然你已经写了一个 LDAP 测试服务器,那么已经有写的测试服务器来实现,例如,你可以使用

https://hub.docker.com/r/upekshejay/simple-ldap-test-server