simplify authentications
This commit is contained in:
@@ -10,29 +10,20 @@ def authentication(username, password):
|
|||||||
if password is None or password.strip() == "":
|
if password is None or password.strip() == "":
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# username format
|
|
||||||
new_username = "uid={username},ou=user,dc=fet,dc=htu,dc=tuwien,dc=ac,dc=at"
|
|
||||||
userdn = new_username.format(username=username)
|
|
||||||
|
|
||||||
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 = ldap3.Server(server_uri, port=389, use_ssl=True)
|
||||||
|
userdn = f"uid={username},ou=user,dc=fet,dc=htu,dc=tuwien,dc=ac,dc=at"
|
||||||
has_user = False
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
conn = ldap3.Connection(server, user=userdn, password=password, auto_bind=True)
|
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)")
|
conn.search("dc=fet,dc=htu,dc=tuwien,dc=ac,dc=at", "(objectclass=person)")
|
||||||
for user in sorted(conn.entries):
|
for user in sorted(conn.entries):
|
||||||
if ("DN: uid=" + str(username.lower())) in str(user):
|
if f"DN: uid={username}" in str(user):
|
||||||
has_user = True
|
|
||||||
except LDAPBindError as e:
|
|
||||||
logger.info("Username does not exist. Error: {}".format(e))
|
|
||||||
username = None
|
|
||||||
except Exception as e:
|
|
||||||
logger.info("Connection to server lost. Error: {}".format(e))
|
|
||||||
username = None
|
|
||||||
|
|
||||||
if not has_user:
|
|
||||||
username = None
|
|
||||||
|
|
||||||
return username
|
return username
|
||||||
|
except LDAPBindError as e:
|
||||||
|
logger.info(f"Username does not exist. Error: {e}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.info(f"Connection to server lost. Error: {e}")
|
||||||
|
|
||||||
|
logger.info(f"This username has been typed: '{username}'")
|
||||||
|
return None
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
from django.shortcuts import render, redirect
|
|
||||||
from django.contrib.auth import login, logout
|
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
|
from django.contrib.auth import login, logout
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
from django.shortcuts import render, redirect
|
||||||
|
|
||||||
from documents.etherpadlib import del_ep_cookie
|
from documents.etherpadlib import del_ep_cookie
|
||||||
|
|
||||||
from .authentications import authentication
|
from .authentications import authentication
|
||||||
from .decorators import unauthenticated_user, authenticated_user
|
from .decorators import unauthenticated_user, authenticated_user
|
||||||
from .forms import LoginForm
|
from .forms import LoginForm
|
||||||
@@ -13,12 +12,12 @@ from .forms import LoginForm
|
|||||||
@unauthenticated_user
|
@unauthenticated_user
|
||||||
def loginPage(request):
|
def loginPage(request):
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
username = request.POST.get("username")
|
username = request.POST.get("username").lower()
|
||||||
password = request.POST.get("password")
|
password = request.POST.get("password")
|
||||||
|
|
||||||
auth_user = authentication(username, password)
|
auth_user = authentication(username, password)
|
||||||
|
|
||||||
if auth_user is not None:
|
if auth_user:
|
||||||
try:
|
try:
|
||||||
user = User.objects.get(username=auth_user.lower())
|
user = User.objects.get(username=auth_user.lower())
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
@@ -31,7 +30,7 @@ def loginPage(request):
|
|||||||
except:
|
except:
|
||||||
return redirect("home")
|
return redirect("home")
|
||||||
else:
|
else:
|
||||||
messages.info(request, "username or password is incorrect")
|
messages.error(request, "Anmeldung nicht erfolgreich. Bitte überprüfe Benutzername und Passwort.")
|
||||||
|
|
||||||
form = LoginForm()
|
form = LoginForm()
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
{% extends 'base.html' %}
|
{% extends 'base.html' %}
|
||||||
|
|
||||||
{% block title %}Login{% endblock %}
|
{% block title %}Anmeldung{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<!-- Main Content -->
|
<!-- Main Content -->
|
||||||
<main class="container mx-auto w-full px-4 my-8 flex-grow flex flex-col">
|
<main class="container mx-auto w-full px-4 my-8 flex-grow flex flex-col">
|
||||||
<h1 class="page-title">Login für FET-Mitarbeiter</h1>
|
<h1 class="page-title">Anmeldung für FET-Mitarbeiter</h1>
|
||||||
<div class="w-full h-full flex-1 flex justify-center items-center">
|
<div class="w-full h-full flex-1 flex justify-center items-center">
|
||||||
<form action="" method="POST" class="sm:p-4 sm:w-3/5 md:w-1/2 lg:w-2/5 xl:w-1/3 2xl:w-1/4 grid grid-cols-1 gap-3 sm:gap-6">
|
<form action="" method="POST" class="sm:p-4 sm:w-3/5 md:w-1/2 lg:w-2/5 xl:w-1/3 2xl:w-1/4 grid grid-cols-1 gap-3 sm:gap-6">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
@@ -19,7 +19,7 @@
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
<label class="block">
|
<label class="block">
|
||||||
<span class="text-gray-700 dark:text-gray-200">Username</span>
|
<span class="text-gray-700 dark:text-gray-200">Benutzername</span>
|
||||||
<input type="text" name="username" class="mt-1 block w-full rounded-md border-gray-300 dark:border-none shadow-sm focus:border-none focus:ring focus:ring-blue-200 dark:focus:ring-sky-700 focus:ring-opacity-50" required="required">
|
<input type="text" name="username" class="mt-1 block w-full rounded-md border-gray-300 dark:border-none shadow-sm focus:border-none focus:ring focus:ring-blue-200 dark:focus:ring-sky-700 focus:ring-opacity-50" required="required">
|
||||||
</label>
|
</label>
|
||||||
<label class="block">
|
<label class="block">
|
||||||
|
|||||||
Reference in New Issue
Block a user