authentik.policies.expiry.models
authentik password_expiry_policy Models
1"""authentik password_expiry_policy Models""" 2 3from datetime import timedelta 4 5from django.db import models 6from django.utils.timezone import now 7from django.utils.translation import gettext_lazy as _ 8from rest_framework.serializers import BaseSerializer 9from structlog.stdlib import get_logger 10 11from authentik.policies.models import Policy 12from authentik.policies.types import PolicyRequest, PolicyResult 13from authentik.stages.password.models import PasswordDevice 14 15LOGGER = get_logger() 16 17 18class PasswordExpiryPolicy(Policy): 19 """If password change date is more than x days in the past, invalidate the user's password 20 and show a notice""" 21 22 deny_only = models.BooleanField(default=False) 23 days = models.IntegerField() 24 25 @property 26 def serializer(self) -> type[BaseSerializer]: 27 from authentik.policies.expiry.api import PasswordExpiryPolicySerializer 28 29 return PasswordExpiryPolicySerializer 30 31 @property 32 def component(self) -> str: 33 return "ak-policy-password-expiry-form" 34 35 def passes(self, request: PolicyRequest) -> PolicyResult: 36 """If password change date is more than x days in the past, call set_unusable_password 37 and show a notice""" 38 actual_days = (now() - request.user.password_change_date).days 39 days_since_expiry = ( 40 now() - (request.user.password_change_date + timedelta(days=self.days)) 41 ).days 42 if actual_days >= self.days: 43 if not self.deny_only: 44 PasswordDevice.set_unusable_password(request.user) 45 message = _( 46 "Password expired {days} days ago. Please update your password." 47 ).format(days=days_since_expiry) 48 return PolicyResult(False, message) 49 return PolicyResult(False, str(_("Password has expired."))) 50 return PolicyResult(True) 51 52 class Meta(Policy.PolicyMeta): 53 verbose_name = _("Password Expiry Policy") 54 verbose_name_plural = _("Password Expiry Policies")
19class PasswordExpiryPolicy(Policy): 20 """If password change date is more than x days in the past, invalidate the user's password 21 and show a notice""" 22 23 deny_only = models.BooleanField(default=False) 24 days = models.IntegerField() 25 26 @property 27 def serializer(self) -> type[BaseSerializer]: 28 from authentik.policies.expiry.api import PasswordExpiryPolicySerializer 29 30 return PasswordExpiryPolicySerializer 31 32 @property 33 def component(self) -> str: 34 return "ak-policy-password-expiry-form" 35 36 def passes(self, request: PolicyRequest) -> PolicyResult: 37 """If password change date is more than x days in the past, call set_unusable_password 38 and show a notice""" 39 actual_days = (now() - request.user.password_change_date).days 40 days_since_expiry = ( 41 now() - (request.user.password_change_date + timedelta(days=self.days)) 42 ).days 43 if actual_days >= self.days: 44 if not self.deny_only: 45 PasswordDevice.set_unusable_password(request.user) 46 message = _( 47 "Password expired {days} days ago. Please update your password." 48 ).format(days=days_since_expiry) 49 return PolicyResult(False, message) 50 return PolicyResult(False, str(_("Password has expired."))) 51 return PolicyResult(True) 52 53 class Meta(Policy.PolicyMeta): 54 verbose_name = _("Password Expiry Policy") 55 verbose_name_plural = _("Password Expiry Policies")
If password change date is more than x days in the past, invalidate the user's password and show a notice
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
26 @property 27 def serializer(self) -> type[BaseSerializer]: 28 from authentik.policies.expiry.api import PasswordExpiryPolicySerializer 29 30 return PasswordExpiryPolicySerializer
Get serializer for this model
36 def passes(self, request: PolicyRequest) -> PolicyResult: 37 """If password change date is more than x days in the past, call set_unusable_password 38 and show a notice""" 39 actual_days = (now() - request.user.password_change_date).days 40 days_since_expiry = ( 41 now() - (request.user.password_change_date + timedelta(days=self.days)) 42 ).days 43 if actual_days >= self.days: 44 if not self.deny_only: 45 PasswordDevice.set_unusable_password(request.user) 46 message = _( 47 "Password expired {days} days ago. Please update your password." 48 ).format(days=days_since_expiry) 49 return PolicyResult(False, message) 50 return PolicyResult(False, str(_("Password has expired."))) 51 return PolicyResult(True)
If password change date is more than x days in the past, call set_unusable_password and show a notice
Accessor to the related object on the forward side of a one-to-one relation.
In the example::
class Restaurant(Model):
place = OneToOneField(Place, related_name='restaurant')
Restaurant.place is a ForwardOneToOneDescriptor instance.
Inherited Members
- authentik.policies.models.Policy
- policy_uuid
- name
- execution_logging
- objects
- PolicyMeta
- created
- last_updated
- get_next_by_created
- get_previous_by_created
- get_next_by_last_updated
- get_previous_by_last_updated
- bindings
- dummypolicy
- eventmatcherpolicy
- passwordexpirypolicy
- reputationpolicy
- expressionpolicy
- geoippolicy
- promptstage_set
- promptstagevalidationpolicy_set
- passwordpolicy
- uniquepasswordpolicy
The requested object does not exist
The query returned multiple objects when only one was expected.