authentik.enterprise.stages.password.lockout
Licensed policy for creating password locks after failed attempts.
1"""Licensed policy for creating password locks after failed attempts.""" 2 3from django.utils.timezone import now 4 5from authentik.core.models import User 6from authentik.enterprise.license import LicenseKey 7from authentik.events.models import Event, EventAction 8from authentik.sources.kerberos.models import UserKerberosSourceConnection 9from authentik.sources.ldap.models import LDAP_DISTINGUISHED_NAME, LDAPSource 10from authentik.stages.password import BACKEND_KERBEROS, BACKEND_LDAP 11from authentik.stages.password.lockout import PasswordLockoutResult 12from authentik.stages.password.models import PasswordDevice 13 14 15class PasswordLockoutMixin: 16 """Count failures while the caller holds the password device's row lock.""" 17 18 def record_failure(self, device: PasswordDevice) -> PasswordLockoutResult: 19 if not LicenseKey.cached_summary().status.is_valid: 20 return PasswordLockoutResult() 21 devices = PasswordDevice.objects.filter(pk=device.pk) 22 threshold = self.password_stage.failed_attempts_before_lockout 23 if threshold == 0 or self._uses_external_password(device.user): 24 return PasswordLockoutResult() 25 26 attempts = device.failed_attempts + 1 27 if attempts < threshold: 28 devices.update(failed_attempts=attempts) 29 return PasswordLockoutResult(last_attempt=attempts == threshold - 1) 30 31 devices.update(failed_attempts=0, locked_at=now()) 32 Event.new( 33 EventAction.AUTHENTICATOR_LOCKED, 34 affected_user=device.user, 35 authenticator=device, 36 reason="failed_attempts", 37 threshold=threshold, 38 ).from_http(self.request) 39 return PasswordLockoutResult(lockout_reached=True) 40 41 def _uses_external_password(self, user: User) -> bool: 42 """Return whether the user's password is verified by an external system.""" 43 backends = self.password_stage.backends 44 if ( 45 BACKEND_LDAP in backends 46 and LDAP_DISTINGUISHED_NAME in user.attributes 47 and LDAPSource.objects.filter(enabled=True).exists() 48 ): 49 return True 50 return ( 51 BACKEND_KERBEROS in backends 52 and UserKerberosSourceConnection.objects.filter( 53 user=user, source__enabled=True 54 ).exists() 55 )
class
PasswordLockoutMixin:
16class PasswordLockoutMixin: 17 """Count failures while the caller holds the password device's row lock.""" 18 19 def record_failure(self, device: PasswordDevice) -> PasswordLockoutResult: 20 if not LicenseKey.cached_summary().status.is_valid: 21 return PasswordLockoutResult() 22 devices = PasswordDevice.objects.filter(pk=device.pk) 23 threshold = self.password_stage.failed_attempts_before_lockout 24 if threshold == 0 or self._uses_external_password(device.user): 25 return PasswordLockoutResult() 26 27 attempts = device.failed_attempts + 1 28 if attempts < threshold: 29 devices.update(failed_attempts=attempts) 30 return PasswordLockoutResult(last_attempt=attempts == threshold - 1) 31 32 devices.update(failed_attempts=0, locked_at=now()) 33 Event.new( 34 EventAction.AUTHENTICATOR_LOCKED, 35 affected_user=device.user, 36 authenticator=device, 37 reason="failed_attempts", 38 threshold=threshold, 39 ).from_http(self.request) 40 return PasswordLockoutResult(lockout_reached=True) 41 42 def _uses_external_password(self, user: User) -> bool: 43 """Return whether the user's password is verified by an external system.""" 44 backends = self.password_stage.backends 45 if ( 46 BACKEND_LDAP in backends 47 and LDAP_DISTINGUISHED_NAME in user.attributes 48 and LDAPSource.objects.filter(enabled=True).exists() 49 ): 50 return True 51 return ( 52 BACKEND_KERBEROS in backends 53 and UserKerberosSourceConnection.objects.filter( 54 user=user, source__enabled=True 55 ).exists() 56 )
Count failures while the caller holds the password device's row lock.
def
record_failure( self, device: authentik.stages.password.models.PasswordDevice) -> authentik.stages.password.lockout.PasswordLockoutResult:
19 def record_failure(self, device: PasswordDevice) -> PasswordLockoutResult: 20 if not LicenseKey.cached_summary().status.is_valid: 21 return PasswordLockoutResult() 22 devices = PasswordDevice.objects.filter(pk=device.pk) 23 threshold = self.password_stage.failed_attempts_before_lockout 24 if threshold == 0 or self._uses_external_password(device.user): 25 return PasswordLockoutResult() 26 27 attempts = device.failed_attempts + 1 28 if attempts < threshold: 29 devices.update(failed_attempts=attempts) 30 return PasswordLockoutResult(last_attempt=attempts == threshold - 1) 31 32 devices.update(failed_attempts=0, locked_at=now()) 33 Event.new( 34 EventAction.AUTHENTICATOR_LOCKED, 35 affected_user=device.user, 36 authenticator=device, 37 reason="failed_attempts", 38 threshold=threshold, 39 ).from_http(self.request) 40 return PasswordLockoutResult(lockout_reached=True)