【问题标题】:How to get profile of user I have binded as using ldapjs如何获取我已绑定为使用 ldapjs 的用户的个人资料
【发布时间】:2021-02-01 12:56:54
【问题描述】:

我正在使用ldapjs 包。我正在使用这段代码,它允许我 使用 readonly 凭据绑定到 ldap 服务器并从 ou=people 提取用户配置文件之一。

'use strict';

// Figure 1

const ldap = require('ldapjs');

const ldapClient = ldap.createClient({
  url: 'ldap://127.0.0.1:389'
});

const username = 'cn=readonly,dc=vodolaz095,dc=life';
const password = 'readonly';

ldapClient.bind(
  username,
  password,
  function (error) {
    if (error) {
      throw error;
    }
    console.log('bind performed');

    ldapClient.search('ou=people,dc=vodolaz095,dc=life', {
      filter: `(uid=vodolaz095)`,
      scope: 'one',
      attributes: ['uid', 'dn', 'cn', 'mail']
    }, function (error, res) {
      if (error) {
        throw error;
      }
      res.on('searchEntry', function (data) {
        // console.log('Data found', data);
        console.log('Data object', JSON.stringify(data.object, null, 2));
      });
      res.once('error', function(error){
        throw error;
      });
      res.once('end', function () {
        console.log('Completed');
        process.exit(0)
      });
    });
  }
);

现在,我将用户名和密码更改为受限用户的用户名和密码,我已通过 readonly 凭据提取并执行相同的代码:


// same code as in figure 1

const username = 'uid=vodolaz095,ou=people,dc=vodolaz095,dc=life';
const password = 'thisIsNotAPassword123';

// same code as in figure 1

我可以绑定到 ldap 服务器,没问题。但是当我尝试获取自己的个人资料时,它返回给我 NoSuchObjectError: No such Object 错误

所以,问题是:我如何在openldap 中获取我绑定为的用户的个人资料? 比如,我怎样才能使whoami 命令?

【问题讨论】:

    标签: node.js ldap openldap ldapjs


    【解决方案1】:

    您可以通过使用您的 bindDN 设置 base 搜索来获取您绑定的用户的条目,并将范围设置为 base(没有任何过滤器)。

    所以如果 username 是 bindDN,这应该可以工作:

    ldapClient.search(username, {
      scope: 'base',
      attributes: ['uid', 'dn', 'cn', 'mail']
    }
    

    UPD:按预期工作的完整代码示例:

    
    
    'use strict';
    
    const ldap = require('ldapjs');
    
    const ldapClient = ldap.createClient({
      url: 'ldap://127.0.0.1:389'
    });
    
    
    const username = 'uid=vodolaz095,ou=people,dc=vodolaz095,dc=life'
    const password = 'thisIsVerySecureSecretPassword';
    
    
    ldapClient.bind(
      username,
      password,
      function (error) {
        if (error) {
          throw error;
        }
        console.log('bind performed');
    
        ldapClient.search('uid=vodolaz095,ou=people,dc=vodolaz095,dc=life', { // notice, full id of user profile here
          filter: `(uid=vodolaz095)`, // seems to be ignored, same result, if filter used or not used
          scope: 'base', // important
          attributes: ['uid', 'dn', 'cn', 'mail']
        }, function (error, res) {
          if (error) {
            throw error;
          }
          res.on('searchEntry', function (data) {
            // console.log('Data found', data);
            console.log('Data object', JSON.stringify(data.object, null, 2));
          });
          res.once('error', function (error){
            throw error;
          });
          res.once('end', function () {
            console.log('All passed');
            process.exit(0);
          });
        });
      }
    );
    
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-16
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      • 2014-09-27
      • 1970-01-01
      相关资源
      最近更新 更多