Node.js LDAP Authentication Fork

6.1.0 · active · verified Sun Apr 19

ldapauth-fork is a Node.js library for authenticating users against an LDAP server. It's a maintained fork of the original `node-ldapauth` package, primarily created to integrate newer versions of `ldapjs`, enable `tlsOptions` support, and address various community-reported issues. The package provides a robust API for user authentication, including support for group membership checks and configurable search filters. It ships with TypeScript type definitions since v4.0.0 and utilizes Bunyan for logging, aligning with `ldapjs`'s logging approach. The current stable version is 6.1.0, with a release cadence that addresses bug fixes, dependency updates, and new features, indicating active maintenance. Key differentiators include its explicit support for modern `ldapjs` versions, comprehensive configuration options for diverse LDAP setups, and improved error handling through `EventEmitter` inheritance.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate `LdapAuth`, authenticate a user with a username and password, handle errors, and close the LDAP connection using modern async/await syntax. It utilizes environment variables for sensitive configuration options and shows basic error logging. A simple `console` logger is used, but a Bunyan instance is recommended for production.

import LdapAuth, { LdapAuthOptions } from 'ldapauth-fork';
import type { User } from 'ldapjs';

const options: LdapAuthOptions = {
  url: process.env.LDAP_URL ?? 'ldaps://localhost:636',
  bindDN: process.env.LDAP_BIND_DN ?? 'cn=admin,dc=example,dc=org',
  bindCredentials: process.env.LDAP_BIND_CREDENTIALS ?? 'adminsecret',
  searchBase: process.env.LDAP_SEARCH_BASE ?? 'ou=users,dc=example,dc=org',
  searchFilter: process.env.LDAP_SEARCH_FILTER ?? '(uid={{username}})',
  log: console // Simple logger, use a Bunyan instance in production
};

async function authenticateUser(username: string, password: string): Promise<User | null> {
  const auth = new LdapAuth(options);

  auth.on('error', (err) => {
    console.error(`LDAP Authentication Error: ${err.message}`);
  });

  try {
    console.log(`Attempting to authenticate user: ${username}`);
    const user = await new Promise<User | null>((resolve, reject) => {
      auth.authenticate(username, password, (err, user) => {
        if (err) {
          console.error(`Authentication failed for ${username}: ${err.message}`);
          return reject(err);
        }
        if (user) {
          console.log(`User ${username} authenticated successfully.`);
          resolve(user as User);
        } else {
          console.log(`Authentication failed: No user found for ${username}.`);
          resolve(null);
        }
      });
    });
    return user;
  } catch (error) {
    console.error('An unexpected error occurred during authentication:', error);
    return null;
  } finally {
    await new Promise<void>((resolve, reject) => {
      auth.close((err) => {
        if (err) return reject(err);
        resolve();
      });
    });
    console.log('LDAP connection closed.');
  }
}

// Example usage:
authenticateUser('testuser', 'testpassword')
  .then(user => {
    if (user) {
      console.log('Authenticated User Details:', user);
    }
  })
  .catch(console.error);

view raw JSON →