|
| 1 | +# @simulacrum/ldap-simulator |
| 2 | + |
| 3 | +Simulate an actual LDAP server for testing and development. |
| 4 | + |
| 5 | +Often you are working on software that depends on the presence of an |
| 6 | +LDAP directory. This let's you create an LDAP server in a known state |
| 7 | +that can be used for offline development and testing. |
| 8 | + |
| 9 | +There are two different ways to start an LDAP simulator, but they both involve |
| 10 | +the same set of options. If you are running in a vanilla JavaScript |
| 11 | +environment, you can use promise-based API. |
| 12 | + |
| 13 | +## Plain JavaScript |
| 14 | + |
| 15 | +```js |
| 16 | +import { runLDAPServer } from "@simulacrum/ldap-simulator"; |
| 17 | + |
| 18 | +async function run() { |
| 19 | + let server = await runLDAPServer({ |
| 20 | + port: 3890, |
| 21 | + baseDN: "ou=users,dc=org.com", |
| 22 | + bindDn: "admin@org.com", |
| 23 | + bindPassword: "password", |
| 24 | + groupDN:"ou=groups,dc=org.com", |
| 25 | + users: [{ |
| 26 | + //required |
| 27 | + cn: 'Charles Lowell', |
| 28 | + //optional to bind using this user |
| 29 | + password: "super-secret-but-not-really", |
| 30 | + //optional: |
| 31 | + uid: 'cowboyd', |
| 32 | + |
| 33 | + }] |
| 34 | + }); |
| 35 | + console.log(`LDAP server running on ${server.port}`); |
| 36 | + try { |
| 37 | + //.... do some stuff; |
| 38 | + } finally { |
| 39 | + // don't forget to release the server resources! |
| 40 | + await server.close(); |
| 41 | + } |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +## Effection |
| 46 | + |
| 47 | +However, if you are already using [Effection][effection], the LDAP |
| 48 | +server is available as a [Resource][resource], and so you can use it |
| 49 | +freely in any context: |
| 50 | + |
| 51 | +```js |
| 52 | +import { createLDAPServer } from "@simulacrum/ldap-simulator"; |
| 53 | + |
| 54 | +function* run() { |
| 55 | + let server = yield runLDAPServer({ |
| 56 | + port: 3890, |
| 57 | + baseDN: "ou=users,dc=org.com", |
| 58 | + bindDn: "admin@org.com", |
| 59 | + bindPassword: "password", |
| 60 | + groupDN:"ou=groups,dc=org.com", |
| 61 | + users: [{ |
| 62 | + //required |
| 63 | + cn: 'Charles Lowell', |
| 64 | + //optional to bind using this user |
| 65 | + password: "super-secret-but-not-really", |
| 66 | + //optional: |
| 67 | + uid: 'cowboyd', |
| 68 | + |
| 69 | + }] |
| 70 | + }); |
| 71 | + |
| 72 | + //... do some stuff |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +[effection]: https://frontside.com/effection |
| 77 | +[resource]: https://frontside.com/effection/docs/guides/resources |
0 commit comments