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

ldap-server-mock

v6.0.1

Published

Simple mock for LDAP server

Downloads

27,188

Readme

LDAP simple server mock

Really simple basic mock for LDAP server based on ldaps. Use it to mock an LDAP server and authenticate a user without further verifications, it simply searches for the user in the database and returns it. It does not implement LDAP SASL authentication. This should not be used in production environment, it is just for test purpose, nothing more.

Install

npm install ldap-server-mock

Usage

Using the API

ldap-server-mock exposes the LdapServerMock class:

constructor(users: LdapUser[], serverConfiguration: LdapServerMockConfiguration, certificatePublicKey?: Buffer, certificatePrivateKey?: Buffer, logger?: LdapServerMockLogger)

| Argument | Required | Default | Details | | --------------------- | -------- | ------------------------------------ | --------------------------------------------------------------------------------- | | users | yes | - | The list of LDAP users as JavaScript objects (see below) | | serverConfiguration | yes | { port: 3004, searchBase: 'dc=test'} | The LDAP server mock configuration (see below) | | certificatePublicKey | no | - | The public key of the certificate to use for creating an LDAP server over TLS | | certificatePrivateKey | no | - | The private key corresponding to the public key defined by certificatePublicKey | | logger | no | console | A custom logger to use instead of console |

start(): Promise<void>

stop(): Promise<void>

Example

{
import * as fs from 'node:fs/promises';
import { LdapServerMock } from 'ldap-server-mock';

async function main() {

  const ldapUsers = [
    {
      dn: 'cn=user,dc=test',
      attributes: {
        objectClass: 'person',
        cn: 'user-login',
        attribute1: 'value1',
        attribute2: 'value2'
      }
    }
  ];

  const serverConfiguration = {
    port: 3004,
    searchBase: 'dc=test'
  };

  const customLogger = {
    info: (...args) => {
      console.info(...args);
    }
  }

  const certificatePublicKey = await fs.readFile('/path/to/certificate/public/key.pem');
  const certificatePrivateKey = await fs.readFile('/path/to/certificate/private/key.pem');

  const ldapServer = new LdapServerMock(ldapUsers, serverConfiguration, certificatePublicKey, certificatePrivateKey, customLogger);
  await ldapServer.start();
  await ldapServer.stop();
}

main();

Using command line

LDAP server mock can be started with command:

npx ldap-server-mock --conf=/tmp/ldap-server-mock-conf.json --database=/tmp/users.json

With:

  • --conf The path to a JSON file containing server's configuration (see below)
  • --database The path to a JSON file containing the database of users (see below)

Nb: If process is launched as a sub process it will send a message to its parent process when started:

{
  status: 'started';
}

Example

/tmp/ldap-server-mock-conf.json

The server's configuration file must be a simple JSON file:

{
  "certPath": "/path/to/certificate/public/key.pem",
  "certKeyPath": "/path/to/certificate/private/key.pem",
  "port": 3004,
  "searchBase": "dc=test"
}
/tmp/users.json

The database's configuration file must be a simple JSON file containing an array of users:

[
  {
    dn: 'cn=user,dc=test',
    attributes: {
      objectClass: 'person',
      cn: 'user-login',
      attribute1: 'value1',
      attribute2: 'value2'
    }
  }
];
npx ldap-server-mock --conf=/tmp/ldap-server-mock-conf.json --database=/tmp/users.json

Server configuration

| Property | Type | Required | Default | Details | | ----------- | ------ | -------- | --------- | ----------------------------------------------------------------------------------------------- | | certPath | string | no | - | The path of the certificate's public key to use for creating an LDAP server over TLS | | certKeyPath | string | no | - | The path of the certificate's private key corresponding to the public key defined by certPath | | port | number | no | 3004 | The port the LDAP server will listen to | | searchBase | string | no | "dc=test" | The search base to use when searching for the user who is trying to connect |

{
  certPath: '/path/to/certificate/public/key.pem',
  certKeyPath: '/path/to/certificate/private/key.pem',
  port: 3004,
  searchBase: 'dc=test'
}

LDAP User

An LDAP user must have a valid Dinstinguished Name and any number of other attributes:

| Property | Type | Required | Default | Details | | ---------- | ------ | -------- | ------- | ----------------------------------- | | dn | string | yes | - | Dinstinguish Name | | attributes | Object | yes | - | Any key / value pairs of attributes |

{
  dn: 'cn=user,dc=test",
  attributes: {
    objectClass: 'person',
    cn: 'user-login',
    attribute1: 'value1',
    attribute2: 'value2'
}

Test a connection to the LDAP server

Here is an example using the ldapsearch client from OpenLDAP with the configuration used in examples:

ldapsearch -x -H ldaps://127.0.0.1:3004 -b "dc=test" "(&(objectclass=person)(cn=user-login))" attribute1 attribute2

With:

  • -x to use simple authentication without setting binding DN
  • -H ldaps://127.0.0.1:3004 the server URL
  • -b "dc=test" the search base in LDAP directory, it should be the same as the searchBase property in server's configuration above
  • "(&(objectclass=person)(cn=user-login))" the search filter
  • attribute1, attribute2 the list of attributes you want to be returned

Nb: Don't forget to change protocol to ldap if you haven't configured a certificate.

Known issues

STARTTLS

This mock supports running an LDAP server over TLS which is the non-standard LDAPS. However STARTTLS (the standard way to run an LDAP server over TLS) is not supported as the underlying ldapjs module has not support for it on the server side. See issue STARTTLS support for the Server API for more information.

Contributors

Maintainer: Veo-Labs

License

AGPL