simplify authentications
This commit is contained in:
@@ -10,29 +10,20 @@ def authentication(username, password):
|
||||
if password is None or password.strip() == "":
|
||||
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 = ldap3.Server(server_uri, port=389, use_ssl=True)
|
||||
|
||||
has_user = False
|
||||
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 ("DN: uid=" + str(username.lower())) in str(user):
|
||||
has_user = True
|
||||
if f"DN: uid={username}" in str(user):
|
||||
return username
|
||||
except LDAPBindError as e:
|
||||
logger.info("Username does not exist. Error: {}".format(e))
|
||||
username = None
|
||||
logger.info(f"Username does not exist. Error: {e}")
|
||||
except Exception as e:
|
||||
logger.info("Connection to server lost. Error: {}".format(e))
|
||||
username = None
|
||||
logger.info(f"Connection to server lost. Error: {e}")
|
||||
|
||||
if not has_user:
|
||||
username = None
|
||||
|
||||
return username
|
||||
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.auth import login, logout
|
||||
from django.contrib.auth.models import User
|
||||
from django.shortcuts import render, redirect
|
||||
|
||||
from documents.etherpadlib import del_ep_cookie
|
||||
|
||||
from .authentications import authentication
|
||||
from .decorators import unauthenticated_user, authenticated_user
|
||||
from .forms import LoginForm
|
||||
@@ -13,12 +12,12 @@ from .forms import LoginForm
|
||||
@unauthenticated_user
|
||||
def loginPage(request):
|
||||
if request.method == "POST":
|
||||
username = request.POST.get("username")
|
||||
username = request.POST.get("username").lower()
|
||||
password = request.POST.get("password")
|
||||
|
||||
auth_user = authentication(username, password)
|
||||
|
||||
if auth_user is not None:
|
||||
if auth_user:
|
||||
try:
|
||||
user = User.objects.get(username=auth_user.lower())
|
||||
except User.DoesNotExist:
|
||||
@@ -31,7 +30,7 @@ def loginPage(request):
|
||||
except:
|
||||
return redirect("home")
|
||||
else:
|
||||
messages.info(request, "username or password is incorrect")
|
||||
messages.error(request, "Anmeldung nicht erfolgreich. Bitte überprüfe Benutzername und Passwort.")
|
||||
|
||||
form = LoginForm()
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block title %}Login{% endblock %}
|
||||
{% block title %}Anmeldung{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<!-- Main Content -->
|
||||
<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">
|
||||
<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 %}
|
||||
@@ -19,7 +19,7 @@
|
||||
{% endfor %}
|
||||
|
||||
<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">
|
||||
</label>
|
||||
<label class="block">
|
||||
|
||||
Reference in New Issue
Block a user