5
5
import hashlib
6
6
from datetime import datetime
7
7
8
- import ldap
8
+ from ldap3 import Server , Connection , ALL
9
+ from ldap3 .core .exceptions import LDAPBindError , LDAPCertificateError
9
10
from flask import current_app
10
11
from flask_sqlalchemy import BaseQuery
11
12
@@ -57,9 +58,7 @@ def authenticate_with_key(self, key, secret, args, path):
57
58
return user , authenticated
58
59
59
60
def authenticate_with_ldap (self , username , password ):
60
- ldap_conn = ldap .initialize (current_app .config .get ('LDAP_SERVER' ))
61
- ldap_conn .protocol_version = 3
62
- ldap_conn .set_option (ldap .OPT_REFERRALS , 0 )
61
+ server = Server (current_app .config .get ('LDAP_SERVER' ), get_info = ALL )
63
62
if '@' in username :
64
63
email = username
65
64
who = current_app .config .get ('LDAP_USER_DN' ).format (username .split ('@' )[0 ])
@@ -70,11 +69,12 @@ def authenticate_with_ldap(self, username, password):
70
69
username = username .split ('@' )[0 ]
71
70
user = self .get_by_username (username )
72
71
try :
73
-
74
72
if not password :
75
- raise ldap . INVALID_CREDENTIALS
73
+ raise LDAPCertificateError
76
74
77
- ldap_conn .simple_bind_s (who , password )
75
+ conn = Connection (server , user = who , password = password )
76
+ conn .bind ()
77
+ conn .unbind ()
78
78
79
79
if not user :
80
80
from api .lib .perm .acl .user import UserCRUD
@@ -84,7 +84,7 @@ def authenticate_with_ldap(self, username, password):
84
84
op_record .apply_async (args = (None , username , OperateType .LOGIN , ["ACL" ]), queue = ACL_QUEUE )
85
85
86
86
return user , True
87
- except ldap . INVALID_CREDENTIALS :
87
+ except LDAPBindError :
88
88
return user , False
89
89
90
90
def search (self , key ):
0 commit comments