LDAP
LDAP() and its options.
On this page
API
method
LDAP(options, [callback])
Searches an LDAP server (Active Directory) and returns users or groups.
LDAP(options, [callback])| Parameter | Type | Description |
|---|---|---|
options |
Object |
The connection and the search, see below. |
callback? |
Function |
function(err, response). Without it the function returns a promise. |
Returns Promise With the response when no callback is given.
Binds to the server with user and password (a simple bind), runs one search and returns the entries. The connection is closed afterwards. The search is complete when the server stops sending data for 1.5 seconds.
| Option | Description |
|---|---|
ldap | { host, port, secure, timeout }. port defaults to 389; secure: true connects with TLS; timeout limits the connection in ms |
user, password | Credentials for the bind (a DN or, for Active Directory, also user@domain) |
dn | Base DN of the search |
type | objectClass to search for, such as person or group; profile searches one person by login |
login | The sAMAccountName for type: 'profile' |
noauth | true skips the bind |
attributes | How to read attributes: { thumbnailPhoto: 'buffer' }. Types: string, number, date, buffer, binary (also a buffer), hex, base64, ascii, utf8, in lower case |
The response is an array of objects, or one object (or null) for type: 'profile' and when dn is missing. Without dn the search uses user as the base DN, so you get the entry of the user that logged in. Values are strings, arrays of strings for several values, and hex strings for objectGUID and objectSid.
When the server refuses the bind (a wrong user or password), the error is the text of the server or, when the server sends no text, LDAP bind error <code> (LDAP bind error 49 for invalid credentials). This error is a string, so the promise rejects with a string; the response is null for a profile and an empty array for a search. An invalid ldap.host gives the error Invalid hostname; the host is converted with U.tohost() before the connection.
var groups = await LDAP({
ldap: { host: 'ad.example.com' },
user: 'reader@example.com',
password: CONF.ldap_password,
dn: 'OU=Groups,DC=example,DC=com',
type: 'group'
});
LDAP({ ldap: { host: 'ad.example.com', port: 636, secure: true }, user: 'reader@example.com', password: CONF.ldap_password, dn: 'DC=example,DC=com', type: 'profile', login: 'peter' }, function(err, profile) {
profile && console.log(profile.mail);
});