simplify authentication
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
import ldap3
|
from ldap3 import Server, Connection
|
||||||
from ldap3.core.exceptions import LDAPBindError
|
from ldap3.core.exceptions import LDAPBindError
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -12,19 +12,18 @@ def authentication(username, password):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
server_uri = "ldap://juri.fet.htu.tuwien.ac.at"
|
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"
|
userdn = f"uid={username},ou=user,dc=fet,dc=htu,dc=tuwien,dc=ac,dc=at"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
conn = ldap3.Connection(server, user=userdn, password=password, auto_bind=True)
|
c = Connection(server, user=userdn, password=password, auto_bind=True)
|
||||||
conn.search("dc=fet,dc=htu,dc=tuwien,dc=ac,dc=at", "(objectclass=person)")
|
if c.extend.standard.who_am_i():
|
||||||
for user in sorted(conn.entries):
|
return username
|
||||||
if f"DN: uid={username}" in str(user):
|
|
||||||
return username
|
|
||||||
except LDAPBindError as e:
|
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:
|
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}'")
|
logger.info(f"This username has been typed: '{username}'")
|
||||||
return None
|
return None
|
||||||
|
|||||||
Reference in New Issue
Block a user