From 888500fca8c5fc1007e07d20205d30b3b1b54198 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 25 Aug 2020 17:09:08 +0000 Subject: [PATCH 1/9] add a new app for ldap3-authentication (currently, it uses a testserver), login/logout from django, my own decorator --- fet2020/authentications/__init__.py | 0 fet2020/authentications/admin.py | 3 + fet2020/authentications/apps.py | 5 + fet2020/authentications/authentications.py | 27 ++++ fet2020/authentications/decorators.py | 11 ++ fet2020/authentications/models.py | 3 + fet2020/authentications/tests.py | 3 + fet2020/authentications/urls.py | 8 ++ fet2020/authentications/views.py | 40 ++++++ fet2020/fet2020/urls.py | 1 + fet2020/templates/authentications/login.html | 123 +++++++++++++++++++ 11 files changed, 224 insertions(+) create mode 100644 fet2020/authentications/__init__.py create mode 100644 fet2020/authentications/admin.py create mode 100644 fet2020/authentications/apps.py create mode 100644 fet2020/authentications/authentications.py create mode 100644 fet2020/authentications/decorators.py create mode 100644 fet2020/authentications/models.py create mode 100644 fet2020/authentications/tests.py create mode 100644 fet2020/authentications/urls.py create mode 100644 fet2020/authentications/views.py create mode 100644 fet2020/templates/authentications/login.html 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..cf928a75 --- /dev/null +++ b/fet2020/authentications/authentications.py @@ -0,0 +1,27 @@ +import ldap3 + + +def authentication(username, password): + # no empty passwords + if password is None or password.strip() == "": + # messages.info("username:%s Login denied for blank password", username) + return None + + # username format + new_username = 'uid={username}, cn=users, cn=accounts, dc=demo1, dc=freeipa, dc=org' + userdn = new_username.format(username=username) + + server_uri = 'ipa.demo1.freeipa.org' + server = ldap3.Server(server_uri, get_info=ldap3.ALL) + + try: + ldap3.Connection( + server, + userdn, + password, + auto_bind=True, + ) + except ldap3.core.exceptions.LDAPBindError: + username = None + + return username diff --git a/fet2020/authentications/decorators.py b/fet2020/authentications/decorators.py new file mode 100644 index 00000000..db0e0b60 --- /dev/null +++ b/fet2020/authentications/decorators.py @@ -0,0 +1,11 @@ +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 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..e9907280 --- /dev/null +++ b/fet2020/authentications/views.py @@ -0,0 +1,40 @@ +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 + + +# @unauthenticated_user +def loginPage(request): + """ + if request.user.is_authenticated: + return redirect('home') + else: + """ + 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=username) + except User.DoesNotExist: + user = User.objects.create_user(auth_user) + + login(request, user) + return redirect('home') + else: + messages.info(request, 'username OR password is incorrect') + + context = {} + return render(request, 'authentications/login.html', context) + + +def logoutUser(request): + logout(request) + return redirect('home') diff --git a/fet2020/fet2020/urls.py b/fet2020/fet2020/urls.py index fb8164f1..41a166a2 100644 --- a/fet2020/fet2020/urls.py +++ b/fet2020/fet2020/urls.py @@ -30,6 +30,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 @@ + + + + + Login + + + + + + + + + +
+
+
+
+ + +

LOGIN

+
+
+
+ {% csrf_token %} +
+
+ +
+ + +
+ +
+
+ +
+ + +
+ + +
+ +
+ + {% for message in messages %} +

{{message}}

+ {% endfor %} + +
+
+
+ + + + \ No newline at end of file -- 2.49.1 From dfdd9625fdaab970138317a72407b3939ed57fb4 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 25 Aug 2020 17:09:08 +0000 Subject: [PATCH 2/9] add a new app for ldap3-authentication (currently, it uses a testserver), login/logout from django, my own decorator --- fet2020/authentications/__init__.py | 0 fet2020/authentications/admin.py | 3 + fet2020/authentications/apps.py | 5 + fet2020/authentications/authentications.py | 27 ++++ fet2020/authentications/decorators.py | 11 ++ fet2020/authentications/models.py | 3 + fet2020/authentications/tests.py | 3 + fet2020/authentications/urls.py | 8 ++ fet2020/authentications/views.py | 40 ++++++ fet2020/fet2020/urls.py | 1 + fet2020/templates/authentications/login.html | 123 +++++++++++++++++++ 11 files changed, 224 insertions(+) create mode 100644 fet2020/authentications/__init__.py create mode 100644 fet2020/authentications/admin.py create mode 100644 fet2020/authentications/apps.py create mode 100644 fet2020/authentications/authentications.py create mode 100644 fet2020/authentications/decorators.py create mode 100644 fet2020/authentications/models.py create mode 100644 fet2020/authentications/tests.py create mode 100644 fet2020/authentications/urls.py create mode 100644 fet2020/authentications/views.py create mode 100644 fet2020/templates/authentications/login.html 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..cf928a75 --- /dev/null +++ b/fet2020/authentications/authentications.py @@ -0,0 +1,27 @@ +import ldap3 + + +def authentication(username, password): + # no empty passwords + if password is None or password.strip() == "": + # messages.info("username:%s Login denied for blank password", username) + return None + + # username format + new_username = 'uid={username}, cn=users, cn=accounts, dc=demo1, dc=freeipa, dc=org' + userdn = new_username.format(username=username) + + server_uri = 'ipa.demo1.freeipa.org' + server = ldap3.Server(server_uri, get_info=ldap3.ALL) + + try: + ldap3.Connection( + server, + userdn, + password, + auto_bind=True, + ) + except ldap3.core.exceptions.LDAPBindError: + username = None + + return username diff --git a/fet2020/authentications/decorators.py b/fet2020/authentications/decorators.py new file mode 100644 index 00000000..db0e0b60 --- /dev/null +++ b/fet2020/authentications/decorators.py @@ -0,0 +1,11 @@ +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 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..e9907280 --- /dev/null +++ b/fet2020/authentications/views.py @@ -0,0 +1,40 @@ +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 + + +# @unauthenticated_user +def loginPage(request): + """ + if request.user.is_authenticated: + return redirect('home') + else: + """ + 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=username) + except User.DoesNotExist: + user = User.objects.create_user(auth_user) + + login(request, user) + return redirect('home') + else: + messages.info(request, 'username OR password is incorrect') + + context = {} + return render(request, 'authentications/login.html', context) + + +def logoutUser(request): + logout(request) + return redirect('home') diff --git a/fet2020/fet2020/urls.py b/fet2020/fet2020/urls.py index fb8164f1..41a166a2 100644 --- a/fet2020/fet2020/urls.py +++ b/fet2020/fet2020/urls.py @@ -30,6 +30,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 @@ + + + + + Login + + + + + + + + + +
+
+
+
+ + +

LOGIN

+
+
+
+ {% csrf_token %} +
+
+ +
+ + +
+ +
+
+ +
+ + +
+ + +
+ +
+ + {% for message in messages %} +

{{message}}

+ {% endfor %} + +
+
+
+ + + + \ No newline at end of file -- 2.49.1 From 8fe9d7cfc043267f051b43066b08130fdc3ccc14 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sun, 30 Aug 2020 22:06:36 +0000 Subject: [PATCH 3/9] add login/logout to home, add 'only authenticated users are allowed to go to admin view' --- fet2020/authentications/decorators.py | 10 ++++++++++ fet2020/authentications/views.py | 12 ++++-------- fet2020/fet2020/settings.py | 2 +- fet2020/fet2020/urls.py | 3 ++- fet2020/templates/layout.html | 20 ++++++++++++-------- 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/fet2020/authentications/decorators.py b/fet2020/authentications/decorators.py index db0e0b60..d3af1cae 100644 --- a/fet2020/authentications/decorators.py +++ b/fet2020/authentications/decorators.py @@ -9,3 +9,13 @@ def unauthenticated_user(view_func): 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/views.py b/fet2020/authentications/views.py index e9907280..a861ee8c 100644 --- a/fet2020/authentications/views.py +++ b/fet2020/authentications/views.py @@ -4,16 +4,11 @@ from django.contrib import messages from django.contrib.auth.models import User from .authentications import authentication -# from .decorators import unauthenticated_user +from .decorators import unauthenticated_user, authenticated_user -# @unauthenticated_user +@unauthenticated_user def loginPage(request): - """ - if request.user.is_authenticated: - return redirect('home') - else: - """ if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') @@ -29,12 +24,13 @@ def loginPage(request): login(request, user) return redirect('home') else: - messages.info(request, 'username OR password is incorrect') + 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 41a166a2..6a515d5f 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) @@ -29,7 +30,7 @@ router.register(r'members', MemberViewSet) urlpatterns = [ path('posts/', include('posts.urls')), path('admin/doc/', include('django.contrib.admindocs.urls')), - path('admin/', admin.site.urls), + path('admin/', authenticated_user(admin.site.urls)), path('auth/', include('authentications.urls')), path('', views.index, name='home'), path('index.html', views.index, name='home'), diff --git a/fet2020/templates/layout.html b/fet2020/templates/layout.html index 66493f63..8ffbc2e1 100644 --- a/fet2020/templates/layout.html +++ b/fet2020/templates/layout.html @@ -6,9 +6,9 @@ - FET DjangoLayout + FET - {% csrf_token %} + {% csrf_token %} {% block header %} {% endblock %} @@ -20,11 +20,16 @@
@@ -36,7 +41,6 @@
-- 2.49.1 From f50d6680e9f24cc660c9afc4183f7d9c01c7c8bb Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 2 Sep 2020 23:41:05 +0000 Subject: [PATCH 4/9] ldap3-authentication works now with fet server --- fet2020/authentications/authentications.py | 30 ++++++++++++++-------- fet2020/authentications/views.py | 4 +-- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/fet2020/authentications/authentications.py b/fet2020/authentications/authentications.py index cf928a75..4d01d570 100644 --- a/fet2020/authentications/authentications.py +++ b/fet2020/authentications/authentications.py @@ -1,27 +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() == "": - # messages.info("username:%s Login denied for blank password", username) return None # username format - new_username = 'uid={username}, cn=users, cn=accounts, dc=demo1, dc=freeipa, dc=org' + new_username = 'uid={username},ou=user,dc=fet,dc=htu,dc=tuwien,dc=ac,dc=at' userdn = new_username.format(username=username) - server_uri = 'ipa.demo1.freeipa.org' - server = ldap3.Server(server_uri, get_info=ldap3.ALL) + server_uri = 'ldap://gagarin.fet.htu.tuwien.ac.at' + server = ldap3.Server(server_uri, port=389, use_ssl=True) + + has_user = False try: - ldap3.Connection( - server, - userdn, - password, - auto_bind=True, - ) - except ldap3.core.exceptions.LDAPBindError: + 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/views.py b/fet2020/authentications/views.py index a861ee8c..2e6c489c 100644 --- a/fet2020/authentications/views.py +++ b/fet2020/authentications/views.py @@ -17,9 +17,9 @@ def loginPage(request): if auth_user is not None: try: - user = User.objects.get(username=username) + user = User.objects.get(username=auth_user.lower()) except User.DoesNotExist: - user = User.objects.create_user(auth_user) + user = User.objects.create_user(auth_user.lower()) login(request, user) return redirect('home') -- 2.49.1 From 9f1aa7670801794661613ac9cb10bfd4fc628a48 Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 3 Sep 2020 00:04:04 +0000 Subject: [PATCH 5/9] fix permission --- fet2020/fet2020/urls.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fet2020/fet2020/urls.py b/fet2020/fet2020/urls.py index 6a515d5f..d0fbda3e 100644 --- a/fet2020/fet2020/urls.py +++ b/fet2020/fet2020/urls.py @@ -21,7 +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 +# from authentications.decorators import authenticated_user router = routers.DefaultRouter() router.register(r'posts', PostViewSet) @@ -30,7 +30,7 @@ router.register(r'members', MemberViewSet) urlpatterns = [ path('posts/', include('posts.urls')), path('admin/doc/', include('django.contrib.admindocs.urls')), - path('admin/', authenticated_user(admin.site.urls)), + path('admin/', admin.site.urls), path('auth/', include('authentications.urls')), path('', views.index, name='home'), path('index.html', views.index, name='home'), -- 2.49.1 From ffa248ce03d0e121e9947993a5b77cba5eabd6c7 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 25 Aug 2020 17:09:08 +0000 Subject: [PATCH 6/9] add a new app for ldap3-authentication (currently, it uses a testserver), login/logout from django, my own decorator --- fet2020/authentications/__init__.py | 0 fet2020/authentications/admin.py | 3 + fet2020/authentications/apps.py | 5 + fet2020/authentications/authentications.py | 27 ++++ fet2020/authentications/decorators.py | 11 ++ fet2020/authentications/models.py | 3 + fet2020/authentications/tests.py | 3 + fet2020/authentications/urls.py | 8 ++ fet2020/authentications/views.py | 40 ++++++ fet2020/fet2020/urls.py | 1 + fet2020/templates/authentications/login.html | 123 +++++++++++++++++++ 11 files changed, 224 insertions(+) create mode 100644 fet2020/authentications/__init__.py create mode 100644 fet2020/authentications/admin.py create mode 100644 fet2020/authentications/apps.py create mode 100644 fet2020/authentications/authentications.py create mode 100644 fet2020/authentications/decorators.py create mode 100644 fet2020/authentications/models.py create mode 100644 fet2020/authentications/tests.py create mode 100644 fet2020/authentications/urls.py create mode 100644 fet2020/authentications/views.py create mode 100644 fet2020/templates/authentications/login.html 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..cf928a75 --- /dev/null +++ b/fet2020/authentications/authentications.py @@ -0,0 +1,27 @@ +import ldap3 + + +def authentication(username, password): + # no empty passwords + if password is None or password.strip() == "": + # messages.info("username:%s Login denied for blank password", username) + return None + + # username format + new_username = 'uid={username}, cn=users, cn=accounts, dc=demo1, dc=freeipa, dc=org' + userdn = new_username.format(username=username) + + server_uri = 'ipa.demo1.freeipa.org' + server = ldap3.Server(server_uri, get_info=ldap3.ALL) + + try: + ldap3.Connection( + server, + userdn, + password, + auto_bind=True, + ) + except ldap3.core.exceptions.LDAPBindError: + username = None + + return username diff --git a/fet2020/authentications/decorators.py b/fet2020/authentications/decorators.py new file mode 100644 index 00000000..db0e0b60 --- /dev/null +++ b/fet2020/authentications/decorators.py @@ -0,0 +1,11 @@ +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 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..e9907280 --- /dev/null +++ b/fet2020/authentications/views.py @@ -0,0 +1,40 @@ +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 + + +# @unauthenticated_user +def loginPage(request): + """ + if request.user.is_authenticated: + return redirect('home') + else: + """ + 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=username) + except User.DoesNotExist: + user = User.objects.create_user(auth_user) + + login(request, user) + return redirect('home') + else: + messages.info(request, 'username OR password is incorrect') + + context = {} + return render(request, 'authentications/login.html', context) + + +def logoutUser(request): + logout(request) + return redirect('home') diff --git a/fet2020/fet2020/urls.py b/fet2020/fet2020/urls.py index fb8164f1..41a166a2 100644 --- a/fet2020/fet2020/urls.py +++ b/fet2020/fet2020/urls.py @@ -30,6 +30,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 @@ + + + + + Login + + + + + + + + + +
+
+
+
+ + +

LOGIN

+
+
+
+ {% csrf_token %} +
+
+ +
+ + +
+ +
+
+ +
+ + +
+ + +
+ +
+ + {% for message in messages %} +

{{message}}

+ {% endfor %} + +
+
+
+
+ + + \ No newline at end of file -- 2.49.1 From be572eedebb08a2407e188db5404a60577ef988a Mon Sep 17 00:00:00 2001 From: Patrick Date: Sun, 30 Aug 2020 22:06:36 +0000 Subject: [PATCH 7/9] add login/logout to home, add 'only authenticated users are allowed to go to admin view' --- fet2020/authentications/decorators.py | 10 ++++++++++ fet2020/authentications/views.py | 12 ++++-------- fet2020/fet2020/settings.py | 2 +- fet2020/fet2020/urls.py | 3 ++- fet2020/templates/layout.html | 20 ++++++++++++-------- 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/fet2020/authentications/decorators.py b/fet2020/authentications/decorators.py index db0e0b60..d3af1cae 100644 --- a/fet2020/authentications/decorators.py +++ b/fet2020/authentications/decorators.py @@ -9,3 +9,13 @@ def unauthenticated_user(view_func): 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/views.py b/fet2020/authentications/views.py index e9907280..a861ee8c 100644 --- a/fet2020/authentications/views.py +++ b/fet2020/authentications/views.py @@ -4,16 +4,11 @@ from django.contrib import messages from django.contrib.auth.models import User from .authentications import authentication -# from .decorators import unauthenticated_user +from .decorators import unauthenticated_user, authenticated_user -# @unauthenticated_user +@unauthenticated_user def loginPage(request): - """ - if request.user.is_authenticated: - return redirect('home') - else: - """ if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') @@ -29,12 +24,13 @@ def loginPage(request): login(request, user) return redirect('home') else: - messages.info(request, 'username OR password is incorrect') + 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 41a166a2..6a515d5f 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) @@ -29,7 +30,7 @@ router.register(r'members', MemberViewSet) urlpatterns = [ path('posts/', include('posts.urls')), path('admin/doc/', include('django.contrib.admindocs.urls')), - path('admin/', admin.site.urls), + path('admin/', authenticated_user(admin.site.urls)), path('auth/', include('authentications.urls')), path('', views.index, name='home'), path('index.html', views.index, name='home'), diff --git a/fet2020/templates/layout.html b/fet2020/templates/layout.html index 66493f63..8ffbc2e1 100644 --- a/fet2020/templates/layout.html +++ b/fet2020/templates/layout.html @@ -6,9 +6,9 @@ - FET DjangoLayout + FET - {% csrf_token %} + {% csrf_token %} {% block header %} {% endblock %} @@ -20,11 +20,16 @@
@@ -36,7 +41,6 @@
-- 2.49.1 From ee2c846a41709d02303902a7e8352506b4c8c563 Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 2 Sep 2020 23:41:05 +0000 Subject: [PATCH 8/9] ldap3-authentication works now with fet server --- fet2020/authentications/authentications.py | 30 ++++++++++++++-------- fet2020/authentications/views.py | 4 +-- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/fet2020/authentications/authentications.py b/fet2020/authentications/authentications.py index cf928a75..4d01d570 100644 --- a/fet2020/authentications/authentications.py +++ b/fet2020/authentications/authentications.py @@ -1,27 +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() == "": - # messages.info("username:%s Login denied for blank password", username) return None # username format - new_username = 'uid={username}, cn=users, cn=accounts, dc=demo1, dc=freeipa, dc=org' + new_username = 'uid={username},ou=user,dc=fet,dc=htu,dc=tuwien,dc=ac,dc=at' userdn = new_username.format(username=username) - server_uri = 'ipa.demo1.freeipa.org' - server = ldap3.Server(server_uri, get_info=ldap3.ALL) + server_uri = 'ldap://gagarin.fet.htu.tuwien.ac.at' + server = ldap3.Server(server_uri, port=389, use_ssl=True) + + has_user = False try: - ldap3.Connection( - server, - userdn, - password, - auto_bind=True, - ) - except ldap3.core.exceptions.LDAPBindError: + 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/views.py b/fet2020/authentications/views.py index a861ee8c..2e6c489c 100644 --- a/fet2020/authentications/views.py +++ b/fet2020/authentications/views.py @@ -17,9 +17,9 @@ def loginPage(request): if auth_user is not None: try: - user = User.objects.get(username=username) + user = User.objects.get(username=auth_user.lower()) except User.DoesNotExist: - user = User.objects.create_user(auth_user) + user = User.objects.create_user(auth_user.lower()) login(request, user) return redirect('home') -- 2.49.1 From b6bb0670684d4c51339368ebe282084c1b529f09 Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 3 Sep 2020 00:04:04 +0000 Subject: [PATCH 9/9] fix permission --- fet2020/fet2020/urls.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fet2020/fet2020/urls.py b/fet2020/fet2020/urls.py index 6a515d5f..d0fbda3e 100644 --- a/fet2020/fet2020/urls.py +++ b/fet2020/fet2020/urls.py @@ -21,7 +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 +# from authentications.decorators import authenticated_user router = routers.DefaultRouter() router.register(r'posts', PostViewSet) @@ -30,7 +30,7 @@ router.register(r'members', MemberViewSet) urlpatterns = [ path('posts/', include('posts.urls')), path('admin/doc/', include('django.contrib.admindocs.urls')), - path('admin/', authenticated_user(admin.site.urls)), + path('admin/', admin.site.urls), path('auth/', include('authentications.urls')), path('', views.index, name='home'), path('index.html', views.index, name='home'), -- 2.49.1