ToB项目身份认证AD集成(三完):利用ldap.js实现与windows AD对接实现用户搜索、认证、密码修改等功能 - 以及针对中文转义问题的补丁方法

简介: 本文详细介绍了如何使用 `ldapjs` 库在 Node.js 中实现与 Windows AD 的交互,包括用户搜索、身份验证、密码修改和重置等功能。通过创建 `LdapService` 类,提供了与 AD 服务器通信的完整解决方案,同时解决了中文字段在 LDAP 操作中被转义的问题。

在前面的两篇文章中,我详细的介绍了使用ldap与window AD服务集成,实现ToB项目中的身份认证集成方案,包括技术方案介绍、环境配置:
ToB项目身份认证AD集成(一):基于目录的用户管理、LDAP和Active Directory简述
ToB项目身份认证AD集成(二):一分钟搞定window server 2003部署AD域服务并支持ssl加密

在本文中,我将详细介绍如何利用 ldapjs 库使之一个 Node.js 服务类 LdapService,该类实现了与 之前搭建的Windows AD 交互,包括用户搜索、身份验证、密码修改等功能。

也算是AD集成系列的完结吧,后续可能出其它客户端的对接,但目前工作核心在AI那块儿,大概率也不会继续了

一、实现方案和LdapService类概述

LdapService 类的核心是通过 LDAP(轻量级目录访问协议)与 AD 进行交互,提供用户搜索、认证、密码修改、重置等功能。下图是该类的基本结构,后续将一步步的介绍如何实现各个方法。

class LdapService {
   
  client: Promise<ldap.Client>;
  private config: MustProperty<LdapServiceConfig>;
  constructor(config: LdapServiceConfig) {
   
    this.config = {
   
      ...defaultConfig,
      ...config,
    };
    this.client = this.init();
  }
  async findUsers(
    filter = this.config.userSearchFilter,
    attributes: string[] = ["sAMAccountName", "userPrincipalName", "memberOf"]
  ) {
   

  }
  // 关闭连接
  async close() {
   
    (await this.client).destroy();
  }

  async findUser() {
   

  }
  // 修改用户密码的方法
  async changePassword(
    user: LdapUserSimInfo,
    newPassword: string,
    oldPassword: string
  ) {
   

  }
  // 用户认证的方法 - 检查密码是否正确
  async checkPassword(user: LdapUserSimInfo, password: string) {
   

  }
  /*重置密码 */
  async resetPassword(user: LdapUserSimInfo, resetPassword: string) {
   

  }
  private async init() {
   
    const conf = this.config;
    const client = ldap.createClient({
   
      url: conf.url,
      tlsOptions: {
   
        minVersion: "TLSv1.2",
        rejectUnauthorized: false,
      },
    });
    await promisify(client.bind).call(client, conf.adminDN, conf.adminPassword);
    return client; // 返回绑定后的客户端
  }
  private mergeSearchEntryObjectAttrs(entry: ldap.SearchEntryObject) {
   

  }
  private doSearch(client: ldap.Client, opts: ldap.SearchOptions) {
   

  }
  private encodePassword(password) {
   

  }
  private safeDn(dn: string) {
   

  }
}

二、中文字段的特殊patch

ldap.js对于数据的字段进行了escape操作,会导致中文输入被转化成\xxx的形式,无论是接收的数据还是发送的请求,这时候会导致cn包含中文会出现错。需要用如下方法进行patch,通过在出现问题的rdn上配置unescaped参数控制是否对字符串进行escape(如果不知道啥是escape,参见十六进制转义escape介绍

const oldString = ldap.RDN.prototype.toString;
ldap.RDN.prototype.toString = function () {
   
  return oldString.call(this, {
    unescaped: this.unescaped });
};

加了这个补丁后,就可以控制rdn的转义情况了。

三、用户搜索功能

findUsers() 方法用于在 AD 中搜索用户,返回用户的基本信息。

async findUsers(
  filter = this.config.userSearchFilter,
  attributes: string[] = ["sAMAccountName", "userPrincipalName", "memberOf"]
): Promise<LdapUserSimInfo[]> {
   
    await this.bindAsAdmin();
    const opts = {
   
      filter, 
      scope: "sub", 
      attributes: Array.from(new Set(["distinguishedName", "cn"].concat(attributes))),
    };
    const searchResult = await this.doSearch(await this.client, opts);
    return searchResult.map((user) => {
   
      return this.mergeSearchEntryObjectAttrs(user) as LdapUserSimInfo;
    });
}
  • filter 是用于搜索的 LDAP 过滤器,默认为查找所有用户的 (objectClass=user) 过滤器。
  • attributes 参数允许指定返回哪些用户属性,默认返回 sAMAccountNameuserPrincipalNamememberOf 等属性。
  • 该方法调用了 doSearch() 进行搜索,并通过 mergeSearchEntryObjectAttrs() 整理和转换 AD 返回的用户数据。

doSearch() 方法是实际进行 LDAP 搜索的地方:

private doSearch(client: ldap.Client, opts: ldap.SearchOptions) {
   
    return new Promise<ldap.SearchEntryObject[]>((resolve, reject) => {
   
      const entries = [] as ldap.SearchEntryObject[];
      client.search(this.config.userSearchBase, opts, (err, res) => {
   
        if (err) {
   
          return reject(err);
        }
        res.on("searchEntry", (entry) => {
   
          entries.push(entry.pojo);
        });
        res.on("end", (result) => {
   
          if (result?.status !== 0) {
   
            return reject(new Error(`Non-zero status from LDAP search: ${
     result?.status}`));
          }
          resolve(entries);
        });
        res.on("error", (err) => {
   
          reject(err);
        });
      });
    });
}
  • client.search()ldapjs 提供的一个方法,用于执行搜索操作。搜索结果通过事件 searchEntry 逐条返回,最终在 end 事件时完成。

四、用户认证功能

checkPassword() 方法用于用户身份验证,检查用户输入的密码是否正确。

async checkPassword(user: LdapUserSimInfo, password: string) {
   
    const userDN = user.objectName;
    const client = await this.client;
    await promisify(client.bind).call(client, userDN, password);
}
  • 通过 LDAP 的 bind() 方法,可以尝试使用用户的 DN 和密码进行绑定。如果绑定成功,表示密码正确;否则,会抛出错误,表示认证失败。

五、密码修改功能

changePassword() 方法允许用户修改自己的密码。

async changePassword(user: LdapUserSimInfo, newPassword: string, oldPassword: string) {
   
    await this.bindAsAdmin();
    const userDN = this.safeDn(user.objectName);
    const changes = [
      new ldap.Change({
   
        operation: "delete",
        modification: new ldap.Attribute({
   
          type: "unicodePwd",
          values: [this.encodePassword(oldPassword)],
        }),
      }),
      new ldap.Change({
   
        operation: "add",
        modification: new ldap.Attribute({
   
          type: "unicodePwd",
          values: [this.encodePassword(newPassword)],
        }),
      }),
    ];
    const client = await this.client;
    await promisify(client.modify).call(client, userDN, changes);
}
  • 在修改密码时,LDAP 需要先删除旧密码,再添加新密码。这里使用 ldap.Change 创建修改操作,通过 client.modify() 方法应用到 AD。

六、密码重置功能

resetPassword() 方法允许管理员重置用户的密码:

async resetPassword(user: LdapUserSimInfo, resetPassword: string) {
   
    await this.bindAsAdmin();
    const client = await this.client;
    const userDN = this.safeDn(user.objectName);
    const changes = new ldap.Change({
   
      operation: "replace",
      modification: new ldap.Attribute({
   
        type: "unicodePwd",
        values: [this.encodePassword(resetPassword)],
      }),
    });
    await promisify(client.modify).call(client, userDN, changes);
}
  • 与修改密码不同,重置密码直接使用 replace 操作,替换用户的现有密码。

七、结语

通过对 LdapService 类的逐步解析,相信你已经学会了如何利用 ldapjs 库与 Windows AD 进行交互。在实际使用中,还可以根据业务需求对这个类进行扩展,从而满足大规模企业系统中的用户管理需求。

另外这个中文的问题,暂时还只能是如此打补丁,期待社区修复可能不会那么及时

相关文章
|
1天前
|
存储 JavaScript 数据库
ToB项目身份认证AD集成(一):基于目录的用户管理、LDAP和Active Directory简述
本文介绍了基于目录的用户管理及其在企业中的应用,重点解析了LDAP协议和Active Directory服务的概念、关系及差异。通过具体的账号密码认证时序图,展示了利用LDAP协议与AD域进行用户认证的过程。总结了目录服务在现代网络环境中的重要性,并预告了后续的深入文章。
|
2月前
|
Android开发
【Azure 环境】移动应用 SSO 登录AAD, MSAL的配置为Webview模式时登录页面无法加载
【Azure 环境】移动应用 SSO 登录AAD, MSAL的配置为Webview模式时登录页面无法加载
|
Shell
绕过360/某绒添加管理用户CS插件
绕过360/某绒添加管理用户CS插件
104 0
|
Web App开发 安全 内存技术
新版谷歌Chrome取消对PPAPI插件支持后,浏览器网页打开编辑保存微软Office、金山WPS文档解决方案
最近陆续看到一些大学发布公告,谷歌Chrome取消了对PPAPI插件支持,导致某些在线Office厂家产品将无法在谷歌Chrome107及以上版本运行,被迫更换360浏览器或者使用低版本Chrome浏览器苟延残喘。
378 0
新版谷歌Chrome取消对PPAPI插件支持后,浏览器网页打开编辑保存微软Office、金山WPS文档解决方案
|
Linux Android开发
支付宝二维码脱机认证库测试(linux_x86平台验证)
支付宝二维码脱机认证库测试(linux_x86平台验证)
|
安全 iOS开发 MacOS
【解决方案】MacOS遇到“无法打开xxx,因为Apple无法检查其是否包含恶意软件”,怎么处理。
【解决方案】MacOS遇到“无法打开xxx,因为Apple无法检查其是否包含恶意软件”,怎么处理。
1653 0
【解决方案】MacOS遇到“无法打开xxx,因为Apple无法检查其是否包含恶意软件”,怎么处理。
CDN
|
Web App开发 开发者 Windows
通过组策略管理模板强制允许安装第三方拓展(无禁用提示)
在开发者模式加载拓展吼,每次启动Chrome/Chromium系列浏览器都会有烦人的禁用弹窗,通过Chrome/Edge等提供的组策略模板把来源加入白名单可以从源头上制止这个弹窗
CDN
507 0
|
Java Android开发
CC框架实践(1):实现登录成功再进入目标界面功能
用CC来AOP地实现登录成功后再跳转到目标界面功能
1560 0