npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ldapcrud

v1.4.1

Published

A lightweight wrapper for ldapjs for CRUD actions and some more

Downloads

95

Readme

LDAPCRUD

A lightweight wrapper for ldapjs for CRUD actions and some more.

Install it via npm

npm install ldapcrud

Setup

First of all, install and require ldapcrud module in your script and create new instance of LDAPCRUD class with your config;

const LDAPCRUD = require('ldapcrud');

let config = {
  clientOptions: {
    url: 'ldaps://your-ldap-url',
    tlsOptions: {
      rejectUnauthorized: false
    }
  },
  baseDN: 'OU=Customers,DC=Company,DC=local',
  userDN: 'CN=serviceadmin,OU=Customers,DC=Company,DC=local',
  password: 'secret',
  attributes: [
    'sAMAccountName',
    'mail',
    'sn',
    'givenName'
  ],
  defaultFilter: '(mail=*@*)',
  suffix: '@Company.local',
  model: {
    'sAMAccountName': 'ldap',
    'mail': 'email',
    'sn': 'name.last',
    'givenName': 'name.first'
  }
};

let ldap = new LDAPCRUD(config);

Config

  • clientOptions object - options for ldapjs client creation. See more
  • baseDN string - DN where search users.
  • userDN string - Admin User DN, that can performs operations against the LDAP server.
  • password string - Admin User password.
  • attributes Array - Array of properties to select
  • defaultFilter string - LDAP Filter string
  • suffix string - User model suffix
  • model object - relation LDAP properties to your custom User model, where keys are LDAP properties and values are yours User model fields.

convertModel(data, [toLdapModel])

Convert LDAP User model to yours format or vice versa. model param of config is required. Also you can use flatten module, if you have nested user object

Example:

let user = flatten({
  name: {
    first: 'John',
    last: 'Doe'
  },
  email: '[email protected]'
});
let ldapModel = ldap.convertModel(user, true);

// ldapModel === {
//   sn: 'Doe',
//   givenName: 'John',
//   mail: '[email protected]'
// }

Params:

  • object data (JS object)
  • boolean [toLdapModel] (if true convert Node model to LDAP, else LDAP to Node)

Return:

  • object result model

createClient([dn], [password], callback)

Create LDAP client

Example:

ldap.createClient((err, client) => {
  // Handle error and do something
});

Params:

  • string [dn] (custom User DN for bind)
  • string [password] (custom password for bind)
  • function callback (callback(err, client))

authenticate(dn, password, callback)

LDAP Authentication

Example:

let dn = '(sAMAccountName=username)';
let pwd = 'secret';
ldap.authenticate(dn, pwd, (err, auth) => {
  if (err) return console.error(err);
  console.log('Authorize:', (auth) ? 'success' : 'failed');
});

Params:

  • string dn (User DN for bind)
  • string password (bind password)
  • function callback (callback(err, auth))

Return:

  • interrupt executing on error

create(entry, callback)

Create entry in LDAP by provided entry properties.

  • displayName, cn, name properties generetes from sn and givenName.
  • dn / distinguishedName generetes by cn, provided dn property and baseDN property of config
  • userPrincipalName concatenates from provided sAMAccountName property and suffix property of config

Example:

let entry = {
  sn: 'User',
  givenName: 'Test',
  sAMAccountName: 'testUser',
  mail: '[email protected]',
};
ldap.create(entry, (err) => {
  // Handle error and do something
});

Params:

  • object entry (user data)
  • function callback (callback)

Return:

  • execute callback with error

read([options], callback)

Read entries in LDAP. findUsers is alias for read

Example:

ldap.read({
  filter: '(sAMAccountName=username)'
}, (err, users) => {
  // Handle error and do something
});

Params:

  • object [options] (search options)
  • function callback (callback)

update(filter, changedAttrs, callback)

Update user

Example:

Change password in Active Directory

function encodePassword(password) {
  return new Buffer('"' + password + '"', 'utf16le').toString();
}

let pwd = 'secret';
let attrs = [
  {
    type: 'replace',
    attr: 'unicodePwd',
    value: encodePassword(pwd)
  },
  {
    type: 'replace',
    attr: 'userAccountControl',
    value: '66048'
  }
];

ldap.update('(sAMAccountName=username)', attrs, (err) => {
  // Handle error and do something
});

Params:

  • string filter (LDAP search filter)
  • Array changedAttrs (array of objects attributes to change)
  • function callback (callback(err))

Return:

  • execute callback with error

delete(filter, callback)

Delete user

Example:

ldap.delete('(sAMAccountName=username)', (err) => {
  // Handle error and do something
});

Params:

  • string filter (LDAP search filter)
  • function callback (callback(err))

Return:

  • execute callback with error

move(filter, newDN, callback)

Move user to other DN. Work in progress! Not tested!

Params:

  • string filter (LDAP search filter)
  • string newDN (new DN for user without cn)
  • function callback (callback(err))

Return:

  • execute callback with error