From 888500fca8c5fc1007e07d20205d30b3b1b54198 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 25 Aug 2020 17:09:08 +0000 Subject: [PATCH] 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