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

时间:2018-09-07 08:55:35

标签: node.js authentication authorization distinguishedname ldapjs

我将用户绑定到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);
  });

});

1 个答案:

答案 0 :(得分:0)

找到它,可能会对某人有所帮助)

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

$auth = [
    "ver:authentication" => [
        "pw" => $this->pw,
        "user" => $this->user
    ]
];

$options = [];
$options["trace"] = TRUE;
$options["cache_wsdl"] = WSDL_CACHE_NONE;
$options["compression"] = SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP;

$client = new SoapClient("www.my-url.com/wsdl", $options);

$header = new SoapHeader("www.my-url.com", "authentication", $auth, false);
$client->__setSoapHeaders($header);

相关问题