Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Django authentication backend to authenticate the users from a LDAP
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
if get_LDAP_user(username, password) is None:
return None
user = User.objects.get(username=username)
except User.DoesNotExist:
user = User(username=username)
user.is_staff = True
return user
def get_user(self, user_id):
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment