authentik.enterprise.policies.unique_password.signals

authentik policy signals

 1"""authentik policy signals"""
 2
 3from django.dispatch import receiver
 4
 5from authentik.core.models import User
 6from authentik.core.signals import password_changed
 7from authentik.enterprise.policies.unique_password.models import (
 8    UniquePasswordPolicy,
 9    UserPasswordHistory,
10)
11
12
13@receiver(password_changed)
14def copy_password_to_password_history(sender, user: User, *args, **kwargs):
15    """Preserve the user's old password if UniquePasswordPolicy is enabled anywhere"""
16    # Check if any UniquePasswordPolicy is in use
17    unique_pwd_policy_in_use = UniquePasswordPolicy.is_in_use()
18
19    if unique_pwd_policy_in_use:
20        """NOTE: Because we run this in a signal after saving the user,
21        we are not atomically guaranteed to save password history.
22        """
23        UserPasswordHistory.create_for_user(user, user.password)
@receiver(password_changed)
def copy_password_to_password_history(sender, user: authentik.core.models.User, *args, **kwargs):
14@receiver(password_changed)
15def copy_password_to_password_history(sender, user: User, *args, **kwargs):
16    """Preserve the user's old password if UniquePasswordPolicy is enabled anywhere"""
17    # Check if any UniquePasswordPolicy is in use
18    unique_pwd_policy_in_use = UniquePasswordPolicy.is_in_use()
19
20    if unique_pwd_policy_in_use:
21        """NOTE: Because we run this in a signal after saving the user,
22        we are not atomically guaranteed to save password history.
23        """
24        UserPasswordHistory.create_for_user(user, user.password)

Preserve the user's old password if UniquePasswordPolicy is enabled anywhere