Skip to content

Commit

Permalink
Merge pull request #178 from larrabee/master
Browse files Browse the repository at this point in the history
[bugfix] Fix LDAP issue with OpenLDAP/MS AD
  • Loading branch information
andris9 authored Mar 15, 2017
2 parents 5332c81 + b6497b0 commit d2b0a61
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 2 additions & 0 deletions config/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ host="localhost"
port=3002
baseDN="ou=users,dc=company"
filter="(|(username={{username}})(mail={{username}}))"
#Username field in LDAP (uid/cn/username)
uidTag="username"
passwordresetlink=""

[postfixbounce]
Expand Down
15 changes: 9 additions & 6 deletions lib/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ let LdapStrategy;
try {
LdapStrategy = require('passport-ldapjs').Strategy; // eslint-disable-line global-require
} catch (E) {
// ignore
if (config.ldap.enabled) {
log.info('LDAP', 'Module "passport-ldapjs" not installed. LDAP auth will fail.');
}
}

module.exports.csrfProtection = csrf({
Expand Down Expand Up @@ -80,27 +82,28 @@ if (config.ldap.enabled && LdapStrategy) {
base: config.ldap.baseDN,
search: {
filter: config.ldap.filter,
attributes: ['username', 'mail'],
attributes: [config.ldap.uidTag, 'mail'],
scope: 'sub'
}
},
uidTag: config.ldap.uidTag
};

passport.use(new LdapStrategy(opts, (profile, done) => {
users.findByUsername(profile.username, (err, user) => {
users.findByUsername(profile[config.ldap.uidTag], (err, user) => {
if (err) {
return done(err);
}

if (!user) {
// password is empty for ldap
users.add(profile.username, '', profile.mail, (err, id) => {
users.add(profile[config.ldap.uidTag], '', profile.mail, (err, id) => {
if (err) {
return done(err);
}

return done(null, {
id,
username: profile.username
username: profile[config.ldap.uidTag]
});
});
} else {
Expand Down

0 comments on commit d2b0a61

Please sign in to comment.