add ldap3 for authentification (use a testserver), login/logout from django, my own decorator and test-template for login

This commit is contained in:
2020-08-11 14:25:38 +00:00
parent aad82d1a51
commit 1845fe73af
7 changed files with 205 additions and 36 deletions

View File

@@ -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

View File

@@ -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

8
fet2020/accounts/urls.py Normal file
View File

@@ -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"),
]

View File

@@ -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. from .authentications import authentication
############################## # from .decorators import unauthenticated_user
import ldap3
from pprint import pprint
server_uri = 'ipa.demo1.freeipa.org' # @unauthenticated_user
search_base = 'ou=users,dc=example,dc=com' def loginPage(request):
search_filter = '(uid=rob)' """
attrs = ['*'] 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 auth_user = authentication(username, password)
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 if auth_user is not None:
# cn: Rob McBroom try:
# displayName: Rob McBroom user = User.objects.get(username=username)
# gidNumber: 99999 except User.DoesNotExist:
# givenName: Rob user = User.objects.create_user(auth_user)
# 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
# ]
######################################## 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')

View File

@@ -5,7 +5,7 @@ from django.shortcuts import render
# from django.http import HttpResponseRedirect # from django.http import HttpResponseRedirect
# from django.template import RequestContext # 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 _ # from django.utils.translation import ugettext_lazy as _
# import urllib # import urllib
from urllib.parse import urlparse from urllib.parse import urlparse
@@ -18,7 +18,6 @@ import datetime
SERVER_URL = "http://etherpad.2020.fet.at/" SERVER_URL = "http://etherpad.2020.fet.at/"
@login_required
def document(request, id=None): def document(request, id=None):
"""Create and session and display an embedded pad """Create and session and display an embedded pad
""" """

View File

@@ -36,4 +36,5 @@ urlpatterns = [
path('ckeditor/', include('ckeditor_uploader.urls')), path('ckeditor/', include('ckeditor_uploader.urls')),
path('api/', include(router.urls)), path('api/', include(router.urls)),
path('members/', include('members.urls')), path('members/', include('members.urls')),
path('accounts/', include('accounts.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

View File

@@ -0,0 +1,123 @@
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.1/css/all.css" integrity="sha384-gfdkjb5BdAXd+lj+gudLWI+BXq4IuLW5IT+brZEZsLFm++aCMlF1V92rMkPaX4PP" crossorigin="anonymous">
<style>
body,
html {
margin: 0;
padding: 0;
height: 100%;
background: #7abecc !important;
}
.user_card {
width: 350px;
margin-top: auto;
margin-bottom: auto;
background: #74cfbf;
position: relative;
display: flex;
justify-content: center;
flex-direction: column;
padding: 10px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
-webkit-box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
-moz-box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
border-radius: 5px;
}
.form_container {
margin-top: 20px;
}
#form-title{
color: #fff;
}
.login_btn {
width: 100%;
background: #33ccff !important;
color: white !important;
}
.login_btn:focus {
box-shadow: none !important;
outline: 0px !important;
}
.login_container {
padding: 0 2rem;
}
.input-group-text {
background: #f7ba5b !important;
color: white !important;
border: 0 !important;
border-radius: 0.25rem 0 0 0.25rem !important;
}
.input_user,
.input_pass:focus {
box-shadow: none !important;
outline: 0px !important;
}
#messages{
background-color: grey;
color: #fff;
padding: 10px;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="container h-100">
<div class="d-flex justify-content-center h-100">
<div class="user_card">
<div class="d-flex justify-content-center">
<h3 id="form-title">LOGIN</h3>
</div>
<div class="d-flex justify-content-center form_container">
<form method="POST" action="">
{% csrf_token %}
<div class="input-group mb-3">
<div class="input-group-append">
<span class="input-group-text"><i class="fas fa-user"></i></span>
</div>
<input type="text" name="username" placeholder="Username..." class="form-control">
</div>
<div class="input-group mb-2">
<div class="input-group-append">
<span class="input-group-text"><i class="fas fa-key"></i></span>
</div>
<input type="password" name="password" placeholder="Password..." class="form-control" >
</div>
<div class="d-flex justify-content-center mt-3 login_container">
<input class="btn login_btn" type="submit" value="Login">
</div>
</form>
</div>
{% for message in messages %}
<p id="messages">{{message}}</p>
{% endfor %}
</div>
</div>
</div>
</div>
</body>
</html>