This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
authentik/passbook/sources/ldap/auth.py
2020-07-10 20:21:51 +02:00

25 lines
802 B
Python

"""passbook LDAP Authentication Backend"""
from django.contrib.auth.backends import ModelBackend
from django.http import HttpRequest
from structlog import get_logger
from passbook.sources.ldap.connector import Connector
from passbook.sources.ldap.models import LDAPSource
LOGGER = get_logger()
class LDAPBackend(ModelBackend):
"""Authenticate users against LDAP Server"""
def authenticate(self, request: HttpRequest, **kwargs):
"""Try to authenticate a user via ldap"""
if "password" not in kwargs:
return None
for source in LDAPSource.objects.filter(enabled=True):
LOGGER.debug("LDAP Auth attempt", source=source)
user = Connector(source).auth_user(**kwargs)
if user:
return user
return None