Node and LDAP communication

Edison Devadoss
3 min readMar 6, 2021

Hi friends, In this article, I will explain Node.js and LDAP communication.

https://www.photopea.com/

Before delving into Node and LDAP communication we should know about LDAP.

What is LDAP?

LDAP stands for Lightweight Directory Access Protocol. As the name suggests, it is a lightweight client-server protocol for accessing directory services, specifically X.500-based directory services. LDAP runs over TCP/IP or other connection-oriented transfer services.

Why LDAP?

LDAP provides communication language using that application can communicate with other directory services server. Directory services story users, password and computer accounts and share that information with other entities on the network.

Example of Node.js and LDAP

Initial setup for a node express project

https://carbon.now.sh/

Create index.js

https://carbon.now.sh/

LDAP connection

Create a file for LDAP connection

$ mkdir lib
$ cd lib
$ touch ldap-communication.js
https://carbon.now.sh/

Call LDAP connection when App starts. If the connection is failed then App will be crashed.

ldapClinet.on(‘connnectError’) is called when it throws an error in the LDAP connection.

ldapClinet.on(‘connect’) is called once it connected. We throw an error if LDAP is already connected.

reconnnect: true it is for when the connection is closed it enables to connect again once connection comes.

LDAP bind:

After LDAP connected successfully we need to bind the client with username, password.

https://carbon.now.sh/

After the bind only we can get users and perform other operations.

Search users

https://carbon.now.sh/

The above code is for search user. It gives a response through the listener. So we are using Promise for get all value at once. ‘end’ is called finally. on ‘end’ the block, we resolve all entries.

Update password:

We change LDAP user password using ldap.Change({}) method.

https://carbon.now.sh/

To change the password we need to pass userDn, newPassword. And set operation to replace. Using this method we can replace it with a new password. Not necessary to give the correct old password.

--

--

Edison Devadoss

Software developer / JavaScript / React / React Native / Firebase / Node.js / C Programming / Book Reader