This repository has been archived on 2022-05-05. You can view files and clone it, but cannot push or open issues or pull requests.
userausfall/userausfall/ldap.py
2021-10-21 09:05:08 +02:00

47 lines
1.3 KiB
Python

from django.conf import settings
from ldap3 import Server, Connection, SYNC
def create_account(username, raw_password):
connection = _get_connection()
is_success = connection.add(
f"cn={username},dc=local",
["simpleSecurityObject", "organizationalRole"],
{"userPassword": raw_password},
)
return is_success
def account_exists(username):
connection = _get_connection()
exists = connection.search(
f"cn={username},dc=local", "(objectclass=simpleSecurityObject)"
)
return exists
def is_valid_account_data(username, raw_password):
connection = _get_connection()
is_valid = connection.search(
f"cn={username},dc=local",
"(objectclass=simpleSecurityObject)",
attributes=["userPassword"],
)
if is_valid:
is_valid = connection.entries[0]["userPassword"].value == raw_password
return is_valid
def _get_connection():
server = Server("localhost")
# The SAFE_SYNC client strategy doesn't seem to be present in Buster version of ldap3. We might want to use it as
# soon as it is available (multithreading).
connection = Connection(
server,
settings.USERAUSFALL_LDAP["ADMIN_USER_DN"],
settings.USERAUSFALL_LDAP["ADMIN_USER_PASSWORD"],
client_strategy=SYNC,
auto_bind=True,
)
return connection