Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
Fix LDAP and Python 3.7 issues (#18)
Browse files Browse the repository at this point in the history
Fix LDAP and Python 3.7 issues
  • Loading branch information
thinkh authored Jan 27, 2020
2 parents 6cd2de7 + cd47bbc commit 9c003ca
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
20 changes: 11 additions & 9 deletions phovea_security_store_ldap/ldap.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ def _authenticate_direct_bind_and_search(self, username, password):
AuthenticationResponse
"""
import re
import json

connection = self._make_connection(username, password)

Expand Down Expand Up @@ -281,14 +282,14 @@ def _authenticate_direct_bind_and_search(self, username, password):
attributes=self._config.get('user.attributes') or ldap3.ALL_ATTRIBUTES)

if len(connection.response) > 0:
user_info = connection.response[0]['attributes']
user_info = json.loads(connection.response_to_json())["entries"][0]["attributes"]
user_info['dn'] = connection.response[0]['dn']
bind_user = user_info['dn']
else:
raise Exception

if len(connection.response) > 0 and 'memberOf' in connection.response[0]['attributes']:
user_groups = connection.response[0]['attributes'].get('memberOf', [])
if len(connection.response) > 0 and 'memberOf' in json.loads(connection.response_to_json())["entries"][0]["attributes"]:
user_groups = json.loads(connection.response_to_json())["entries"][0]["attributes"].get('memberOf', [])
else:
user_groups = self._get_user_groups(dn=bind_user, _connection=self._choose(connection))

Expand Down Expand Up @@ -325,7 +326,7 @@ def _authenticate_search_bind(self, username, password):
try:
connection.bind()
log.debug('Successfully bound to LDAP as "{0}"'
' for search_bind method'.format(self._config.get('bind_user_nd') or 'Anonymous'))
' for search_bind method'.format(self._config.get('bind_user_dn') or 'Anonymous'))
except Exception:
connection.unbind()
log.exception('unknown')
Expand Down Expand Up @@ -383,7 +384,7 @@ def _refind_user(self, dn):
try:
connection.bind()
log.debug('Successfully bound to LDAP as "{0}" '
'for search_bind method'.format(self._config.get('bind_user_nd') or 'Anonymous'))
'for search_bind method'.format(self._config.get('bind_user_dn') or 'Anonymous'))
infos = self._get_user_info(dn, _connection=self._choose(connection))
groups = self._get_user_groups(dn, _connection=self._choose(connection))
return LDAPUser(infos.get('name', infos.get('cn', dn)), dn=dn, info=infos, groups=groups, group_prop=self._config.get('group.prop', default='dn'))
Expand Down Expand Up @@ -414,7 +415,7 @@ def _get_user_groups(self, dn, group_search_dn=None, _connection=None):

connection = _connection
if not connection:
connection = self._make_connection(bind_user=self._config.get('bind_user_dn'),
connection = self._make_connection(bind_user=dn,
bind_password=self._config.get('bind_user_password'))
connection.bind()

Expand Down Expand Up @@ -455,7 +456,7 @@ def _get_user_groups(self, dn, group_search_dn=None, _connection=None):
if match is not None:
import re
match = re.compile(match)
results = [r for r in results if match.match(r['dn'])]
results = [r for r in list(results) if match.match(r['dn'])]

return results

Expand Down Expand Up @@ -532,10 +533,11 @@ def _get_object(self, dn, filter, attributes, _connection=None):
Returns:
dict: A dictionary of the object info from LDAP
"""
import json

connection = _connection
if not connection:
connection = self._make_connection(bind_user=self._config.get('bind_user_nd'),
connection = self._make_connection(bind_user=self._config.get('bind_user_dn'),
bind_password=self._config.get('bind_user_password')
)
connection.bind()
Expand All @@ -544,7 +546,7 @@ def _get_object(self, dn, filter, attributes, _connection=None):

data = None
if len(connection.response) > 0:
data = connection.response[0]['attributes']
data = json.loads(connection.response_to_json())["entries"][0]['attributes']
data['dn'] = connection.response[0]['dn']

if not _connection:
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Copyright (c) The Caleydo Team. All rights reserved.
# Licensed under the new BSD license, available at http://caleydo.org/license
###############################################################################
from __future__ import with_statement, print_function
from setuptools import setup, find_packages
from codecs import open
from os import path
Expand Down

0 comments on commit 9c003ca

Please sign in to comment.