From 18fdeb483e422f8c97bae5d371e61459be95d162 Mon Sep 17 00:00:00 2001 From: kakwa Date: Fri, 8 Feb 2019 20:33:58 +0100 Subject: [PATCH] better handling of the str/byte mess for python3 * add dedicated methods for python 3 in handling of bytearrays/strings * using them to compare attributes checks in AD backend --- ldapcherry/backend/backendAD.py | 4 ++-- ldapcherry/backend/backendLdap.py | 35 +++++++++++++++++++++---------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/ldapcherry/backend/backendAD.py b/ldapcherry/backend/backendAD.py index 5e34715..57bf47a 100644 --- a/ldapcherry/backend/backendAD.py +++ b/ldapcherry/backend/backendAD.py @@ -144,10 +144,10 @@ class Backend(ldapcherry.backend.backendLdap.Backend): for a in attrslist: self.attrlist.append(self._str(a)) - if b'cn' not in self.attrlist: + if self._str('cn') not in self.attrlist: raise MissingAttr() - if b'unicodePwd' not in self.attrlist: + if self._str('unicodePwd') not in self.attrlist: raise MissingAttr() def _search_group(self, searchfilter, groupdn): diff --git a/ldapcherry/backend/backendLdap.py b/ldapcherry/backend/backendLdap.py index 9bf177c..b777929 100644 --- a/ldapcherry/backend/backendLdap.py +++ b/ldapcherry/backend/backendLdap.py @@ -312,22 +312,35 @@ class Backend(ldapcherry.backend.Backend): else: dn_entry = r[0] return dn_entry + # python-ldap talks in bytes, # as the rest of ldapcherry talks in unicode utf-8: # * everything passed to python-ldap must be converted to bytes # * everything coming from python-ldap must be converted to unicode + if sys.version < '3': + def _str(self, s): + """unicode -> bytes conversion""" + if s is None: + return None + return s.encode('utf-8') + def _uni(self, s): + """bytes -> unicode conversion""" + if s is None: + return None + return s.decode('utf-8', 'ignore') + else: + def _str(self, s): + """unicode -> bytes conversion""" + return s - def _str(self, s): - """unicode -> bytes conversion""" - if s is None: - return None - return s.encode('utf-8') - - def _uni(self, s): - """bytes -> unicode conversion""" - if s is None: - return None - return s.decode('utf-8', 'ignore') + def _uni(self, s): + """bytes -> unicode conversion""" + if s is None: + return None + if type(s) is not str: + return s.decode('utf-8', 'ignore') + else: + return s def auth(self, username, password): """Authentication of a user"""