I've made the following changes to get mine to work. Commenting out or removing the check_credentials function at the end of the file should resolve the issue obtaining the user's firstname, lastname and email attributes.
diff --git a/py4web/utils/auth_plugins/ldap_plugin.py b/py4web/utils/auth_plugins/ldap_plugin.py
index 9a60506e..5b4a6b7e 100644
--- a/py4web/utils/auth_plugins/ldap_plugin.py
+++ b/py4web/utils/auth_plugins/ldap_plugin.py
@@ -794,7 +794,7 @@ class LDAPPlugin(UsernamePassword):
con.start_tls_s()
return con
- def get_user_groups_from_ldap(self, con, username):
+ def get_user_groups_from_ldap(self, username=None, password=None):
"""
Get all group names from ldap where the user is in
"""
@@ -832,6 +832,10 @@ class LDAPPlugin(UsernamePassword):
logger = self.logger
groups = self.groups
+ # if username is None, return empty list
+ if username is None:
+ return []
+
logger.info("[%s] Get user groups from ldap" % str(username))
#
# Get all group name where the user is in actually in ldap
@@ -847,7 +851,17 @@ class LDAPPlugin(UsernamePassword):
domain.append(x.split("=")[-1])
username = "%s@%s" % (username, ".".join(domain))
username_bare = username.split("@")[0]
+ con = self._init_ldap()
con.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
+ try:
+ if bind_dn:
+ # need to search directory with an admin account 1st
+ con.simple_bind_s(bind_dn, bind_pw)
+ else:
+ # credentials should be in the form of user...@domain.tld
+ con.simple_bind_s(username, password)
+ except (ldap.INVALID_CREDENTIALS, ldap.OPERATIONS_ERROR):
+ return []
# In cases where ForestDnsZones and DomainDnsZones are found,
# result will look like the following:
# ['ldap://
ForestDnsZones.domain.com/DC=ForestDnsZones,
@@ -880,7 +894,6 @@ class LDAPPlugin(UsernamePassword):
ldap_groups_of_the_user.append(
str(group[group_name_attrib][0], encoding="utf-8")
)
- print(ldap_groups_of_the_user)
logger.debug("User groups: %s" % ldap_groups_of_the_user)
return list(ldap_groups_of_the_user)
@@ -889,5 +902,6 @@ class LDAPPlugin(UsernamePassword):
filterstr = filterstr[1:-1] # parens added again where used
return []
- def check_credentials(self, username, password):
+# def check_credentials(self, username, password):
return self.is_user_in_allowed_groups(username, password)
+#