This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Application definition | |
MIDDLEWARE = [ | |
'django.middleware.security.SecurityMiddleware', | |
'django.contrib.sessions.middleware.SessionMiddleware', | |
'django.middleware.common.CommonMiddleware', | |
'django.middleware.csrf.CsrfViewMiddleware', | |
'django.contrib.auth.middleware.AuthenticationMiddleware', | |
# Add the new middleware just after the default AuthenticationMiddleware that manages sessions and cookies | |
'AutomaticDjangoAuthentication.authentication_middleware.AutomaticUserLoginMiddleware', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ldap3 import Server, Connection, ALL | |
LDAP_URL = 'ldap.forumsys.com' | |
# Check user authentication in the LDAP and return his information | |
def get_LDAP_user(username, password): | |
try: | |
server = Server(LDAP_URL, get_info=ALL) | |
connection = Connection(server, | |
'uid={username},dc=example,dc=com'.format( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ... | |
class AuthenticationBackend(ModelBackend): | |
def authenticate(self, request, username=None, password=None, **kwargs): | |
# Get credentials from the query strings | |
username = request.GET.get('username') | |
password = request.GET.get('password') | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
# Application definition | |
AUTHENTICATION_BACKENDS = [ | |
'AutomaticDjangoAuthentication.authentication_backend.AuthenticationBackend', | |
] | |
INSTALLED_APPS = [ | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.auth.backends import ModelBackend | |
from django.contrib.auth.models import User | |
from .services.ldap import get_LDAP_user | |
class AuthenticationBackend(ModelBackend): | |
def authenticate(self, request, username=None, password=None, **kwargs): | |
# Get the user information from the LDAP if he can be authenticated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib import auth | |
from django.contrib.auth.middleware import MiddlewareMixin | |
from django.http import HttpResponseForbidden | |
class AutomaticUserLoginMiddleware(MiddlewareMixin): | |
def process_view(self, request, view_func, view_args, view_kwargs): | |
if not AutomaticUserLoginMiddleware._is_user_authenticated(request): | |
user = auth.authenticate(request) |
NewerOlder