美文网首页
使用js对接ldap

使用js对接ldap

作者: 清霆 | 来源:发表于2022-09-02 15:23 被阅读0次
  • demo
import ldap from 'ldapjs';

// DC的网络路径
const server = '10.1.10.1';
// userPrincipalName 带邮箱后缀的 登录名
const userPrincipalName = 'administrator@abc.com';
const password = 'password';

const authentication = (userPrincipalName, password) => {
  return new Promise((resolve, reject) => {
    // DC的目录
    const adSuffix = 'DC=abc,DC=com';

    const success = (userInfo) => {
      client.unbind();
      resolve(userInfo);
    };

    const fail = (e) => {
      client.unbind();
      reject(e?.message ?? e);
    };

    // 建立连接,默认使用端口389
    const client = ldap.createClient({
      url: `ldap://${server}`,
    });
    // 鉴权
    client.bind(userPrincipalName, password, (error, res) => {
      if (error) {
        return fail(error);
      }
    });

    // 用户查询条件
    const searchOptions = {
      scope: 'sub',
      filter: `(userPrincipalName=${userPrincipalName})`,
      attributes: [
        'displayName',
        'name',
        'sAMAccountName',
        'userPrincipalName',
        'objectGUID',
        'objectSid',
        'accountExpires',
      ],
    };

    // 查询用户信息
    client.search(adSuffix, searchOptions, (error, res) => {
      if (error) {
        return fail(error);
      }

      res.on('searchEntry', (entry) => {
        const userInfo = entry.object;
        userInfo.objectGUID = formatGUID(entry);
        userInfo.accountExpires =
          Math.floor(userInfo.accountExpires / 10000) - 11644473600000;
        if (userInfo.accountExpires < Date.now()) {
          return fail('用户已禁用');
        }
        return success(userInfo);
      });
      res.on('error', (error) => {
        return fail(error);
      });
      res.on('end', () => {});
    });
  });
};

function formatGUID(entry){
  if(!Array.isArray(entry.attributes))
      throw new Error('Attributes must be an array');

  const binaryGUID = entry.attributes.find(attribute => attribute.type === 'objectGUID').buffers[0];
  const guidFormat = [
      [3,2,1,0],
      [5,4],
      [7,6],
      [8,9],
      [10,11,12,13,14,15]
  ];

  const guidArray = guidFormat.map( part => {
      const stringPart = part.map(byte => {
          const byteString = binaryGUID[byte] < 16 ?
              `0${binaryGUID[byte].toString(16)}` :
              binaryGUID[byte].toString(16)

          return byteString
      });
      return `${stringPart.join('')}`;
  });
  return guidArray.join('-');
}

const index = async () => {
  try {
    const userInfo = await authentication(userPrincipalName, password);
    console.log('userInfo', userInfo);
  } catch (e) {
    console.log('index:e', e);
  }
};
index();
{
  "dn": "CN=Admin,OU=Users,DC=abc,DC=com",
  "controls": [],
  "displayName": "Admin",
  "name": "Admin",
  "objectGUID": "��!����A��8�ŢZ�",
  "objectSid": "\u0001\u0002\u0000\u0000\u0000\u0000\u0000\u0003\u0012\u0000\u0000\u0000\u0011L�q�d�]\u000e\u0012C֖\u0012\u0000\u0000",
  "accountExpires": "9223372036854775807",
  "sAMAccountName": "admin",
  "userPrincipalName": "administrator@abc.com"
}
  • 登录鉴权的几种方式
const client = ldap.createClient({
  url: `ldap://${server}`,
});
const password = '';

// bind to AD by userPrincipalName
const userPrincipalName = 'admin@abc.com';
client.bind(userPrincipalName, password, (err, res) => {
  // ...
});

// bind to AD by CN(DN)
const CN = 'CN=Admin,OU=Users,DC=abc,DC=com';
client.bind(CN, password, (err, res) => {
  // ...
});

// bind to AD by sAMAccountName?
// 使用任意已知账号如管理员账号进行bind操作,
// bind成功后通过search操作以传入的sAMAccountName为搜索条件进行搜索
// 从搜索结果拿到userPrincipalName或DN后进行登录鉴权操作

相关文章

  • 使用js对接ldap

    demo 登录鉴权的几种方式

  • k8s接入ldap

    为了对接上 LDAP,可谓是煞费苦心。网上能找到的对接上 LDAP 的方案,都得在 LDAP 上自定义一个 tok...

  • jira对接ldap

    对接LDAP配置 管理员账号登陆jira后,点击 用户管理image.png 用户管理 -> 用户目录-> 添加目...

  • centos下 docker方式安装openldap服务及原数据

    原ldap服务器使用slapd.conf文件配置,新ldap服务使用docker安装openldap,新的ldap...

  • ldap-mongo-server

    ldap-mongo-server 基于node.js快速构建一个Ldap server(AD域) 项目地址 地址...

  • Python对接LDAP/AD的过程详解

    不同公司的 LDAP/AD 服务配置各不相同,很难封装一个通用的方法,所以我们在对接 LDAP/AD 的过程中,需...

  • xxl-job对接ldap

    0、pom.xml 1、LdapConfiguration.java 2、LdapProperties.java ...

  • python3 使用ldap3来作为django认证后台

    首先先使用ldap3测试ldap服务是否正常 我们先要拿到dc的数据,以及连接ldap的密码,还有搜索的字段(se...

  • gitlab配置ldap

    使用gitlab 配置ldap 配置文件路径/etc/gitlab/gitlab.rb 文件中配置的ldap 配置...

  • Java反射实战

    背景 使用spring-ldap操作LDAP,完成LdapTemplate设置后,进行多条件查询,因为需求是能够多...

网友评论

      本文标题:使用js对接ldap

      本文链接:https://www.haomeiwen.com/subject/yrxynrtx.html