使用ldap-authentication
库https://www.npmjs.com/package/ldap-authentication
官方样例代码:
let authenticated = await authenticate({
ldapOpts: { url: 'ldap://ldap.forumsys.com' },
userDn: 'uid=gauss,dc=example,dc=com',
userPassword: 'password',
userSearchBase: 'dc=example,dc=com',
usernameAttribute: 'uid',
username: 'gauss',
})
我实际操作连我们单位ldap域的代码:
const { authenticate } = require('ldap-authentication');
let authenticated = await authenticate({
ldapOpts: { url: 'ldaps://ldap.xiaocaicai.com', tlsOptions: { rejectUnauthorized: false } },
userDn: 'username@xx.com',
userPassword: 'thepassword',
userSearchBase: 'DC=xiaocaicai,DC=com',
usernameAttribute: 'sAMAccountName',
username: 'username',
});
console.log(JSON.stringify(authenticated));