2020-07-10 18:51:45 -07:00
|
|
|
var ldap = require('ldapjs');
|
2019-01-15 19:15:20 -08:00
|
|
|
var session = require('express-session');
|
|
|
|
var router = module.parent.router;
|
|
|
|
|
|
|
|
module.exports = function(plugin) {
|
|
|
|
plugin.defaults({
|
|
|
|
"session": {
|
|
|
|
secret: 'session-ldap',
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: true
|
|
|
|
},
|
|
|
|
// See https://github.com/joyent/node-ldapjs/blob/master/docs/client.md#create-a-client
|
|
|
|
"bind": {
|
|
|
|
dn: "cn=%u,ou=People,dc=domain",
|
|
|
|
password: "%u"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
function requestAuth(res) {
|
|
|
|
res.set('WWW-Authenticate', 'Basic realm="Auth"');
|
|
|
|
res.status(401).send('Auth required.');
|
|
|
|
}
|
|
|
|
|
|
|
|
router.all('*', session(plugin.conf.session));
|
|
|
|
router.all('*', function(req, res, next) {
|
|
|
|
if (req.session.isValid) {
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// parse login and password from headers
|
|
|
|
var b64auth = (req.headers.authorization || '').split(' ')[1] || '';
|
|
|
|
var strauth = new Buffer(b64auth, 'base64').toString();
|
|
|
|
var splitIndex = strauth.indexOf(':');
|
|
|
|
var login = strauth.substring(0, splitIndex);
|
|
|
|
var password = strauth.substring(splitIndex + 1);
|
|
|
|
|
|
|
|
if (!login || !password) {
|
|
|
|
requestAuth(res);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-10 19:05:06 -07:00
|
|
|
// It seems stupid to create a client each time, but joyent's ldapjs cannot bind then unbind to the same client multiple times...
|
|
|
|
var client = ldap.createClient(plugin.conf.options)
|
|
|
|
|
2020-07-10 18:51:45 -07:00
|
|
|
client.bind(plugin.conf.bind.dn.replace('%u', login), plugin.conf.bind.password.replace('%p', password), function(err) {
|
|
|
|
if (err) {
|
|
|
|
console.log('E: ldap.bind: ', err)
|
|
|
|
requestAuth(res);
|
|
|
|
} else {
|
|
|
|
req.session.isValid = true;
|
|
|
|
client.unbind(function(err) {
|
|
|
|
if (err) {
|
|
|
|
console.log('E: ldap.unbind: ', err)
|
|
|
|
}
|
|
|
|
})
|
2020-07-10 19:05:06 -07:00
|
|
|
next();
|
2020-07-10 18:51:45 -07:00
|
|
|
}
|
2019-01-15 19:15:20 -08:00
|
|
|
})
|
|
|
|
});
|
|
|
|
};
|