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")
LOGGER = <BoundLoggerLazyProxy(logger=None, wrapper_class=None, processors=None, context_class=None, initial_values={}, logger_factory_args=())>
class PasswordExpiryPolicy(authentik.policies.models.Policy):
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

def deny_only(unknown):

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

def days(unknown):

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

serializer: type[rest_framework.serializers.BaseSerializer]
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

component: str
32    @property
33    def component(self) -> str:
34        return "ak-policy-password-expiry-form"

Return component used to edit this object

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

policy_ptr_id
policy_ptr

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.

class PasswordExpiryPolicy.DoesNotExist(authentik.policies.models.Policy.DoesNotExist):

The requested object does not exist

class PasswordExpiryPolicy.MultipleObjectsReturned(authentik.policies.models.Policy.MultipleObjectsReturned):

The query returned multiple objects when only one was expected.