从ldapjs搜索中获取特定对象属性

问题描述 投票:1回答:1

我绑定并认证用户到ldap,如下面的代码所示。现在我得到了对象的所有属性,我想要的只是获得'distingushedName'。在ldapjs中有一个方法吗?这是一个过滤问题吗?

谢谢 !

'use strict';
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var ldap = require('ldapjs');
const assert = require('assert');

var creds = {
  url: "ldap://************.local:389",
  bindDN: "DC=***,DC=***,DC=local"
};

var opts = {
  filter: "(&(objectClass=user))",
  scope: "sub",
  client: "*"
};
//binding
function authDN(user, baseDN, password, cb) {
  client.bind(baseDN, password, function (err) {
    client.unbind();
    cb(err === null, err);
  });
}

function output(res, err) {				
  if (res) {
    console.log('success :' + res);
  } else {
    console.log(['Error :',err.code, err.dn, err.message ]);
  }
}

var client = ldap.createClient(creds);
authDN(client, 'username', 'password', output);

//search
  client.search('CN=*** ,OU=****,...,OU=****,DC=***,DC=***,DC=local', opts, function(err, res) {
  assert.ifError(err);

  res.on('searchEntry', function(entry) {
    console.log('entry: ' + JSON.stringify(entry.object));
  });
  res.on('searchReference', function(referral) {
    console.log('referral: ' + referral.uris.join());
  });
  res.on('error', function(err) {
    console.error('error: ' + err.message);
  });
  res.on('end', function(result) {
    console.log('status: ' + result.status);
    console.log('result: ' + result);
    process.exit(1);
  });

});
node.js authentication authorization distinguishedname ldapjs
1个回答
0
投票

发现它,它可能会帮助某人=)

我刚刚添加(属性:['distinguishedName'])到以下选项:

var opts = {
  filter: "(&(objectClass=user))",
  scope: "sub",
  client: "*",
  attributes: ['distinguishedName']
};
© www.soinside.com 2019 - 2024. All rights reserved.