LDAP
Leggi utenti e gruppi da un server LDAP come Active Directory con LDAP().
LDAP(options, [callback]) legge utenti o gruppi da un server LDAP come Active Directory. Esegue il bind con un utente e una password, esegue una ricerca e restituisce le voci. Senza callback restituisce una 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,...', ...], ... }]Ogni voce è un oggetto con gli attributi della voce. Un singolo valore è una stringa, più valori sono un array di stringhe, e objectGUID e objectSid sono stringhe esadecimali. type è l'objectClass da cercare (person, group...).
Per leggere un account, usa type: 'profile' con login (il sAMAccountName); il risultato è un oggetto o null. Senza dn la funzione esegue il bind come user e restituisce la voce di quell'utente, un modo semplice per verificare un login:
var profile = await LDAP({ ldap: { host: 'ad.example.com' }, user: 'peter@example.com', password: password });ldap.secure: true si connette con TLS (imposta la porta, di solito 636) e ldap.timeout limita il tempo di connessione in millisecondi. noauth: true salta il bind.
Gli attributi sono testo per impostazione predefinita. L'opzione attributes indica come leggere gli altri, per esempio una foto:
var profile = await LDAP({
ldap: { host: 'ad.example.com' },
user: 'peter@example.com',
password: password,
attributes: { thumbnailPhoto: 'buffer', employeeNumber: 'number', whenCreated: 'date' }
});I tipi sono string, number, date, buffer (o binary) e le codifiche hex, base64, ascii e utf8.
Un bind rifiutato (utente o password sbagliati) è un errore: il testo del server, oppure LDAP bind error 49 quando il server non invia alcun testo. L'errore è una stringa:
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
}