LDAP Server Mock
ldap-server-mock provides a streamlined, in-memory mock LDAP server designed for testing and development purposes. It is built upon the `ldapjs` library and simplifies the process of simulating LDAP authentication without the overhead of a full-fledged LDAP instance. The package's current stable version is 6.0.1, released in November 2022, with a release cadence that addresses breaking changes and feature enhancements (e.g., v6.0.0 and v6.0.1 in November 2022, v5.0.0 in February 2022). It specifically focuses on basic user search and retrieval for authentication, not full SASL authentication or complex LDAP operations. Key differentiators include its ease of setup via API or command-line using simple JSON configuration files for users and server settings, making it an ideal choice for unit and integration testing where minimal LDAP functionality is required.
Common errors
-
Error: Unknown option '--conf' or 'filePath' is not a valid value for '--conf'
cause Incorrect command-line argument format for `--conf` or `--database` after v6.0.0.fixUse quoted values for file paths: `npx ldap-server-mock --conf="./conf.json" --database="./users.json"`. -
LDAP search operation returns no results, but I expect a user to be found.
cause The search filter logic in v5.0.0 and later is more stringent, requiring all filter conditions to match, or user attributes are not correctly structured under the `attributes` property since v6.0.0.fixVerify that your mock user objects conform to the v6.0.0 structure (all non-DN attributes under an `attributes` object) and that their attributes satisfy all conditions in your LDAP search filter. -
Error: The 'dn' attribute is missing from an LDAP user object.
cause An `LdapUser` object in your configuration is missing the mandatory `dn` (Distinguished Name) property.fixEnsure every user object in your `users.json` file or `LdapUser[]` array has a `dn` property with a valid LDAP distinguished name string.
Warnings
- breaking The command-line arguments `--conf` and `--database` now require values to be quoted (e.g., `--conf="filePath"` instead of `--conf filePath`).
- breaking User configuration JSON structure has changed. All user attributes, except `dn`, must now be nested under an `attributes` property within each user object. This is due to the adoption of `ldapjs` filters.
- breaking The search filter logic has changed. Previously, only the `userLoginAttribute` was used to match a user, ignoring other aspects of the search query. Now, the server considers all aspects of the `searchFilter` property, which may require adjustments to user attributes in your database to ensure matches.
- breaking Dropped support for older Node.js and NPM versions. The package now requires Node.js >=16.3.0 and NPM >=7.15.1.
- breaking Relative paths for `--conf` and `--database` options are now resolved relative to the *current working directory* where the command is executed, rather than relative to the `ldap-server-mock` module's installation directory.
Install
-
npm install ldap-server-mock -
yarn add ldap-server-mock -
pnpm add ldap-server-mock
Imports
- LdapServerMock
const { LdapServerMock } = require('ldap-server-mock');import { LdapServerMock } from 'ldap-server-mock'; - LdapUser
import { LdapUser } from 'ldap-server-mock'; - LdapServerMockConfiguration
import { LdapServerMockConfiguration } from 'ldap-server-mock';
Quickstart
import * as fs from 'node:fs/promises';
import { LdapServerMock } from 'ldap-server-mock';
async function main() {
// Define your mock LDAP users
const ldapUsers = [
{
dn: 'cn=testuser,dc=example,dc=com',
attributes: {
objectClass: 'person',
cn: 'testuser',
uid: 'testuser',
mail: 'testuser@example.com'
}
},
{
dn: 'cn=anotheruser,dc=example,dc=com',
attributes: {
objectClass: 'person',
cn: 'anotheruser',
uid: 'anotheruser',
mail: 'anotheruser@example.com'
}
}
];
// Configure the LDAP server mock
const serverConfiguration = {
port: 3004,
searchBase: 'dc=example,dc=com'
};
// For a runnable example, we'll use dummy certificate buffers.
// In a real scenario, you'd load these from files as shown in the README.
const certificatePublicKey = Buffer.from('-----BEGIN CERTIFICATE-----\n... (your cert content) ...\n-----END CERTIFICATE-----\n');
const certificatePrivateKey = Buffer.from('-----BEGIN PRIVATE KEY-----\n... (your key content) ...\n-----END PRIVATE KEY-----\n');
// Create an instance of the mock server
const ldapServer = new LdapServerMock(
ldapUsers,
serverConfiguration,
certificatePublicKey,
certificatePrivateKey
);
try {
console.log('Starting LDAP mock server...');
await ldapServer.start();
console.log(`LDAP mock server started on port ${serverConfiguration.port}`);
// Simulate some work or keep the server running for tests
// For a quick example, we'll stop it after a short delay
await new Promise(resolve => setTimeout(resolve, 2000));
console.log('Stopping LDAP mock server...');
await ldapServer.stop();
console.log('LDAP mock server stopped.');
} catch (error) {
console.error('Failed to run LDAP mock server:', error);
}
}
main();