From 757f7e19d6f5cfc9deb85bd66f2f042f5d8986df Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 3 Aug 2020 23:43:33 +0000 Subject: [PATCH 1/3] small changes --- fet2020/documents/views.py | 9 +++++---- fet2020/fet2020/settings.py | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/fet2020/documents/views.py b/fet2020/documents/views.py index 00a71463..c9efabe6 100644 --- a/fet2020/documents/views.py +++ b/fet2020/documents/views.py @@ -30,7 +30,7 @@ def document(request, id=None): author = epc.createAuthorIfNotExistsFor( name=str(request.user), authorMapper=str(request.user) - )['authorID'] + ) # Create the session on the etherpad-lite side expires = datetime.datetime.utcnow() + datetime.timedelta(hours=3) @@ -39,7 +39,7 @@ def document(request, id=None): try: result = epclient.createSession( groupID=str(group['groupID']), - authorID=str(author), + authorID=str(author['authorID']), validUntil=str(int(expires.timestamp())) ) except Exception as e: @@ -71,8 +71,9 @@ def document(request, id=None): # Delete the existing session first if ('padSessionID' in request.COOKIES): - epclient.deleteSession(request.COOKIES['sessionID']) - response.delete_cookie('sessionID', server.hostname) + print(request.COOKIES['sessionid']) + # TODO: epclient.deleteSession(request.COOKIES['sessionid']) + response.delete_cookie('sessionid', server.hostname) response.delete_cookie('padSessionID') # Set the new session cookie for both the server and the local site diff --git a/fet2020/fet2020/settings.py b/fet2020/fet2020/settings.py index debce2a8..97ca8e27 100644 --- a/fet2020/fet2020/settings.py +++ b/fet2020/fet2020/settings.py @@ -50,7 +50,6 @@ DATA_UPLOAD_MAX_MEMORY_SIZE = 1024 * 1024 * 1024 CKEDITOR_UPLOAD_PATH = 'upload' INSTALLED_APPS = [ - 'documents.apps.DocumentsConfig', 'django.contrib.admin', 'django.contrib.admindocs', 'django.contrib.auth', @@ -66,6 +65,7 @@ INSTALLED_APPS = [ 'django_static_jquery_ui', 'posts.apps.PostsConfig', 'members.apps.MembersConfig', + 'documents.apps.DocumentsConfig', ] MIDDLEWARE = [ @@ -114,7 +114,7 @@ DATABASES = { } AUTHENTICATION_BACKENDS = [ - 'django.contrib.auth.backends.RemoteUserBackend' + 'django.contrib.auth.backends.RemoteUserBackend', ] # Password validation From aad82d1a51a20c59adf644896338958f9a821a62 Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 3 Aug 2020 23:45:27 +0000 Subject: [PATCH 2/3] add accounts for login/logout --- fet2020/accounts/__init__.py | 0 fet2020/accounts/admin.py | 3 +++ fet2020/accounts/apps.py | 5 +++++ fet2020/accounts/models.py | 3 +++ fet2020/accounts/tests.py | 3 +++ fet2020/accounts/views.py | 40 ++++++++++++++++++++++++++++++++++++ fet2020/fet2020/settings.py | 1 + 7 files changed, 55 insertions(+) create mode 100644 fet2020/accounts/__init__.py create mode 100644 fet2020/accounts/admin.py create mode 100644 fet2020/accounts/apps.py create mode 100644 fet2020/accounts/models.py create mode 100644 fet2020/accounts/tests.py create mode 100644 fet2020/accounts/views.py diff --git a/fet2020/accounts/__init__.py b/fet2020/accounts/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/fet2020/accounts/admin.py b/fet2020/accounts/admin.py new file mode 100644 index 00000000..4185d360 --- /dev/null +++ b/fet2020/accounts/admin.py @@ -0,0 +1,3 @@ +# from django.contrib import admin + +# Register your models here. diff --git a/fet2020/accounts/apps.py b/fet2020/accounts/apps.py new file mode 100644 index 00000000..9b3fc5a4 --- /dev/null +++ b/fet2020/accounts/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class AccountsConfig(AppConfig): + name = 'accounts' diff --git a/fet2020/accounts/models.py b/fet2020/accounts/models.py new file mode 100644 index 00000000..0b4331b3 --- /dev/null +++ b/fet2020/accounts/models.py @@ -0,0 +1,3 @@ +# from django.db import models + +# Create your models here. diff --git a/fet2020/accounts/tests.py b/fet2020/accounts/tests.py new file mode 100644 index 00000000..a79ca8be --- /dev/null +++ b/fet2020/accounts/tests.py @@ -0,0 +1,3 @@ +# from django.test import TestCase + +# Create your tests here. diff --git a/fet2020/accounts/views.py b/fet2020/accounts/views.py new file mode 100644 index 00000000..bd388ef9 --- /dev/null +++ b/fet2020/accounts/views.py @@ -0,0 +1,40 @@ +# from django.shortcuts import render + +# Create your views here. +############################## +import ldap3 +from pprint import pprint + + +server_uri = 'ipa.demo1.freeipa.org' +search_base = 'ou=users,dc=example,dc=com' +search_filter = '(uid=rob)' +attrs = ['*'] + +# Using ldap3 +server = ldap3.Server(server_uri, get_info='ALL') +with ldap3.Connection(server, auto_bind=True) as conn: + conn.search(search_base, search_filter, attributes=attrs) + pprint(conn.entries) + pprint(server.info) + +# [DN: uid=rob,ou=users,dc=example,dc=com +# cn: Rob McBroom +# displayName: Rob McBroom +# gidNumber: 99999 +# givenName: Rob +# homeDirectory: /home/rob +# homePhone: 800-555-1212 +# host: * +# loginShell: /bin/zsh +# mail: rob@example.com +# objectClass: top +# inetOrgPerson +# hostObject +# posixAccount +# sn: McBroom +# uid: rob +# uidNumber: 99999 +# ] + +######################################## diff --git a/fet2020/fet2020/settings.py b/fet2020/fet2020/settings.py index 97ca8e27..5d918512 100644 --- a/fet2020/fet2020/settings.py +++ b/fet2020/fet2020/settings.py @@ -63,6 +63,7 @@ INSTALLED_APPS = [ 'rest_framework', 'django_filters', 'django_static_jquery_ui', + 'accounts.apps.AccountsConfig', 'posts.apps.PostsConfig', 'members.apps.MembersConfig', 'documents.apps.DocumentsConfig', From 1845fe73afc7c42558eb1c1834d4a6247cc01fac Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 11 Aug 2020 14:25:38 +0000 Subject: [PATCH 3/3] add ldap3 for authentification (use a testserver), login/logout from django, my own decorator and test-template for login --- fet2020/accounts/authentications.py | 27 ++++++ fet2020/accounts/decorators.py | 11 +++ fet2020/accounts/urls.py | 8 ++ fet2020/accounts/views.py | 68 +++++++------- fet2020/documents/views.py | 3 +- fet2020/fet2020/urls.py | 1 + fet2020/templates/accounts/login.html | 123 ++++++++++++++++++++++++++ 7 files changed, 205 insertions(+), 36 deletions(-) create mode 100644 fet2020/accounts/authentications.py create mode 100644 fet2020/accounts/decorators.py create mode 100644 fet2020/accounts/urls.py create mode 100644 fet2020/templates/accounts/login.html diff --git a/fet2020/accounts/authentications.py b/fet2020/accounts/authentications.py new file mode 100644 index 00000000..cf928a75 --- /dev/null +++ b/fet2020/accounts/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/accounts/decorators.py b/fet2020/accounts/decorators.py new file mode 100644 index 00000000..db0e0b60 --- /dev/null +++ b/fet2020/accounts/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/accounts/urls.py b/fet2020/accounts/urls.py new file mode 100644 index 00000000..7c084c15 --- /dev/null +++ b/fet2020/accounts/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/accounts/views.py b/fet2020/accounts/views.py index bd388ef9..7ec72d10 100644 --- a/fet2020/accounts/views.py +++ b/fet2020/accounts/views.py @@ -1,40 +1,40 @@ -# from django.shortcuts import render +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 -# Create your views here. -############################## -import ldap3 -from pprint import pprint +from .authentications import authentication +# from .decorators import unauthenticated_user -server_uri = 'ipa.demo1.freeipa.org' -search_base = 'ou=users,dc=example,dc=com' -search_filter = '(uid=rob)' -attrs = ['*'] +# @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') -# Using ldap3 -server = ldap3.Server(server_uri, get_info='ALL') -with ldap3.Connection(server, auto_bind=True) as conn: - conn.search(search_base, search_filter, attributes=attrs) - pprint(conn.entries) - pprint(server.info) + auth_user = authentication(username, password) -# [DN: uid=rob,ou=users,dc=example,dc=com -# cn: Rob McBroom -# displayName: Rob McBroom -# gidNumber: 99999 -# givenName: Rob -# homeDirectory: /home/rob -# homePhone: 800-555-1212 -# host: * -# loginShell: /bin/zsh -# mail: rob@example.com -# objectClass: top -# inetOrgPerson -# hostObject -# posixAccount -# sn: McBroom -# uid: rob -# uidNumber: 99999 -# ] + 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, 'accounts/login.html', context) + + +def logoutUser(request): + logout(request) + return redirect('home') diff --git a/fet2020/documents/views.py b/fet2020/documents/views.py index c9efabe6..1cc9d7cc 100644 --- a/fet2020/documents/views.py +++ b/fet2020/documents/views.py @@ -5,7 +5,7 @@ from django.shortcuts import render # from django.http import HttpResponseRedirect # from django.template import RequestContext -from django.contrib.auth.decorators import login_required +# from django.contrib.auth.decorators import login_required # from django.utils.translation import ugettext_lazy as _ # import urllib from urllib.parse import urlparse @@ -18,7 +18,6 @@ import datetime SERVER_URL = "http://etherpad.2020.fet.at/" -@login_required def document(request, id=None): """Create and session and display an embedded pad """ diff --git a/fet2020/fet2020/urls.py b/fet2020/fet2020/urls.py index 00ea79d6..2b63f7d9 100644 --- a/fet2020/fet2020/urls.py +++ b/fet2020/fet2020/urls.py @@ -36,4 +36,5 @@ urlpatterns = [ path('ckeditor/', include('ckeditor_uploader.urls')), path('api/', include(router.urls)), path('members/', include('members.urls')), + path('accounts/', include('accounts.urls')), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/fet2020/templates/accounts/login.html b/fet2020/templates/accounts/login.html new file mode 100644 index 00000000..85620a87 --- /dev/null +++ b/fet2020/templates/accounts/login.html @@ -0,0 +1,123 @@ + + + + + Login + + + + + + + + + +
+
+
+
+ + +

LOGIN

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

{{message}}

+ {% endfor %} + +
+
+
+ + + + \ No newline at end of file