aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/auth.js
blob: c003d2aaaf50f2b4d581a53e5b2beaf39036250e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';

var passport = require('passport'),
    LdapStrategy = require('passport-ldapjs').Strategy;

passport.serializeUser(function (user, done) {
    console.log('serializeUser', user);
    done(null, user.uid);
});

passport.deserializeUser(function (id, done) {
    console.log('deserializeUser', id);
    done(null, { uid: id });
});

var LDAP_URL = process.env.LDAP_URL;
var LDAP_USERS_BASE_DN = process.env.LDAP_USERS_BASE_DN;

if (LDAP_URL && LDAP_USERS_BASE_DN) {
    console.log('Enable ldap auth');

    exports.ldap = passport.authenticate('ldap');
} else {
    exports.ldap = function (req, res, next) {
        console.log('Disable ldap auth, use developer credentials!');

        if (req.query.username !== 'username') return res.send(401);
        if (req.query.password !== 'password') return res.send(401);

        next();
    };
}

var opts = {
    server: {
        url: LDAP_URL,
    },
    base: LDAP_USERS_BASE_DN,
    search: {
        filter: '(uid={{username}})',
        attributes: ['displayname', 'username', 'mail', 'uid'],
        scope: 'sub'
    },
    uidTag: 'cn',
    usernameField: 'username',
    passwordField: 'password',
};

passport.use(new LdapStrategy(opts, function (profile, done) {
    console.log('ldap', profile);
    done(null, profile);
}));