feat(reset): successfully linked the app with the LDAP server
This commit is contained in:
60
app/ldap_client.py
Normal file
60
app/ldap_client.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import ldap3
|
||||
from typing import Tuple
|
||||
|
||||
class Client():
|
||||
def __init__(self, address: str, port: int, base_dn: str, primary_attribute: str = "uid", tls: bool = False):
|
||||
self.server = ldap3.Server(host=address, port=port, use_ssl=tls)
|
||||
self.base_dn = base_dn
|
||||
self.address = address
|
||||
self.port = port
|
||||
self.tls = tls
|
||||
self.primary_attribute = primary_attribute
|
||||
|
||||
def bind(self, user: str, bind_passwd: str) -> Tuple[bool, str]:
|
||||
user_dn = f"{self.primary_attribute}={user},{self.base_dn}"
|
||||
|
||||
self.link = ldap3.Connection(self.server, user=user_dn, password=bind_passwd)
|
||||
|
||||
try:
|
||||
status = self.link.bind()
|
||||
except Exception as _:
|
||||
status = False
|
||||
|
||||
if status == False:
|
||||
print(f"[!!] Could not bind {user_dn} to the LDAP directory: {self.link.last_error}")
|
||||
return (status, "")
|
||||
|
||||
return (status, user_dn)
|
||||
|
||||
def unbind(self) -> bool:
|
||||
if self.link.bound != True:
|
||||
return False
|
||||
|
||||
try:
|
||||
self.link.unbind()
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
return True
|
||||
|
||||
def change_pwd(self, user_dn: str, new_password: str) -> bool:
|
||||
if self.link.bound == False:
|
||||
print("[!!] Can't change the password: not bound to the server")
|
||||
return False
|
||||
|
||||
status = self.link.modify(user_dn, {'userPassword': [(ldap3.MODIFY_REPLACE, [new_password])]})
|
||||
if status == True:
|
||||
print(f"[++] Changed password of user {user_dn}")
|
||||
else:
|
||||
print(f"[!!] Could not change password of user {user_dn}: {self.link.last_error}")
|
||||
|
||||
return status
|
||||
|
||||
if __name__ == "__main__":
|
||||
client = Client("dc01.lan.alxczl.fr", 636, "cn=users,cn=accounts,dc=lan,dc=alxczl,dc=fr", True)
|
||||
client_dn = "uid=alexandre,cn=users,cn=accounts,dc=lan,dc=alxczl,dc=fr"
|
||||
res = client.bind(client_dn, "Getshrektm8")
|
||||
if res[0] == False:
|
||||
print(client.link.result["description"])
|
||||
|
||||
#client.link.unbind()
|
||||
Reference in New Issue
Block a user