从活动目录读取objectGUID [英] Read objectGUID from active directory

查看:297
本文介绍了从活动目录读取objectGUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用node.js从AD获取信息.我已经尝试过activedirectoryldapauth-fork,并且代码通常可以正常工作,但是如果我需要一些octetstring数据(例如objectGUID),那么我会在对象中看到一个垃圾字符串.我找到,将二进制数据转换为utf-8字符串.但是问题在于,转换期间数据已损坏(使用65533代码的大量代码),并且我无法将字符串还原为原始二进制.

I'm trying to get information from AD using node.js. I've tried activedirectory and ldapauth-fork and in general the code works, but if I need some octetstring data like objectGUID, I see a rubbish string in the object. I found that binary data is converted into a string as utf-8. But the problem is that the data is damaged during convertion (a lot of cahrs with 65533 code) and I can't revert the string to original binary.

如何获取octetstring格式的数据以获取正确的二进制表示形式?

How can I access data in octetstring format to get correct binary representation?

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)));
});

相关:

  • https://github.com/mcavage/node-ldapjs/issues/228
  • https://github.com/gheeres/node-activedirectory/pull/15

推荐答案

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
  );
});

这篇关于从活动目录读取objectGUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆