con.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
# In cases where ForestDnsZones and DomainDnsZones are found,
# result will look like the following:
# DC=domain,DC=com']
if ldap_binddn:
# need to search directory with an admin account 1st
con.simple_bind_s(ldap_binddn, ldap_bindpw)
else:
# credentials should be in the form of user...@domain.tld
con.simple_bind_s(username, password)
# this will throw an index error if the account is not found
# in the ldap_basedn
requested_attrs = ['sAMAccountName']
if manage_user:
requested_attrs.extend([user_firstname_attrib,
user_lastname_attrib,
user_mail_attrib])
result = con.search_ext_s(
ldap_basedn, ldap.SCOPE_SUBTREE,
"(&(sAMAccountName=%s)(%s))" % (
ldap.filter.escape_filter_chars(username_bare),
filterstr),
requested_attrs)[0][1]
if not isinstance(result, dict):
# result should be a dict in the form
# {'sAMAccountName': [username_bare]}
logger.warning('User [%s] not found!' % username)
return False
if ldap_binddn:
# We know the user exists & is in the correct OU
# so now we just check the password
con.simple_bind_s(username, password)
username = username_bare
This peace of code is pretty unreliable : It start by re-creating a email and store it in username vars if username it recieves from web2py is not a email before derive a username_bare from the altered username var and at the end it finally set username = username_bare... Why all this just to avoid create a var?!
I propose to refator this using creating a new ID or identifier var to store connection "identifier" var instead reusing the username for that. Then it will require to determine if the IS_NOT_EMAIL() should go at Auth level or ldap_auth. I don't know so much LDAP in general and even less the different implementation, so I don't know if some implementation use email as an identifier or not. Since, the Auth class as mechanism to create missing user I don't no if it intentional to allow the creation of user with email as username or not... So, maybe it a option in to use IS_NOT_EMAIL() on username field in this case it will require that IS_NOT_EMAIL be at level of Auth. Maybe, I didn't be able to make work my custom validator because of the order of validator (I had set multiple validator on username), I will try to set only IS_NOT_EMAIL and report here if it solve the problem I have with LDAP authentication.
Thanks
Richard