simplify authentication

This commit is contained in:
2023-01-06 14:40:02 +00:00
parent 7fb89158a9
commit 0ee942e296

View File

@@ -1,6 +1,6 @@
import logging
import ldap3
from ldap3 import Server, Connection
from ldap3.core.exceptions import LDAPBindError
logger = logging.getLogger(__name__)
@@ -12,19 +12,18 @@ def authentication(username, password):
return None
server_uri = "ldap://juri.fet.htu.tuwien.ac.at"
server = ldap3.Server(server_uri, port=389, use_ssl=True)
server = Server(server_uri, port=389, use_ssl=True)
userdn = f"uid={username},ou=user,dc=fet,dc=htu,dc=tuwien,dc=ac,dc=at"
try:
conn = ldap3.Connection(server, user=userdn, password=password, auto_bind=True)
conn.search("dc=fet,dc=htu,dc=tuwien,dc=ac,dc=at", "(objectclass=person)")
for user in sorted(conn.entries):
if f"DN: uid={username}" in str(user):
return username
c = Connection(server, user=userdn, password=password, auto_bind=True)
if c.extend.standard.who_am_i():
return username
except LDAPBindError as e:
logger.info(f"Username does not exist. Error: {e}")
logger.info(f"LDAP Bind Error. Error: {e}")
except Exception as e:
logger.info(f"Connection to server lost. Error: {e}")
logger.info(f"Auth Exception. Error: {e}")
logger.info(f"This username has been typed: '{username}'")
return None