diff --git a/fet2020/authentications/__init__.py b/fet2020/authentications/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/fet2020/authentications/admin.py b/fet2020/authentications/admin.py new file mode 100644 index 00000000..4185d360 --- /dev/null +++ b/fet2020/authentications/admin.py @@ -0,0 +1,3 @@ +# from django.contrib import admin + +# Register your models here. diff --git a/fet2020/authentications/apps.py b/fet2020/authentications/apps.py new file mode 100644 index 00000000..cb10b004 --- /dev/null +++ b/fet2020/authentications/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class AuthenticationsConfig(AppConfig): + name = 'authentications' diff --git a/fet2020/authentications/authentications.py b/fet2020/authentications/authentications.py new file mode 100644 index 00000000..4d01d570 --- /dev/null +++ b/fet2020/authentications/authentications.py @@ -0,0 +1,35 @@ +import ldap3 +import logging +from ldap3.core.exceptions import LDAPBindError + +logger = logging.getLogger(__name__) + + +def authentication(username, password): + # no empty passwords + 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://gagarin.fet.htu.tuwien.ac.at' + server = ldap3.Server(server_uri, port=389, use_ssl=True) + + has_user = False + + 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 + except LDAPBindError as e: + logger.info('Username does not exist. Error: {}'.format(e)) + username = None + + if not has_user: + username = None + + return username diff --git a/fet2020/authentications/decorators.py b/fet2020/authentications/decorators.py new file mode 100644 index 00000000..d3af1cae --- /dev/null +++ b/fet2020/authentications/decorators.py @@ -0,0 +1,21 @@ +from django.shortcuts import redirect + + +def unauthenticated_user(view_func): + def wrapper_func(request, *args, **kwargs): + if request.user.is_authenticated: + return redirect('home') + else: + return view_func(request, *args, **kwargs) + + return wrapper_func + + +def authenticated_user(view_func): + def wrapper_func(request, *args, **kwargs): + if request.user.is_authenticated: + return view_func(request, *args, **kwargs) + else: + return redirect('home') + + return wrapper_func diff --git a/fet2020/authentications/models.py b/fet2020/authentications/models.py new file mode 100644 index 00000000..0b4331b3 --- /dev/null +++ b/fet2020/authentications/models.py @@ -0,0 +1,3 @@ +# from django.db import models + +# Create your models here. diff --git a/fet2020/authentications/tests.py b/fet2020/authentications/tests.py new file mode 100644 index 00000000..a79ca8be --- /dev/null +++ b/fet2020/authentications/tests.py @@ -0,0 +1,3 @@ +# from django.test import TestCase + +# Create your tests here. diff --git a/fet2020/authentications/urls.py b/fet2020/authentications/urls.py new file mode 100644 index 00000000..7c084c15 --- /dev/null +++ b/fet2020/authentications/urls.py @@ -0,0 +1,8 @@ +from django.urls import path +from . import views + + +urlpatterns = [ + path('login/', views.loginPage, name="login"), + path('logout/', views.logoutUser, name="logout"), +] diff --git a/fet2020/authentications/views.py b/fet2020/authentications/views.py new file mode 100644 index 00000000..2e6c489c --- /dev/null +++ b/fet2020/authentications/views.py @@ -0,0 +1,36 @@ +from django.shortcuts import render, redirect +from django.contrib.auth import login, logout +from django.contrib import messages +from django.contrib.auth.models import User + +from .authentications import authentication +from .decorators import unauthenticated_user, authenticated_user + + +@unauthenticated_user +def loginPage(request): + if request.method == 'POST': + username = request.POST.get('username') + password = request.POST.get('password') + + auth_user = authentication(username, password) + + if auth_user is not None: + try: + user = User.objects.get(username=auth_user.lower()) + except User.DoesNotExist: + user = User.objects.create_user(auth_user.lower()) + + login(request, user) + return redirect('home') + else: + messages.info(request, 'username or password is incorrect') + + context = {} + return render(request, 'authentications/login.html', context) + + +@authenticated_user +def logoutUser(request): + logout(request) + return redirect('home') diff --git a/fet2020/fet2020/settings.py b/fet2020/fet2020/settings.py index 4fb52c2f..cc773ddf 100644 --- a/fet2020/fet2020/settings.py +++ b/fet2020/fet2020/settings.py @@ -115,7 +115,7 @@ DATABASES = { } AUTHENTICATION_BACKENDS = [ - 'django.contrib.auth.backends.RemoteUserBackend', + # 'django.contrib.auth.backends.RemoteUserBackend', 'django.contrib.auth.backends.ModelBackend', ] diff --git a/fet2020/fet2020/urls.py b/fet2020/fet2020/urls.py index fb8164f1..d0fbda3e 100644 --- a/fet2020/fet2020/urls.py +++ b/fet2020/fet2020/urls.py @@ -21,6 +21,7 @@ from . import views from posts.views import PostViewSet from members.views import MemberViewSet from rest_framework import routers +# from authentications.decorators import authenticated_user router = routers.DefaultRouter() router.register(r'posts', PostViewSet) @@ -30,6 +31,7 @@ urlpatterns = [ path('posts/', include('posts.urls')), path('admin/doc/', include('django.contrib.admindocs.urls')), path('admin/', admin.site.urls), + path('auth/', include('authentications.urls')), path('', views.index, name='home'), path('index.html', views.index, name='home'), path('ckeditor/', include('ckeditor_uploader.urls')), diff --git a/fet2020/templates/authentications/login.html b/fet2020/templates/authentications/login.html new file mode 100644 index 00000000..85620a87 --- /dev/null +++ b/fet2020/templates/authentications/login.html @@ -0,0 +1,123 @@ + + + +
+{{message}}
+ {% endfor %} + +