Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LDAP user search/custom mapping + TLS support #545

Merged
merged 9 commits into from
Jul 21, 2023
Prev Previous commit
Next Next commit
Change various default settings from empty string to None
  • Loading branch information
cecilialau6776 committed Jun 6, 2023
commit d645fc2535b8644b90675b1b5f831c402e9f9406
12 changes: 6 additions & 6 deletions coldfront/config/plugins/ldap_user_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@

LDAP_USER_SEARCH_SERVER_URI = ENV.str('LDAP_USER_SEARCH_SERVER_URI')
LDAP_USER_SEARCH_BASE = ENV.str('LDAP_USER_SEARCH_BASE')
LDAP_USER_SEARCH_BIND_DN = ENV.str('LDAP_USER_SEARCH_BIND_DN')
LDAP_USER_SEARCH_BIND_PASSWORD = ENV.str('LDAP_USER_SEARCH_BIND_PASSWORD')
LDAP_USER_SEARCH_BIND_DN = ENV.str('LDAP_USER_SEARCH_BIND_DN', default=None)
LDAP_USER_SEARCH_BIND_PASSWORD = ENV.str('LDAP_USER_SEARCH_BIND_PASSWORD', default=None)
LDAP_USER_SEARCH_CONNECT_TIMEOUT = ENV.float('LDAP_USER_SEARCH_CONNECT_TIMEOUT', default=2.5)
LDAP_USER_SEARCH_USE_SSL = ENV.bool('LDAP_USER_SEARCH_USE_SSL', default=True)
LDAP_USER_SEARCH_USE_TLS = ENV.bool('LDAP_USER_SEARCH_USE_TLS', default=False)
LDAP_USER_SEARCH_PRIV_KEY_FILE = ENV.str("LDAP_USER_SEARCH_PRIV_KEY_FILE", "")
LDAP_USER_SEARCH_CERT_FILE = ENV.str("LDAP_USER_SEARCH_CERT_FILE", "")
LDAP_USER_SEARCH_CACERT_FILE = ENV.str("LDAP_USER_SEARCH_CACERT_FILE", "")
LDAP_USER_SEARCH_PRIV_KEY_FILE = ENV.str("LDAP_USER_SEARCH_PRIV_KEY_FILE", default=None)
LDAP_USER_SEARCH_CERT_FILE = ENV.str("LDAP_USER_SEARCH_CERT_FILE", default=None)
LDAP_USER_SEARCH_CACERT_FILE = ENV.str("LDAP_USER_SEARCH_CACERT_FILE", default=None)

ADDITIONAL_USER_SEARCH_CLASSES = ['coldfront.plugins.ldap_user_search.utils.LDAPUserSearch',]
ADDITIONAL_USER_SEARCH_CLASSES = ['coldfront.plugins.ldap_user_search.utils.LDAPUserSearch']
42 changes: 15 additions & 27 deletions coldfront/plugins/ldap_user_search/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ def __init__(self, user_search_string, search_by):
self.LDAP_CONNECT_TIMEOUT = import_from_settings('LDAP_USER_SEARCH_CONNECT_TIMEOUT', 2.5)
self.LDAP_USE_SSL = import_from_settings('LDAP_USER_SEARCH_USE_SSL', True)
self.LDAP_USE_TLS = import_from_settings("LDAP_USER_SEARCH_USE_TLS", False)
self.LDAP_PRIV_KEY_FILE = import_from_settings('LDAP_USER_SEARCH_PRIV_KEY_FILE', '')
self.LDAP_CERT_FILE = import_from_settings('LDAP_USER_SEARCH_CERT_FILE', '')
self.LDAP_CACERT_FILE = import_from_settings('LDAP_USER_SEARCH_CACERT_FILE', '')
self.LDAP_PRIV_KEY_FILE = import_from_settings('LDAP_USER_SEARCH_PRIV_KEY_FILE', None)
self.LDAP_CERT_FILE = import_from_settings('LDAP_USER_SEARCH_CERT_FILE', None)
self.LDAP_CACERT_FILE = import_from_settings('LDAP_USER_SEARCH_CACERT_FILE', None)

tls = None
if self.LDAP_USE_TLS:
Expand All @@ -40,46 +40,34 @@ def parse_ldap_entry(self, entry):
entry_dict = json.loads(entry.entry_to_json()).get('attributes')

user_dict = {
'last_name':
entry_dict.get('sn')[0] if entry_dict.get('sn') else '',
'first_name':
entry_dict.get('givenName')[0]
if entry_dict.get('givenName') else '',
'username':
entry_dict.get('uid')[0] if entry_dict.get('uid') else '',
'email':
entry_dict.get('mail')[0] if entry_dict.get('mail') else '',
'source':
self.search_source,
'last_name': entry_dict.get('sn')[0] if entry_dict.get('sn') else '',
'first_name': entry_dict.get('givenName')[0] if entry_dict.get('givenName') else '',
'username': entry_dict.get('uid')[0] if entry_dict.get('uid') else '',
'email': entry_dict.get('mail')[0] if entry_dict.get('mail') else '',
'source': self.search_source,
}

return user_dict

def search_a_user(self, user_search_string=None, search_by='all_fields'):
size_limit = 50
if user_search_string and search_by == 'all_fields':
filter = ldap.filter.filter_format(
"(|(givenName=*%s*)(sn=*%s*)(uid=*%s*)(mail=*%s*))",
[user_search_string] * 4)
filter = ldap.filter.filter_format("(|(givenName=*%s*)(sn=*%s*)(uid=*%s*)(mail=*%s*))", [user_search_string] * 4)
elif user_search_string and search_by == 'username_only':
filter = ldap.filter.filter_format("(uid=%s)",
[user_search_string])
filter = ldap.filter.filter_format("(uid=%s)", [user_search_string])
size_limit = 1
else:
filter = '(objectclass=person)'

searchParameters = {
'search_base': self.LDAP_USER_SEARCH_BASE,
'search_filter': filter,
'attributes': ['uid', 'sn', 'givenName', 'mail'],
'size_limit': size_limit
}
searchParameters = {'search_base': self.LDAP_USER_SEARCH_BASE,
'search_filter': filter,
'attributes': ['uid', 'sn', 'givenName', 'mail'],
'size_limit': size_limit}
self.conn.search(**searchParameters)
users = []
for idx, entry in enumerate(self.conn.entries, 1):
user_dict = self.parse_ldap_entry(entry)
users.append(user_dict)

logger.info("LDAP user search for %s found %s results",
user_search_string, len(users))
logger.info("LDAP user search for %s found %s results", user_search_string, len(users))
return users