7272
7373# Used by auth modules
7474import sqlite3
75- import ldap
75+ import ldap3
7676import string
7777import random
7878
@@ -298,7 +298,7 @@ def __init__(self, name, username, password, authoritative_source, auth_options=
298298 self ._logger .debug ('Creating LdapAuth instance' )
299299
300300 self ._logger .debug ('LDAP URI: ' + self ._ldap_uri )
301- self ._ldap_conn = ldap . initialize (self ._ldap_uri )
301+ self ._ldap_conn = ldap3 . Server (self ._ldap_uri )
302302
303303
304304
@@ -314,29 +314,27 @@ def authenticate(self):
314314 return self ._authenticated
315315
316316 try :
317- self ._ldap_conn .simple_bind_s ('uid=' + self .username + ',' + self ._ldap_basedn , self .password )
318- except ldap .SERVER_DOWN as exc :
317+ with ldap3 .Connection (self ._ldap_conn , 'uid=' + self .username + ',' + self ._ldap_basedn , self .password , raise_exceptions = True ) as con :
318+ res = con .search (self ._ldap_basedn , '(uid=' + self .username + ')' , ldap3 .SEARCH_SCOPE_WHOLE_SUBTREE , attributes = ['cn' ], size_limit = 1 )
319+ if (not res ) or (not con .response ):
320+ self .full_name = ''
321+ else :
322+ self .full_name = con .response [0 ]['attributes' ]['cn' ][0 ]
323+ except ldap3 .LDAPSocketOpenError as exc :
319324 raise AuthError ('Could not connect to LDAP server' )
320- except (ldap . INVALID_CREDENTIALS , ldap . INVALID_DN_SYNTAX ,
321- ldap . UNWILLING_TO_PERFORM ) as exc :
325+ except (ldap3 . LDAPInvalidCredentialsResult , ldap3 . LDAPInvalidDNSyntaxResult ,
326+ ldap3 . LDAPUnwillingToPerformResult ) as exc :
322327 # Auth failed
323328 self ._logger .debug ('erroneous password for user %s' % self .username )
324329 self ._authenticated = False
325330 return self ._authenticated
326331
327-
328332 # auth succeeded
329333 self .authenticated_as = self .username
330334 self ._authenticated = True
331335 self .trusted = False
332336 self .readonly = False
333337
334- try :
335- res = self ._ldap_conn .search_s (self ._ldap_basedn , ldap .SCOPE_SUBTREE , 'uid=' + self .username , ['cn' ])
336- self .full_name = res [0 ][1 ]['cn' ][0 ]
337- except :
338- self .full_name = ''
339-
340338 self ._logger .debug ('successfully authenticated as %s, username %s' % (self .authenticated_as , self .username ))
341339 return self ._authenticated
342340
0 commit comments