从活动目录中读取objectGUID

时间:2017-10-18 13:04:15

标签: javascript node.js active-directory ldap

我尝试使用node.js从AD获取信息。我已经尝试了activedirectoryldapauth-fork,一般来说代码都有效,但如果我需要octetstring这样的objectGUID数据,我会在对象中看到一个垃圾字符串。我found将二进制数据转换为字符串为utf-8。但问题是转换过程中数据损坏了(很多代码都是65533代码),我无法将字符串恢复为原始二进制文件。

如何以octetstring格式访问数据以获得正确的二进制表示?

const ActiveDirectory = require('activedirectory');

const config = {
  url: 'LDAP://ldap.example.com',
  baseDN: 'OU=Users,DC=example,DC=com',
  username: 'user@example.com',
  password: 'password'
};

const ad = new ActiveDirectory(config);

const query = { 
  filter: '(objectClass=user)',
  attributes: ["dn", "cn", "objectGUID", "objectSid"]
};

ad.findUsers(query, function (err, result) {
  if (err) {
    return console.error(err);
  }

  console.log(result.length);
  console.log(result[0]); // objectGUID contains rubbish
  console.log([...result[0].objectGUID].map(ch => ch.charCodeAt(0)));
});

相关:

1 个答案:

答案 0 :(得分:0)

entryParser就是出于此目的:

const ActiveDirectory = require('activedirectory');

const config = {
  url: 'LDAP://ldap.example.com',
  baseDN: 'OU=Users,DC=example,DC=com',
  username: 'user@example.com',
  password: 'password',
  entryParser(entry, raw, callback) {
    if (raw.hasOwnProperty("objectGUID")) { entry.objectGUID = raw.objectGUID; }
    callback(entry);
  }
};

const ad = new ActiveDirectory(config);

const query = { 
  filter: '(objectClass=user)',
  attributes: ["dn", "cn", "objectGUID", "objectSid"]
};

ad.findUsers(query, function (err, result) {
  if (err) {
    return console.error(err);
  }

  console.log(result.length);
  console.log(result[0]); // objectGUID contains Buffer with strange byte order
  console.log(result[0].objectGUID
    .toString('hex')
    .replace(
      /^(..)(..)(..)(..)(..)(..)(..)(..)(..)(..)(..)(..)(..)(..)(..)(..)$/,
      "{$4$3$2$1-$6$5-$8$7-$10$9-$16$15$14$13$12$11}"
    ).toUpperCase() // Normal guid, conversion could be moved into the parser
  );
});