Comprehensive Guide to ldapjs API for Node.js

Introduction to ldapjs

ldapjs is a powerful Node.js module that provides an easy and efficient way to interact with LDAP (Lightweight Directory Access Protocol) servers. It is designed for high performance and flexibility while simplifying the complexities usually associated with LDAP operations.

Getting Started

First, you need to install ldapjs via npm:

npm install ldapjs

Connecting to an LDAP Server

To start, let’s create a connection to an LDAP server. The following code demonstrates how to connect to an LDAP server:


  const ldap = require('ldapjs');
  const client = ldap.createClient({
    url: 'ldap://localhost:389'
  });

  client.bind('cn=root', 'secret', (err) => {
    if (err) {
        console.error('Bind error:', err);
    } else {
        console.log('Successfully connected to LDAP');
    }
  });

Searching LDAP

Searching is a common operation when dealing with LDAP. Below is an example of how to search an LDAP directory:


  const opts = {
    filter: '(sn=Smith)',
    scope: 'sub',
    attributes: ['dn', 'sn', 'cn']
  };

  client.search('ou=users, o=mycompany', opts, (err, res) => {
    res.on('searchEntry', (entry) => {
      console.log('entry: ' + JSON.stringify(entry.object));
    });
    res.on('searchReference', (referral) => {
      console.log('referral: ' + referral.uris.join());
    });
    res.on('error', (err) => {
      console.error('error: ' + err.message);
    });
    res.on('end', (result) => {
      console.log('status: ' + result.status);
    });
  });

Adding an Entry

Adding entries to the LDAP directory is straightforward:


  const entry = {
    cn: 'John Doe',
    sn: 'Doe',
    email: 'johndoe@example.com',
    objectClass: 'person'
  };

  client.add('cn=John Doe, ou=users, o=mycompany', entry, (err) => {
    if (err) {
      console.error('Add entry error:', err);
    } else {
      console.log('Entry added successfully');
    }
  });

Deleting an Entry

Deleting an entry requires the DN (Distinguished Name) of the entry to be removed:


  client.del('cn=John Doe, ou=users, o=mycompany', (err) => {
    if (err) {
      console.error('Delete entry error:', err);
    } else {
      console.log('Entry deleted successfully');
    }
  });

Modifying an Entry

Modifications can be performed using the modify method. This can include updates to attributes:


  const change = new ldap.Change({
    operation: 'replace',
    modification: {
      email: 'john.doe@newdomain.com'
    }
  });

  client.modify('cn=John Doe, ou=users, o=mycompany', change, (err) => {
    if (err) {
      console.error('Modify entry error:', err);
    } else {
      console.log('Entry modified successfully');
    }
  });

Example App

Here is an example of a complete application using multiple APIs to demonstrate the functionality.


  const ldap = require('ldapjs');
  const client = ldap.createClient({
    url: 'ldap://localhost:389'
  });

  client.bind('cn=root', 'secret', (err) => {
    if (err) {
      console.error('Bind error:', err);
      return;
    }

    const searchOpts = {
      filter: '(sn=Smith)',
      scope: 'sub',
      attributes: ['dn', 'sn', 'cn']
    };

    client.search('ou=users, o=mycompany', searchOpts, (err, res) => {
      if (err) {
        console.error('Search error:', err);
        return;
      }
      
      res.on('searchEntry', (entry) => {
        const dn = entry.object.dn;
        const change = new ldap.Change({
          operation: 'replace',
          modification: {
            email: 'new.email@example.com'
          }
        });
        
        client.modify(dn, change, (err) => {
          if (err) {
            console.error('Modify entry error:', err);
          } else {
            console.log('Entry modified:', dn);
          }
        });
      });

      res.on('end', () => client.unbind());
    });
  });

With these code snippets and explanations, you should be able to efficiently interact with LDAP servers using ldapjs. It is a robust library that simplifies many of the tasks associated with LDAP.

Hash: deb26643ad5ff3d5db85af10f9aaf98bf53edea1462082627974cca90229331f

Leave a Reply

Your email address will not be published. Required fields are marked *