Merge pull request #23 from thomasDOTde/ldapfix-verdel

Fix some issues with LDAP authorization
Thanks to @verdel for contribution!
This commit is contained in:
Thomas 2018-02-10 13:11:49 +01:00 committed by GitHub
commit e144cf4fd9

View File

@ -9,6 +9,7 @@ import traceback
import pyotp import pyotp
import re import re
import dns.reversename import dns.reversename
import sys
from datetime import datetime from datetime import datetime
from distutils.util import strtobool from distutils.util import strtobool
@ -192,11 +193,13 @@ class User(db.Model):
logging.error('LDAP authentication is disabled') logging.error('LDAP authentication is disabled')
return False return False
searchFilter = "(&(objectcategory=person)(samaccountname=%s))" % self.username if LDAP_TYPE == 'ad':
if LDAP_TYPE == 'ldap': searchFilter = "(&(objectcategory=person)(%s=%s)(%s))" % (LDAP_USERNAMEFIELD, self.username, LDAP_FILTER)
searchFilter = "(&(%s=%s)%s)" % (LDAP_USERNAMEFIELD, self.username, LDAP_FILTER)
logging.info('Ldap searchFilter "%s"' % searchFilter)
elif LDAP_TYPE == 'ldap':
searchFilter = "(&(%s=%s)(%s))" % (LDAP_USERNAMEFIELD, self.username, LDAP_FILTER)
logging.info('Ldap searchFilter "%s"' % searchFilter)
result = self.ldap_search(searchFilter, LDAP_SEARCH_BASE) result = self.ldap_search(searchFilter, LDAP_SEARCH_BASE)
if not result: if not result:
logging.warning('User "%s" does not exist' % self.username) logging.warning('User "%s" does not exist' % self.username)
@ -257,6 +260,13 @@ class User(db.Model):
# this might be changed in the future # this might be changed in the future
self.firstname = result[0][0][1]['givenName'][0] self.firstname = result[0][0][1]['givenName'][0]
self.lastname = result[0][0][1]['sn'][0] self.lastname = result[0][0][1]['sn'][0]
self.email = result[0][0][1]['mail'][0]
if sys.version_info < (3,):
if isinstance(self.firstname, str):
self.firstname = self.firstname.decode('utf-8')
if isinstance(self.lastname, str):
self.lastname = self.lastname.decode('utf-8')
except: except:
self.firstname = self.username self.firstname = self.username
self.lastname = '' self.lastname = ''