{"id":15351,"library":"ldap-server-mock","title":"LDAP Server Mock","description":"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.","status":"active","version":"6.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/veo-labs/ldap-server-mock","tags":["javascript","ldap","mock","sso","veo-labs","typescript"],"install":[{"cmd":"npm install ldap-server-mock","lang":"bash","label":"npm"},{"cmd":"yarn add ldap-server-mock","lang":"bash","label":"yarn"},{"cmd":"pnpm add ldap-server-mock","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core LDAP client/server library that ldap-server-mock is built upon.","package":"ldapjs","optional":false}],"imports":[{"note":"The library primarily targets ESM environments and ships TypeScript types. Use named imports for the main class.","wrong":"const { LdapServerMock } = require('ldap-server-mock');","symbol":"LdapServerMock","correct":"import { LdapServerMock } from 'ldap-server-mock';"},{"note":"Import the `LdapUser` type for type-checking when defining your mock user arrays.","symbol":"LdapUser","correct":"import { LdapUser } from 'ldap-server-mock';"},{"note":"Import the `LdapServerMockConfiguration` type for configuring the mock server via the API.","symbol":"LdapServerMockConfiguration","correct":"import { LdapServerMockConfiguration } from 'ldap-server-mock';"}],"quickstart":{"code":"import * as fs from 'node:fs/promises';\nimport { LdapServerMock } from 'ldap-server-mock';\n\nasync function main() {\n  // Define your mock LDAP users\n  const ldapUsers = [\n    {\n      dn: 'cn=testuser,dc=example,dc=com',\n      attributes: {\n        objectClass: 'person',\n        cn: 'testuser',\n        uid: 'testuser',\n        mail: 'testuser@example.com'\n      }\n    },\n    {\n      dn: 'cn=anotheruser,dc=example,dc=com',\n      attributes: {\n        objectClass: 'person',\n        cn: 'anotheruser',\n        uid: 'anotheruser',\n        mail: 'anotheruser@example.com'\n      }\n    }\n  ];\n\n  // Configure the LDAP server mock\n  const serverConfiguration = {\n    port: 3004,\n    searchBase: 'dc=example,dc=com'\n  };\n\n  // For a runnable example, we'll use dummy certificate buffers.\n  // In a real scenario, you'd load these from files as shown in the README.\n  const certificatePublicKey = Buffer.from('-----BEGIN CERTIFICATE-----\\n... (your cert content) ...\\n-----END CERTIFICATE-----\\n');\n  const certificatePrivateKey = Buffer.from('-----BEGIN PRIVATE KEY-----\\n... (your key content) ...\\n-----END PRIVATE KEY-----\\n');\n\n  // Create an instance of the mock server\n  const ldapServer = new LdapServerMock(\n    ldapUsers,\n    serverConfiguration,\n    certificatePublicKey,\n    certificatePrivateKey\n  );\n\n  try {\n    console.log('Starting LDAP mock server...');\n    await ldapServer.start();\n    console.log(`LDAP mock server started on port ${serverConfiguration.port}`);\n\n    // Simulate some work or keep the server running for tests\n    // For a quick example, we'll stop it after a short delay\n    await new Promise(resolve => setTimeout(resolve, 2000));\n\n    console.log('Stopping LDAP mock server...');\n    await ldapServer.stop();\n    console.log('LDAP mock server stopped.');\n  } catch (error) {\n    console.error('Failed to run LDAP mock server:', error);\n  }\n}\n\nmain();","lang":"typescript","description":"This quickstart demonstrates how to programmatically start and stop an `LdapServerMock` instance, configure its users and server settings, and optionally enable TLS with dummy certificates. It defines two mock users and sets up a server on port 3004, then starts and stops it after a short delay."},"warnings":[{"fix":"Ensure all command-line options containing file paths are correctly quoted, like `npx ldap-server-mock --conf=\"/path/to/conf.json\" --database=\"/path/to/users.json\"`.","message":"The command-line arguments `--conf` and `--database` now require values to be quoted (e.g., `--conf=\"filePath\"` instead of `--conf filePath`).","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"Update your `users.json` or `LdapUser[]` array to move all non-DN attributes into an `attributes` object. For example, `{ dn: 'cn=user', cn: 'value' }` becomes `{ dn: 'cn=user', attributes: { cn: 'value' } }`.","message":"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.","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"Review your `searchFilter` configurations and ensure that the attributes of your mock users in the database (`users.json` or `LdapUser[]`) accurately reflect all criteria specified in your search queries.","message":"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.","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Upgrade your Node.js installation to version 16.3.0 or higher, and ensure NPM is at version 7.15.1 or higher.","message":"Dropped support for older Node.js and NPM versions. The package now requires Node.js >=16.3.0 and NPM >=7.15.1.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"When using relative paths with `--conf` or `--database`, ensure they are correct with respect to the directory from which you are running the `npx ldap-server-mock` command.","message":"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.","severity":"breaking","affected_versions":">=3.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Use quoted values for file paths: `npx ldap-server-mock --conf=\"./conf.json\" --database=\"./users.json\"`.","cause":"Incorrect command-line argument format for `--conf` or `--database` after v6.0.0.","error":"Error: Unknown option '--conf' or 'filePath' is not a valid value for '--conf'"},{"fix":"Verify 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.","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.","error":"LDAP search operation returns no results, but I expect a user to be found."},{"fix":"Ensure every user object in your `users.json` file or `LdapUser[]` array has a `dn` property with a valid LDAP distinguished name string.","cause":"An `LdapUser` object in your configuration is missing the mandatory `dn` (Distinguished Name) property.","error":"Error: The 'dn' attribute is missing from an LDAP user object."}],"ecosystem":"npm"}