Search code examples
node.jsldapopenldapldapjs

How to get profile of user I have binded as using ldapjs


I'm using ldapjs package. I'm using this code, which allows me bind using readonly credentials to ldap server and extract one of users profile from ou=people.

'use strict';

// Figure 1

const ldap = require('ldapjs');

const ldapClient = ldap.createClient({
  url: 'ldap://127.0.0.1:389'
});

const username = 'cn=readonly,dc=vodolaz095,dc=life';
const password = 'readonly';

ldapClient.bind(
  username,
  password,
  function (error) {
    if (error) {
      throw error;
    }
    console.log('bind performed');

    ldapClient.search('ou=people,dc=vodolaz095,dc=life', {
      filter: `(uid=vodolaz095)`,
      scope: 'one',
      attributes: ['uid', 'dn', 'cn', 'mail']
    }, function (error, res) {
      if (error) {
        throw error;
      }
      res.on('searchEntry', function (data) {
        // console.log('Data found', data);
        console.log('Data object', JSON.stringify(data.object, null, 2));
      });
      res.once('error', function(error){
        throw error;
      });
      res.once('end', function () {
        console.log('Completed');
        process.exit(0)
      });
    });
  }
);

Now, i change username and password to limited user's ones, i have extracted via readonly credentials and execute same code:


// same code as in figure 1

const username = 'uid=vodolaz095,ou=people,dc=vodolaz095,dc=life';
const password = 'thisIsNotAPassword123';

// same code as in figure 1

I can bind to ldap server, its ok. But when i try to get myself profile, it returns me NoSuchObjectError: No Such Object error

So, question is: how in openldap can i get profile of user i have binded as ? Like, how can i make whoami command?


Solution

  • You can get the entry of the user you binded as by setting the base search with your bindDN, and set the scope to base (without any filter).

    So if username is the bindDN, this should work :

    ldapClient.search(username, {
      scope: 'base',
      attributes: ['uid', 'dn', 'cn', 'mail']
    }