From 4450dbcee7334da91eea9560f6b7c8426ab6f860 Mon Sep 17 00:00:00 2001 From: amgn1 <dsfunshteyn@miem.hse.ru> Date: Mon, 13 Mar 2023 00:51:28 +0300 Subject: [PATCH] feat: Added auth fronend via modal window --- dev/req3d/req3d/auth.py | 28 +++++++++++++ dev/req3d/req3d/settings.py | 4 ++ dev/req3d/req3d/templates/base_generic.html | 46 ++++++++++++++++++--- 3 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 dev/req3d/req3d/auth.py diff --git a/dev/req3d/req3d/auth.py b/dev/req3d/req3d/auth.py new file mode 100644 index 00000000..4db0a379 --- /dev/null +++ b/dev/req3d/req3d/auth.py @@ -0,0 +1,28 @@ +import datetime +import hashlib + +from django.conf import settings +from django.contrib.auth.backends import ModelBackend +from django.contrib.auth.models import User +from django.core.cache import cache + + +class BruteForceProtectedAuthBackend(ModelBackend): + def authenticate(self, request, username=None, password=None): + if username is None: + return None + + if getattr(settings, 'AUTH_BLOCK_RATE', None): + now = datetime.datetime.now() + key = hashlib.md5(username.encode('utf-8')).hexdigest() + last_user_login = cache.get(key + '-login-timestamp', now - datetime.timedelta(days=1)) + cache.set(key + '-login-timestamp', now) + if (now - last_user_login) < datetime.timedelta(seconds=settings.AUTH_BLOCK_RATE): + return None + + try: + user = User.objects.get(username=username) + if user.check_password(password): + return user + except User.DoesNotExist: + return None \ No newline at end of file diff --git a/dev/req3d/req3d/settings.py b/dev/req3d/req3d/settings.py index c6450bab..d110fa66 100644 --- a/dev/req3d/req3d/settings.py +++ b/dev/req3d/req3d/settings.py @@ -54,6 +54,10 @@ MIDDLEWARE = [ ROOT_URLCONF = 'req3d.urls' +AUTHENTICATION_BACKENDS = [ + 'auth.BruteForceProtectedAuthBackend', +] + TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', diff --git a/dev/req3d/req3d/templates/base_generic.html b/dev/req3d/req3d/templates/base_generic.html index 9011a355..7f4e3575 100644 --- a/dev/req3d/req3d/templates/base_generic.html +++ b/dev/req3d/req3d/templates/base_generic.html @@ -33,7 +33,7 @@ <div class="mt-auto"> <nav class="navbar navbar-expand-lg", style="background-color: #d6e6ff; padding: 1.25% 5% 1.5% 5%"> <div class="container-fluid"> - <a class="navbar-brand" href="#"> + <a class="navbar-brand" href="https://miem.hse.ru/"> <img src="{% static 'home/img/_svg.png' %}" width="110" height="28" alt="" loading="lazy"> </a> <button class="navbar-toggler" type="button" data-bs-toggle="offcanvas" data-bs-target="#navbarOffcanvasLg" aria-controls="navbarOffcanvasLg"> @@ -67,7 +67,7 @@ </li> {% else %} <li class="nav-item"> - <a class="nav-link" href="#"><i class="bi bi-box-arrow-in-right"></i> Войти</a> + <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#authmodal" data-bs-dismiss="offcanvas"><i class="bi bi-box-arrow-in-right"></i> Войти</button> </li> {% endif %} </ul> @@ -89,7 +89,7 @@ <div class="row mt-4"> <!--Grid column--> <div class="col-lg-4 col-md-12 mb-4 mb-md-0"> - <a class="navbar-brand" href="#"> + <a class="navbar-brand" href="https://miem.hse.ru/"> <img src="{% static 'home/img/_svg.png' %}" width="110" height="28" alt="" loading="lazy"> </a> @@ -113,7 +113,7 @@ <span class="fa-li"></span><span class="ms-2">123458, РњРѕСЃРєРІР°, СѓР». Таллинская, Рґ. 34</span> </li> <li class="mb-3"> - <span class="fa-li"></span><a href="#" class="link-light ms-2">imotajlenko@hse.ru</a> + <span class="fa-li"></span><a href="mailto:imotajlenko@hse.ru" class="link-light ms-2">imotajlenko@hse.ru</a> </li> <li class="mb-3"> <span class="fa-li"></span><span class="ms-2">Мотайленко Рлья Александрович</span> @@ -146,6 +146,42 @@ <!-- Grid container --> </footer> </div> - + + <div class="modal fade" id="authmodal" tabindex="-1"> + <div class="modal-dialog"> + <div class="modal-content"> + <div class="modal-header"> + <h4 class="modal-title">Авторизация</h4> + </div> + <div class="modal-body"> + {% if user.is_authenticated %} + <form method="post"> + {% csrf_token %} + <input type="submit" name="logout"/> + </form> + {% else %} + <form method="post"> + {% csrf_token %} + <div class="mb-3"> + <label for="id_username" class="form-label">Email address</label> + <input type="email" name="username" class="form-control" maxlength="30" id="id_username" aria-describedby="emailHelp"> + <div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div> + </div> + <div class="mb-3"> + <label for="id_password" class="form-label">Password</label> + <input type="password" name="password" class="form-control" id="id_password"/> + </div> + <div class="mb-3 form-check"> + <input type="checkbox" class="form-check-input" id="exampleCheck1"> + <label class="form-check-label" for="exampleCheck1">Check me out</label> + </div> + + <button type="submit" class="btn btn-primary">Войти</button> + </form> + {% endif %} + </div> + </div> + </div> + </div> </body> </html> \ No newline at end of file -- GitLab