Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
updating LDAP integration to sync the LDAP user data to the Konga use…
Browse files Browse the repository at this point in the history
…r each time they login. added some documentation on LDAP configuration
  • Loading branch information
ammmze committed Jul 13, 2018
1 parent e11ce37 commit d5f571f
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 32 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ login: demo | password: demodemodemo

This user data is populated to the database if there is not already any user data in it. [It is possible to alter the default user seed data.](DEFAULTUSERSEEDDATA.md)

You may also configure konga to authenticate via [LDAP](./docs/LDAP.md).

## Upgrading
In some cases a newer version of Konga may introduce new db tables, collections or changes in schemas.
The only thing you need to do is to start Konga in dev mode once so that the migrations will be applied.
Expand Down
60 changes: 38 additions & 22 deletions api/services/protocols/ldap.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,51 @@
var _ = require('lodash');
var adminGroup = new RegExp(process.env.ADMIN_GROUP_REG || null);
var commonName = /^cn=([^,]+),.*/
var adminGroup = new RegExp(process.env.KONGA_ADMIN_GROUP_REG || "^(admin|konga)$");
var ldapAttrMap = {
username: process.env.KONGA_LDAP_ATTR_USERNAME || 'uid',
firstName: process.env.KONGA_LDAP_ATTR_FIRSTNAME || 'givenName',
lastName: process.env.KONGA_LDAP_ATTR_LASTNAME || 'sn',
email: process.env.KONGA_LDAP_ATTR_EMAIL || 'mail'
};
var commonName = /^cn=([^,]+),.*/;

var ldapToUser = function (ldapUser, next) {
var data = {
active: true
var ldapToUser = function (ldapUser, user, next) {
var data = _.clone(user || {});
data.active = true;

// copy attributes from the ldap user to the konga user using the ldapAttrMap
for (var userAttr in ldapAttrMap) {
if (ldapAttrMap.hasOwnProperty(userAttr)) {
data[userAttr] = ldapUser[ldapAttrMap[userAttr]];
}
}
data.username = ldapUser.uid;
data.firstName = ldapUser.givenName;
data.lastName = ldapUser.sn;
data.email = ldapUser.mail;

sails.models.user.create(data)
.exec(function (err, user) {
if (data && data.id) {
sails.models.user.update({id: data.id}, data).exec(function(err) {
if (err) {
console.error("Failed to update user from ldap", err);
next(err);
} else {
setAdminStatus(ldapUser, data, next);
}
});
} else {
sails.models.user.create(data).exec(function (err, user) {
if (err) {
console.error("Failed to create user from ldap", err);
next(err);
} else {
setAdminStatus(ldapUser, user, next);
}
});
}
}

var group_test = function (group) {
return group.cn === 'admin' || adminGroup.test(group.cn);
return adminGroup.test(group.cn);
}

var member_test = function (group) {
return group.startsWith('cn=admin') ||
adminGroup.test(commonName.replace(group, "$1"));
return adminGroup.test(commonName.replace(group, "$1"));
}

var setAdminStatus = function (ldapUser, user, next) {
Expand All @@ -49,7 +67,8 @@ var setAdminStatus = function (ldapUser, user, next) {
*/
exports.getResolver = function getResolver(next) {
return function resolveUser(err, result, message) {
if (result === false) {
if (result === false || typeof result === 'undefined') {
console.error('failed to resolve user', err, message);
var error = message;
next(error);
} else {
Expand All @@ -62,16 +81,13 @@ exports.getResolver = function getResolver(next) {
.exec(function onExec(error, user) {
if (error) {
// Dunno, something bad happened
console.error('failed to look up existing user', error);
next(error);
} else if (!user) {
// We've not seen this user yet, so let's create a profile
ldapToUser(ldapUser, next);
} else {
// We trust LDAP explicitly, so we'll check the groups the user
// is a part of evey time they login
setAdminStatus(ldapUser, user, next);
// sync the ldap user to konga user
ldapToUser(ldapUser, user, next);
}
})
}
};
}
}
28 changes: 18 additions & 10 deletions config/ldap.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
var _ = require('lodash');

var groupFilter = process.env.KONGA_LDAP_GROUP_SEARCH_FILTER || '(|(memberUid={{uid}})(memberUid={{uidNumber}})(sAMAccountName={{uid}}))';
var groupFilterTemplate = _.template(groupFilter, {
// use {{...}} syntax for template variables
interpolate: /{{([\s\S]+?)}}/g
});
module.exports = {
server: {
url: process.env.LDAP_HOST || 'ldap://localhost:389',
bindDN: process.env.LDAP_BIND_USER,
bindCredentials: process.env.LDAP_BIND_PASSWORD,
searchAttributes: ['uid', 'givenName', 'sn', 'mail'],
searchBase: process.env.LDAP_SEARCH || "cn=users,dc=com",
searchFilter: '(|(uid={{username}})(sAMAccountName={{username}}))',
groupSearchAttributes: ['cn'],
groupSearchBase: process.env.LDAP_GROUP_SEARCH || 'cn=groups,cn=accounts,dc=com',
groupSearchFilter: '(member={{dn}})'
url: process.env.KONGA_LDAP_HOST || 'ldap://localhost:389',
bindDN: process.env.KONGA_LDAP_BIND_DN,
bindCredentials: process.env.KONGA_LDAP_BIND_PASSWORD,
searchAttributes: (process.env.KONGA_LDAP_USER_ATTRS || 'uid,uidNumber,givenName,sn,mail').split(','),
searchBase: process.env.KONGA_LDAP_USER_SEARCH_BASE || "ou=users,dc=com",
searchFilter: process.env.KONGA_LDAP_USER_SEARCH_FILTER || '(|(uid={{username}})(sAMAccountName={{username}}))',
groupSearchAttributes: (process.env.KONGA_LDAP_GROUP_ATTRS || 'cn').split(','),
groupSearchBase: process.env.KONGA_LDAP_GROUP_SEARCH_BASE || 'ou=groups,dc=com',
groupSearchFilter: function (user) {
return groupFilterTemplate(user);
}
},
usernameField: 'identifier',
passwordField: 'password'
};
};
24 changes: 24 additions & 0 deletions docs/LDAP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# LDAP

With the LDAP integration, you can authenticate via your LDAP server. Currently the application does need the user to be in the konga user database for the user profile page to display properly, so we currently will sync any LDAP authenticated user into the konga user database upon each login. In the future, perhaps the user profile page will be read-only for LDAP authentication? Or maybe it can sync the data back up to the LDAP server?

## Configuration

| Environment Variable | Default | Description |
| --- | --- | --- |
| `KONGA_AUTH_PROVIDER` | `local` | **Set this to `ldap` to switch auth provider to LDAP** |
| `KONGA_LDAP_HOST` | `ldap://localhost:389` | The location of the LDAP server |
| `KONGA_LDAP_BIND_DN` | *no default* | The DN that the konga should use to login to LDAP to search users |
| `KONGA_LDAP_BIND_PASSWORD` | *no default* | The password for the user konga will use to search for users |
| `KONGA_LDAP_USER_SEARCH_BASE` | `ou=users,dc=com` | The base DN used to search for users |
| `KONGA_LDAP_USER_SEARCH_FILTER` | `(|(uid={{username}})(sAMAccountName={{username}}))` | The filter expression used to search for users. Use `{{username}}` where you expect the username to be. |
| `KONGA_LDAP_USER_ATTRS` | `uid,uidNumber,givenName,sn,mail` | Comma separated list of attributes to pull from the LDAP server for users |
| `KONGA_LDAP_GROUP_SEARCH_BASE` | `ou=groups,dc=com` | The base DN used to search for groups |
| `KONGA_LDAP_GROUP_SEARCH_FILTER` | `(|(memberUid={{uid}})(memberUid={{uidNumber}})(sAMAccountName={{uid}}))` | The filter expression used to search for groups. Use `{{some-attr}}` where you expect a user attribute to be or `{{dn}}` for the user `dn`. |
| `KONGA_LDAP_GROUP_ATTRS` | `cn` | Comma separated list of attributes to pull from the LDAP server for groups |
| `KONGA_ADMIN_GROUP_REG` | `^(admin|konga)$` | Regular expression used to determine if a group should be considered as an admin user |
| `KONGA_LDAP_ATTR_USERNAME` | `uid` | LDAP attribute name that should be used as the konga username |
| `KONGA_LDAP_ATTR_FIRSTNAME` | `givenName` | LDAP attribute name that should be used as the konga user's first name |
| `KONGA_LDAP_ATTR_LASTNAME` | `sn` | LDAP attribute name that should be used as the konga user's last name |
| `KONGA_LDAP_ATTR_EMAIL` | `mail` | LDAP attribute name that should be used as the konga user's email address |

0 comments on commit d5f571f

Please sign in to comment.