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
This commit is contained in:
kakwa 2019-02-08 20:33:58 +01:00
parent 12c511b537
commit 18fdeb483e
2 changed files with 26 additions and 13 deletions

View File

@ -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):

View File

@ -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"""