0

I'm trying to write authentication code using ldapjs in node.js. It should simply accept username and password, and just return whether the user exists, and if the password is correct.

For test, I've used JXplorer to confirm that the DN for the user is:

uid=123456789,ou=people,ou=myrealm,dc=MYDCENV

And I've confirmed that on my test LDAP server, the password for that user is abcdefg12345

I was able to connect to the server using JXPlorer using these settings:

Host: myServer-TEST.myHost.com
Port:   7001
Protocol: LDAP v3
BaseDN: EMPTY


SecurityLevel: User + Password
UserDN:  uid=123456789,ou=people,ou=myrealm,dc=MYDCENV
password: abcdefg12345

Not sure if it is relevant, but this is a Weblogic server.

My code looks like this:

ldap.js:

const ldap = require('ldapjs');
const { promisify } = require('util');

// Configure the LDAP client
const ldapClient = ldap.createClient({
  url: 'ldap://myServer-TEST.myHost.com:7001',
  tlsOptions: {
    rejectUnauthorized: false,
  },  
});


ldapClient.on('error', (err) => {
    console.error('LDAP error:', err);
  });
  
  async function searchUser(username) {
    const searchOptions = {
      filter: `(uid=${username})`,
      scope: 'sub',
      attributes: ['*'],
    };
  
    const userDN = `uid=${username},ou=people,ou=myrealm,dc=MYDCENV`;
    
    console.log("START SEARCH");
    const { searchEntries } = await promisify(ldapClient.search.bind(ldapClient))(userDN, searchOptions);
    console.log("FINISH SEARCH");
  
    console.log("Search Entries: ", searchEntries);
  
    if (!searchEntries) {
      throw new Error(`User ${username} not found`);
    }
  
    if (searchEntries.length > 1) {
      throw new Error(`User ${username} not unique`);
    }
  
    return searchEntries[0];
  }
  
  export default async function authenticateUser(username, password) {
    console.log('START BINDING');
    await promisify(ldapClient.bind.bind(ldapClient))(`uid=${username},ou=people,ou=myrealm,dc=MYDCENV`, password);
    console.log('FINISHED BINDING');
  
    const user = await searchUser(username);
  
    console.log('User found:', user);
  
    console.log('START UNBINDING');
    ldapClient.unbind();
    console.log('FINISHED UNBINDING');
  }

To call the authentication script, my code looks like this:
    ldapAuthenticate(userIDInternal, password)
   .then(() => console.log('Authentication successful'))
   .catch((err) => console.error('Authentication failed:', err));

My output looks like this: START BINDING

No output after that. What can cause the bind script to never complete?

NL3294
  • 984
  • 1
  • 10
  • 27

0 Answers0