authentik.policies.dummy.models

Dummy policy

 1"""Dummy policy"""
 2
 3from random import SystemRandom
 4from time import sleep
 5
 6from django.db import models
 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
13
14LOGGER = get_logger()
15
16
17class DummyPolicy(Policy):
18    """Policy used for debugging the PolicyEngine. Returns a fixed result,
19    but takes a random time to process."""
20
21    __debug_only__ = True
22
23    result = models.BooleanField(default=False)
24    wait_min = models.IntegerField(default=5)
25    wait_max = models.IntegerField(default=30)
26
27    @property
28    def serializer(self) -> type[BaseSerializer]:
29        from authentik.policies.dummy.api import DummyPolicySerializer
30
31        return DummyPolicySerializer
32
33    @property
34    def component(self) -> str:  # pragma: no cover
35        return "ak-policy-dummy-form"
36
37    def passes(self, request: PolicyRequest) -> PolicyResult:
38        """Wait random time then return result"""
39        wait = SystemRandom().randrange(self.wait_min, self.wait_max)
40        LOGGER.info("Policy waiting", policy=self, delay=wait)
41        sleep(wait)
42        return PolicyResult(self.result, "dummy")
43
44    class Meta(Policy.PolicyMeta):
45        verbose_name = _("Dummy Policy")
46        verbose_name_plural = _("Dummy Policies")
LOGGER = <BoundLoggerLazyProxy(logger=None, wrapper_class=None, processors=None, context_class=None, initial_values={}, logger_factory_args=())>
class DummyPolicy(authentik.policies.models.Policy):
18class DummyPolicy(Policy):
19    """Policy used for debugging the PolicyEngine. Returns a fixed result,
20    but takes a random time to process."""
21
22    __debug_only__ = True
23
24    result = models.BooleanField(default=False)
25    wait_min = models.IntegerField(default=5)
26    wait_max = models.IntegerField(default=30)
27
28    @property
29    def serializer(self) -> type[BaseSerializer]:
30        from authentik.policies.dummy.api import DummyPolicySerializer
31
32        return DummyPolicySerializer
33
34    @property
35    def component(self) -> str:  # pragma: no cover
36        return "ak-policy-dummy-form"
37
38    def passes(self, request: PolicyRequest) -> PolicyResult:
39        """Wait random time then return result"""
40        wait = SystemRandom().randrange(self.wait_min, self.wait_max)
41        LOGGER.info("Policy waiting", policy=self, delay=wait)
42        sleep(wait)
43        return PolicyResult(self.result, "dummy")
44
45    class Meta(Policy.PolicyMeta):
46        verbose_name = _("Dummy Policy")
47        verbose_name_plural = _("Dummy Policies")

Policy used for debugging the PolicyEngine. Returns a fixed result, but takes a random time to process.

def result(unknown):

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

def wait_min(unknown):

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

def wait_max(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]
28    @property
29    def serializer(self) -> type[BaseSerializer]:
30        from authentik.policies.dummy.api import DummyPolicySerializer
31
32        return DummyPolicySerializer

Get serializer for this model

component: str
34    @property
35    def component(self) -> str:  # pragma: no cover
36        return "ak-policy-dummy-form"

Return component used to edit this object

38    def passes(self, request: PolicyRequest) -> PolicyResult:
39        """Wait random time then return result"""
40        wait = SystemRandom().randrange(self.wait_min, self.wait_max)
41        LOGGER.info("Policy waiting", policy=self, delay=wait)
42        sleep(wait)
43        return PolicyResult(self.result, "dummy")

Wait random time then return result

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 DummyPolicy.DoesNotExist(authentik.policies.models.Policy.DoesNotExist):

The requested object does not exist

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

The query returned multiple objects when only one was expected.