1 | var ldap = require('ldapjs');
|
2 |
|
3 | authorize = function(req, res, next) {
|
4 | return next();
|
5 | };
|
6 |
|
7 | var SUFFIX = 'ou=passport-ldapauth';
|
8 | var server = null;
|
9 |
|
10 | db = {
|
11 | 'valid': {
|
12 | dn: 'cn=valid, ou=passport-ldapauth',
|
13 | attributes: {
|
14 | uid: 'valid',
|
15 | name: 'Valid User'
|
16 | }
|
17 | }
|
18 | };
|
19 |
|
20 | exports.start = function(port, cb) {
|
21 | if (server) {
|
22 | if (typeof cb === 'function') return cb();
|
23 | return;
|
24 | }
|
25 |
|
26 | server = ldap.createServer();
|
27 |
|
28 | server.bind('cn=root', function(req, res, next) {
|
29 | if (req.dn.toString() !== 'cn=root' || req.credentials !== 'secret') {
|
30 | return next(new ldap.InvalidCredentialsError());
|
31 | }
|
32 | res.end();
|
33 | return next();
|
34 | });
|
35 |
|
36 | server.bind(SUFFIX, authorize, function(req, res, next) {
|
37 | var dn = req.dn.toString();
|
38 | if (dn !== 'cn=valid, ou=passport-ldapauth' || req.credentials !== 'valid') {
|
39 | return next(new ldap.InvalidCredentialsError());
|
40 | }
|
41 | res.end();
|
42 | return next();
|
43 | });
|
44 |
|
45 | server.search(SUFFIX, authorize, function(req, res, next) {
|
46 | if (req.filter.value == 'valid') {
|
47 | res.send(db['valid']);
|
48 | }
|
49 | res.end();
|
50 | return next();
|
51 | });
|
52 |
|
53 | server.listen(port, function() {
|
54 | if (typeof cb === 'function') return cb();
|
55 | });
|
56 | };
|
57 |
|
58 | exports.close = function(cb) {
|
59 | if (server) server.close();
|
60 | server = null;
|
61 | if (typeof cb === 'function') return cb();
|
62 | return;
|
63 | };
|
64 |
|
65 | if (!module.parent) {
|
66 | exports.start(1389);
|
67 | }
|