authentik.stages.identification.models

identification stage models

  1"""identification stage models"""
  2
  3from django.contrib.postgres.fields import ArrayField
  4from django.db import models
  5from django.utils.translation import gettext_lazy as _
  6from django.views import View
  7from rest_framework.serializers import BaseSerializer
  8
  9from authentik.core.models import Source
 10from authentik.flows.models import Flow, Stage
 11from authentik.lib.models import SimpleThroughModel
 12from authentik.stages.authenticator_validate.models import AuthenticatorValidateStage
 13from authentik.stages.captcha.models import CaptchaStage
 14from authentik.stages.password.models import PasswordStage
 15
 16
 17class UserFields(models.TextChoices):
 18    """Fields which the user can identify themselves with"""
 19
 20    E_MAIL = "email"
 21    USERNAME = "username"
 22    UPN = "upn"
 23
 24
 25class IdentificationStage(Stage):
 26    """Identify the user for authentication."""
 27
 28    user_fields = ArrayField(
 29        models.CharField(max_length=100, choices=UserFields.choices),
 30        blank=True,
 31        help_text=_(
 32            "Fields of the user object to match against. (Hold shift to select multiple options)"
 33        ),
 34    )
 35
 36    password_stage = models.ForeignKey(
 37        PasswordStage,
 38        null=True,
 39        default=None,
 40        on_delete=models.SET_NULL,
 41        help_text=_(
 42            (
 43                "When set, shows a password field, instead of showing the "
 44                "password field as separate step."
 45            ),
 46        ),
 47    )
 48
 49    captcha_stage = models.ForeignKey(
 50        CaptchaStage,
 51        null=True,
 52        default=None,
 53        on_delete=models.SET_NULL,
 54        help_text=_(
 55            (
 56                "When set, adds functionality exactly like a Captcha stage, but baked into the "
 57                "Identification stage."
 58            ),
 59        ),
 60    )
 61
 62    webauthn_stage = models.ForeignKey(
 63        AuthenticatorValidateStage,
 64        null=True,
 65        default=None,
 66        on_delete=models.SET_NULL,
 67        help_text=_(
 68            (
 69                "When set, and conditional WebAuthn is available, allow the user to use their "
 70                "passkey as a first factor."
 71            ),
 72        ),
 73    )
 74
 75    case_insensitive_matching = models.BooleanField(
 76        default=True,
 77        help_text=_("When enabled, user fields are matched regardless of their casing."),
 78    )
 79    show_matched_user = models.BooleanField(
 80        default=True,
 81        help_text=_(
 82            "When a valid username/email has been entered, and this option is enabled, "
 83            "the user's username and avatar will be shown. Otherwise, the text that the user "
 84            "entered will be shown"
 85        ),
 86    )
 87    pretend_user_exists = models.BooleanField(
 88        default=True,
 89        help_text=_(
 90            "When enabled, the stage will succeed and continue even when incorrect user info "
 91            "is entered."
 92        ),
 93    )
 94    enable_remember_me = models.BooleanField(
 95        default=False,
 96        help_text=_(
 97            "Show the user the 'Remember me on this device' toggle, allowing repeat "
 98            "users to skip straight to entering their password."
 99        ),
100    )
101    enrollment_flow = models.ForeignKey(
102        Flow,
103        on_delete=models.SET_DEFAULT,
104        null=True,
105        blank=True,
106        related_name="+",
107        default=None,
108        help_text=_("Optional enrollment flow, which is linked at the bottom of the page."),
109    )
110    recovery_flow = models.ForeignKey(
111        Flow,
112        on_delete=models.SET_DEFAULT,
113        null=True,
114        blank=True,
115        related_name="+",
116        default=None,
117        help_text=_("Optional recovery flow, which is linked at the bottom of the page."),
118    )
119    passwordless_flow = models.ForeignKey(
120        Flow,
121        on_delete=models.SET_DEFAULT,
122        null=True,
123        blank=True,
124        related_name="+",
125        default=None,
126        help_text=_("Optional passwordless flow, which is linked at the bottom of the page."),
127    )
128
129    sources = models.ManyToManyField(
130        Source,
131        default=list,
132        help_text=_("Specify which sources should be shown."),
133        blank=True,
134        through="IdentificationStageSource",
135    )
136    show_source_labels = models.BooleanField(default=False)
137
138    @property
139    def serializer(self) -> type[BaseSerializer]:
140        from authentik.stages.identification.api import IdentificationStageSerializer
141
142        return IdentificationStageSerializer
143
144    @property
145    def view(self) -> type[View]:
146        from authentik.stages.identification.stage import IdentificationStageView
147
148        return IdentificationStageView
149
150    @property
151    def component(self) -> str:
152        return "ak-stage-identification-form"
153
154    class Meta:
155        verbose_name = _("Identification Stage")
156        verbose_name_plural = _("Identification Stages")
157
158
159class IdentificationStageSource(SimpleThroughModel):
160    identification_stage = models.ForeignKey(
161        IdentificationStage,
162        on_delete=models.CASCADE,
163        db_column="identificationstage_id",
164    )
165    source = models.ForeignKey(Source, on_delete=models.CASCADE)
166
167    class Meta:
168        db_table = "authentik_stages_identification_identificationstage_sources"
169        unique_together = (("identification_stage", "source"),)
170        verbose_name = _("Identification Stage Source")
171        verbose_name_plural = _("Identification Stage Sources")
172
173    def __str__(self):
174        return (
175            f"IdentificationStageSource for IdentificationStage {self.identification_stage_id} "
176            f"and Source {self.source_id}."
177        )
class UserFields(django.db.models.enums.TextChoices):
18class UserFields(models.TextChoices):
19    """Fields which the user can identify themselves with"""
20
21    E_MAIL = "email"
22    USERNAME = "username"
23    UPN = "upn"

Fields which the user can identify themselves with

USERNAME = UserFields.USERNAME
class IdentificationStage(authentik.flows.models.Stage):
 26class IdentificationStage(Stage):
 27    """Identify the user for authentication."""
 28
 29    user_fields = ArrayField(
 30        models.CharField(max_length=100, choices=UserFields.choices),
 31        blank=True,
 32        help_text=_(
 33            "Fields of the user object to match against. (Hold shift to select multiple options)"
 34        ),
 35    )
 36
 37    password_stage = models.ForeignKey(
 38        PasswordStage,
 39        null=True,
 40        default=None,
 41        on_delete=models.SET_NULL,
 42        help_text=_(
 43            (
 44                "When set, shows a password field, instead of showing the "
 45                "password field as separate step."
 46            ),
 47        ),
 48    )
 49
 50    captcha_stage = models.ForeignKey(
 51        CaptchaStage,
 52        null=True,
 53        default=None,
 54        on_delete=models.SET_NULL,
 55        help_text=_(
 56            (
 57                "When set, adds functionality exactly like a Captcha stage, but baked into the "
 58                "Identification stage."
 59            ),
 60        ),
 61    )
 62
 63    webauthn_stage = models.ForeignKey(
 64        AuthenticatorValidateStage,
 65        null=True,
 66        default=None,
 67        on_delete=models.SET_NULL,
 68        help_text=_(
 69            (
 70                "When set, and conditional WebAuthn is available, allow the user to use their "
 71                "passkey as a first factor."
 72            ),
 73        ),
 74    )
 75
 76    case_insensitive_matching = models.BooleanField(
 77        default=True,
 78        help_text=_("When enabled, user fields are matched regardless of their casing."),
 79    )
 80    show_matched_user = models.BooleanField(
 81        default=True,
 82        help_text=_(
 83            "When a valid username/email has been entered, and this option is enabled, "
 84            "the user's username and avatar will be shown. Otherwise, the text that the user "
 85            "entered will be shown"
 86        ),
 87    )
 88    pretend_user_exists = models.BooleanField(
 89        default=True,
 90        help_text=_(
 91            "When enabled, the stage will succeed and continue even when incorrect user info "
 92            "is entered."
 93        ),
 94    )
 95    enable_remember_me = models.BooleanField(
 96        default=False,
 97        help_text=_(
 98            "Show the user the 'Remember me on this device' toggle, allowing repeat "
 99            "users to skip straight to entering their password."
100        ),
101    )
102    enrollment_flow = models.ForeignKey(
103        Flow,
104        on_delete=models.SET_DEFAULT,
105        null=True,
106        blank=True,
107        related_name="+",
108        default=None,
109        help_text=_("Optional enrollment flow, which is linked at the bottom of the page."),
110    )
111    recovery_flow = models.ForeignKey(
112        Flow,
113        on_delete=models.SET_DEFAULT,
114        null=True,
115        blank=True,
116        related_name="+",
117        default=None,
118        help_text=_("Optional recovery flow, which is linked at the bottom of the page."),
119    )
120    passwordless_flow = models.ForeignKey(
121        Flow,
122        on_delete=models.SET_DEFAULT,
123        null=True,
124        blank=True,
125        related_name="+",
126        default=None,
127        help_text=_("Optional passwordless flow, which is linked at the bottom of the page."),
128    )
129
130    sources = models.ManyToManyField(
131        Source,
132        default=list,
133        help_text=_("Specify which sources should be shown."),
134        blank=True,
135        through="IdentificationStageSource",
136    )
137    show_source_labels = models.BooleanField(default=False)
138
139    @property
140    def serializer(self) -> type[BaseSerializer]:
141        from authentik.stages.identification.api import IdentificationStageSerializer
142
143        return IdentificationStageSerializer
144
145    @property
146    def view(self) -> type[View]:
147        from authentik.stages.identification.stage import IdentificationStageView
148
149        return IdentificationStageView
150
151    @property
152    def component(self) -> str:
153        return "ak-stage-identification-form"
154
155    class Meta:
156        verbose_name = _("Identification Stage")
157        verbose_name_plural = _("Identification Stages")

Identify the user for authentication.

def user_fields(unknown):

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

password_stage

Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.

In the example::

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Child.parent is a ForwardManyToOneDescriptor instance.

captcha_stage

Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.

In the example::

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Child.parent is a ForwardManyToOneDescriptor instance.

webauthn_stage

Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.

In the example::

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Child.parent is a ForwardManyToOneDescriptor instance.

def case_insensitive_matching(unknown):

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

def show_matched_user(unknown):

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

def pretend_user_exists(unknown):

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

def enable_remember_me(unknown):

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

enrollment_flow

Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.

In the example::

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Child.parent is a ForwardManyToOneDescriptor instance.

recovery_flow

Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.

In the example::

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Child.parent is a ForwardManyToOneDescriptor instance.

passwordless_flow

Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.

In the example::

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Child.parent is a ForwardManyToOneDescriptor instance.

sources

Accessor to the related objects manager on the forward and reverse sides of a many-to-many relation.

In the example::

class Pizza(Model):
    toppings = ManyToManyField(Topping, related_name='pizzas')

Pizza.toppings and Topping.pizzas are ManyToManyDescriptor instances.

Most of the implementation is delegated to a dynamically defined manager class built by create_forward_many_to_many_manager() defined below.

def show_source_labels(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]
139    @property
140    def serializer(self) -> type[BaseSerializer]:
141        from authentik.stages.identification.api import IdentificationStageSerializer
142
143        return IdentificationStageSerializer

Get serializer for this model

view: type[django.views.generic.base.View]
145    @property
146    def view(self) -> type[View]:
147        from authentik.stages.identification.stage import IdentificationStageView
148
149        return IdentificationStageView

Return StageView class that implements logic for this stage

component: str
151    @property
152    def component(self) -> str:
153        return "ak-stage-identification-form"

Return component used to edit this object

password_stage_id
captcha_stage_id
webauthn_stage_id
enrollment_flow_id
recovery_flow_id
passwordless_flow_id
stage_ptr_id
stage_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.

identificationstagesource_set

Accessor to the related objects manager on the reverse side of a many-to-one relation.

In the example::

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Parent.children is a ReverseManyToOneDescriptor instance.

Most of the implementation is delegated to a dynamically defined manager class built by create_forward_many_to_many_manager() defined below.

class IdentificationStage.DoesNotExist(authentik.flows.models.Stage.DoesNotExist):

The requested object does not exist

class IdentificationStage.MultipleObjectsReturned(authentik.flows.models.Stage.MultipleObjectsReturned):

The query returned multiple objects when only one was expected.

class IdentificationStageSource(authentik.lib.models.SimpleThroughModel):
160class IdentificationStageSource(SimpleThroughModel):
161    identification_stage = models.ForeignKey(
162        IdentificationStage,
163        on_delete=models.CASCADE,
164        db_column="identificationstage_id",
165    )
166    source = models.ForeignKey(Source, on_delete=models.CASCADE)
167
168    class Meta:
169        db_table = "authentik_stages_identification_identificationstage_sources"
170        unique_together = (("identification_stage", "source"),)
171        verbose_name = _("Identification Stage Source")
172        verbose_name_plural = _("Identification Stage Sources")
173
174    def __str__(self):
175        return (
176            f"IdentificationStageSource for IdentificationStage {self.identification_stage_id} "
177            f"and Source {self.source_id}."
178        )

IdentificationStageSource(id, identification_stage, source)

identification_stage

Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.

In the example::

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Child.parent is a ForwardManyToOneDescriptor instance.

source

Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.

In the example::

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Child.parent is a ForwardManyToOneDescriptor instance.

identification_stage_id
source_id
def id(unknown):

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

def objects(unknown):

The type of the None singleton.

class IdentificationStageSource.DoesNotExist(django.core.exceptions.ObjectDoesNotExist):

The requested object does not exist

class IdentificationStageSource.MultipleObjectsReturned(django.core.exceptions.MultipleObjectsReturned):

The query returned multiple objects when only one was expected.