LDAP
Read users and groups from an LDAP server such as Active Directory with LDAP().
LDAP(options, [callback]) reads users or groups from an LDAP server such as Active Directory. It binds with a user and a password, runs one search and returns the entries. Without a callback it returns a promise.
var users = await LDAP({
ldap: { host: 'ad.example.com', port: 389 },
user: 'CN=reader,CN=Users,DC=example,DC=com',
password: CONF.ldap_password,
dn: 'CN=Users,DC=example,DC=com',
type: 'person'
});
// [{ cn: 'Peter', mail: 'peter@example.com', memberOf: ['CN=Admins,...', ...], ... }]Each entry is an object with the attributes of the entry. A single value is a string, several values are an array of strings, and objectGUID and objectSid are hex strings. type is the objectClass to search for (person, group...).
To read one account, use type: 'profile' with login (the sAMAccountName); the result is one object or null. Without dn the function binds as user and returns the entry of that user, which is a simple way to check a login:
var profile = await LDAP({ ldap: { host: 'ad.example.com' }, user: 'peter@example.com', password: password });ldap.secure: true connects with TLS (set the port, usually 636) and ldap.timeout limits the connection time in milliseconds. noauth: true skips the bind.
Attributes are text by default. The attributes option tells how to read the others, for example a photo:
var profile = await LDAP({
ldap: { host: 'ad.example.com' },
user: 'peter@example.com',
password: password,
attributes: { thumbnailPhoto: 'buffer', employeeNumber: 'number', whenCreated: 'date' }
});The types are string, number, date, buffer (or binary) and the encodings hex, base64, ascii and utf8.
A refused bind (a wrong user or password) is an error: the text of the server, or LDAP bind error 49 when the server sends no text. The error is a string:
try {
var profile = await LDAP({ ldap: { host: 'ad.example.com' }, user: 'peter@example.com', password: password });
} catch (err) {
// 'LDAP bind error 49' or the text of the server
}