authentik.sources.ldap.signals
authentik ldap source signals
1"""authentik ldap source signals""" 2 3from typing import Any 4 5from django.dispatch import receiver 6from django.utils.translation import gettext_lazy as _ 7from rest_framework.serializers import ValidationError 8from structlog.stdlib import get_logger 9 10from authentik.core.models import User 11from authentik.core.signals import password_changed 12from authentik.events.models import Event, EventAction 13from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER 14from authentik.sources.ldap.models import LDAPSource 15from authentik.sources.ldap.password import LDAPPasswordChanger 16from authentik.stages.prompt.signals import password_validate 17 18LOGGER = get_logger() 19 20 21@receiver(password_validate) 22def ldap_password_validate(sender, password: str, plan_context: dict[str, Any], **__): 23 """if there's an LDAP Source with enabled password sync, check the password""" 24 sources = LDAPSource.objects.filter(sync_users_password=True, enabled=True) 25 if not sources.exists(): 26 return 27 source = sources.first() 28 user = plan_context.get(PLAN_CONTEXT_PENDING_USER, None) 29 if user and not LDAPPasswordChanger.should_check_user(user): 30 return 31 changer = LDAPPasswordChanger(source) 32 if changer.check_ad_password_complexity_enabled(): 33 passing = changer.ad_password_complexity(password, user) 34 if not passing: 35 raise ValidationError(_("Password does not match Active Directory Complexity.")) 36 37 38@receiver(password_changed) 39def ldap_sync_password(sender, user: User, password: str, **_): 40 """Connect to ldap and update password.""" 41 sources = LDAPSource.objects.filter(sync_users_password=True, enabled=True) 42 if not sources.exists(): 43 return 44 source = sources.first() 45 if source.pk == getattr(sender, "pk", None): 46 return 47 if not LDAPPasswordChanger.should_check_user(user): 48 return 49 from ldap3.core.exceptions import LDAPException, LDAPOperationResult 50 51 try: 52 changer = LDAPPasswordChanger(source) 53 changer.change_password(user, password) 54 except LDAPException as exc: 55 LOGGER.warning("failed to set LDAP password", exc=exc) 56 if isinstance(exc, LDAPOperationResult): 57 message = ( 58 "Failed to change password in LDAP source due to remote error: " 59 f"{exc.result}, {exc.message}, {exc.description}" 60 ) 61 else: 62 message = f"Failed to change password in LDAP source: {type(exc).__name__}: {exc}" 63 Event.new( 64 EventAction.CONFIGURATION_ERROR, 65 message=message, 66 source=source, 67 ).set_user(user).save() 68 raise ValidationError("Failed to set password") from exc
LOGGER =
<BoundLoggerLazyProxy(logger=None, wrapper_class=None, processors=None, context_class=None, initial_values={}, logger_factory_args=())>
@receiver(password_validate)
def
ldap_password_validate(sender, password: str, plan_context: dict[str, typing.Any], **__):
22@receiver(password_validate) 23def ldap_password_validate(sender, password: str, plan_context: dict[str, Any], **__): 24 """if there's an LDAP Source with enabled password sync, check the password""" 25 sources = LDAPSource.objects.filter(sync_users_password=True, enabled=True) 26 if not sources.exists(): 27 return 28 source = sources.first() 29 user = plan_context.get(PLAN_CONTEXT_PENDING_USER, None) 30 if user and not LDAPPasswordChanger.should_check_user(user): 31 return 32 changer = LDAPPasswordChanger(source) 33 if changer.check_ad_password_complexity_enabled(): 34 passing = changer.ad_password_complexity(password, user) 35 if not passing: 36 raise ValidationError(_("Password does not match Active Directory Complexity."))
if there's an LDAP Source with enabled password sync, check the password
@receiver(password_changed)
def
ldap_sync_password(sender, user: authentik.core.models.User, password: str, **_):
39@receiver(password_changed) 40def ldap_sync_password(sender, user: User, password: str, **_): 41 """Connect to ldap and update password.""" 42 sources = LDAPSource.objects.filter(sync_users_password=True, enabled=True) 43 if not sources.exists(): 44 return 45 source = sources.first() 46 if source.pk == getattr(sender, "pk", None): 47 return 48 if not LDAPPasswordChanger.should_check_user(user): 49 return 50 from ldap3.core.exceptions import LDAPException, LDAPOperationResult 51 52 try: 53 changer = LDAPPasswordChanger(source) 54 changer.change_password(user, password) 55 except LDAPException as exc: 56 LOGGER.warning("failed to set LDAP password", exc=exc) 57 if isinstance(exc, LDAPOperationResult): 58 message = ( 59 "Failed to change password in LDAP source due to remote error: " 60 f"{exc.result}, {exc.message}, {exc.description}" 61 ) 62 else: 63 message = f"Failed to change password in LDAP source: {type(exc).__name__}: {exc}" 64 Event.new( 65 EventAction.CONFIGURATION_ERROR, 66 message=message, 67 source=source, 68 ).set_user(user).save() 69 raise ValidationError("Failed to set password") from exc
Connect to ldap and update password.