1 | var express = require('express'),
|
2 | passport = require('passport'),
|
3 | LdapStrategy = require('passport-ldapauth').Strategy,
|
4 | bodyParser = require('body-parser');
|
5 |
|
6 | var server = null;
|
7 |
|
8 | var init_passport = function(opts, testopts) {
|
9 | if (testopts.no_callback === true) {
|
10 | passport.use(new LdapStrategy(opts));
|
11 | } else {
|
12 | passport.use(new LdapStrategy(opts, function(user, cb) {
|
13 | return cb(null, user);
|
14 | }));
|
15 | }
|
16 | };
|
17 |
|
18 | passport.serializeUser(function(user, cb) {
|
19 | cb(null, user.dn);
|
20 | });
|
21 |
|
22 | passport.deserializeUser(function(dn, cb) {
|
23 | cb(null, {dn: dn});
|
24 | });
|
25 |
|
26 | exports.start = function(opts, testopts, cb) {
|
27 |
|
28 | var app = express();
|
29 |
|
30 | init_passport(opts, testopts);
|
31 |
|
32 | app.use(bodyParser.json());
|
33 | app.use(passport.initialize());
|
34 |
|
35 | app.post('/login', passport.authenticate('ldapauth', {session: false}), function(req, res) {
|
36 | res.send({status: 'ok'});
|
37 | });
|
38 |
|
39 | app.post('/custom-cb-login', function(req, res, next) {
|
40 | passport.authenticate('ldapauth', function(err, user, info) {
|
41 | if (err) return next(err);
|
42 | if (!user) return res.status(401).send(info);
|
43 | req.logIn(user, function(err) {
|
44 | if (err) return next(err);
|
45 | return res.json(user);
|
46 | })
|
47 | })(req, res, next);
|
48 | });
|
49 |
|
50 | if (typeof cb === 'function') return cb(app);
|
51 | return;
|
52 | };
|
53 |
|
54 | exports.close = function(cb) {
|
55 | if (server) server.close();
|
56 | server = null;
|
57 | if (typeof cb === 'function') return cb();
|
58 | return;
|
59 | };
|