Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 887a69c

Browse files
feat: python-ldap 更新到 ldap3 (veops#214)
Co-authored-by: sherlock <[email protected]>
1 parent 6d052ea commit 887a69c

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

cmdb-api/Pipfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ supervisor = "==4.0.3"
2424
Flask-Login = "==0.6.2"
2525
Flask-Bcrypt = "==1.0.1"
2626
Flask-Cors = ">=3.0.8"
27-
python-ldap = "==3.4.0"
27+
ldap3 = "==2.9.1"
2828
pycryptodome = "==3.12.0"
2929
# Caching
3030
Flask-Caching = ">=1.0.0"

cmdb-api/api/models/acl.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import hashlib
66
from datetime import datetime
77

8-
import ldap
8+
from ldap3 import Server, Connection, ALL
9+
from ldap3.core.exceptions import LDAPBindError, LDAPCertificateError
910
from flask import current_app
1011
from flask_sqlalchemy import BaseQuery
1112

@@ -57,9 +58,7 @@ def authenticate_with_key(self, key, secret, args, path):
5758
return user, authenticated
5859

5960
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)
6362
if '@' in username:
6463
email = username
6564
who = current_app.config.get('LDAP_USER_DN').format(username.split('@')[0])
@@ -70,11 +69,12 @@ def authenticate_with_ldap(self, username, password):
7069
username = username.split('@')[0]
7170
user = self.get_by_username(username)
7271
try:
73-
7472
if not password:
75-
raise ldap.INVALID_CREDENTIALS
73+
raise LDAPCertificateError
7674

77-
ldap_conn.simple_bind_s(who, password)
75+
conn = Connection(server, user=who, password=password)
76+
conn.bind()
77+
conn.unbind()
7878

7979
if not user:
8080
from api.lib.perm.acl.user import UserCRUD
@@ -84,7 +84,7 @@ def authenticate_with_ldap(self, username, password):
8484
op_record.apply_async(args=(None, username, OperateType.LOGIN, ["ACL"]), queue=ACL_QUEUE)
8585

8686
return user, True
87-
except ldap.INVALID_CREDENTIALS:
87+
except LDAPBindError:
8888
return user, False
8989

9090
def search(self, key):

cmdb-api/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Pillow==9.3.0
3232
pycryptodome==3.12.0
3333
PyJWT==2.4.0
3434
PyMySQL==1.1.0
35-
python-ldap==3.4.0
35+
ldap3==2.9.1
3636
PyYAML==6.0
3737
redis==4.6.0
3838
requests==2.31.0

0 commit comments

Comments
 (0)