authentik.stages.user_login.models

login stage models

 1"""login stage models"""
 2
 3from django.db import models
 4from django.utils.translation import gettext_lazy as _
 5from django.views import View
 6from rest_framework.serializers import BaseSerializer
 7
 8from authentik.flows.models import Stage
 9from authentik.lib.utils.time import timedelta_string_validator
10
11
12class NetworkBinding(models.TextChoices):
13    """Network session binding modes"""
14
15    NO_BINDING = "no_binding"
16    BIND_ASN = "bind_asn"  # Bind to ASN only
17    BIND_ASN_NETWORK = "bind_asn_network"  # Bind to ASN and Network
18    BIND_ASN_NETWORK_IP = "bind_asn_network_ip"  # Bind to ASN, Network and IP
19
20
21class GeoIPBinding(models.TextChoices):
22    """Geo session binding modes"""
23
24    NO_BINDING = "no_binding"
25    BIND_CONTINENT = "bind_continent"  # Bind to continent only
26    BIND_CONTINENT_COUNTRY = "bind_continent_country"  # Bind to continent and country
27    BIND_CONTINENT_COUNTRY_CITY = (
28        "bind_continent_country_city"  # Bind to continent, country and city
29    )
30
31
32class UserLoginStage(Stage):
33    """Attach the pending user to the current session."""
34
35    session_duration = models.TextField(
36        default="seconds=0",
37        validators=[timedelta_string_validator],
38        help_text=_(
39            "Determines how long a session lasts. Default of 0 means "
40            "that the sessions lasts until the browser is closed. "
41            "(Format: hours=-1;minutes=-2;seconds=-3)"
42        ),
43    )
44    network_binding = models.TextField(
45        choices=NetworkBinding.choices,
46        default=NetworkBinding.NO_BINDING,
47        help_text=_("Bind sessions created by this stage to the configured network"),
48    )
49    geoip_binding = models.TextField(
50        choices=GeoIPBinding.choices,
51        default=GeoIPBinding.NO_BINDING,
52        help_text=_("Bind sessions created by this stage to the configured GeoIP location"),
53    )
54    terminate_other_sessions = models.BooleanField(
55        default=False, help_text=_("Terminate all other sessions of the user logging in.")
56    )
57    remember_me_offset = models.TextField(
58        default="seconds=0",
59        validators=[timedelta_string_validator],
60        help_text=_(
61            "Offset the session will be extended by when the user picks the remember me option. "
62            "Default of 0 means that the remember me option will not be shown. "
63            "(Format: hours=-1;minutes=-2;seconds=-3)"
64        ),
65    )
66    remember_device = models.TextField(
67        default="days=30",
68        validators=[timedelta_string_validator],
69        help_text=_(
70            "When set to a non-zero value, authentik will save a cookie with a longer expiry,"
71            "to remember the device the user is logging in from. "
72            "(Format: hours=-1;minutes=-2;seconds=-3)"
73        ),
74    )
75
76    @property
77    def serializer(self) -> type[BaseSerializer]:
78        from authentik.stages.user_login.api import UserLoginStageSerializer
79
80        return UserLoginStageSerializer
81
82    @property
83    def view(self) -> type[View]:
84        from authentik.stages.user_login.stage import UserLoginStageView
85
86        return UserLoginStageView
87
88    @property
89    def component(self) -> str:
90        return "ak-stage-user-login-form"
91
92    class Meta:
93        verbose_name = _("User Login Stage")
94        verbose_name_plural = _("User Login Stages")
class NetworkBinding(django.db.models.enums.TextChoices):
13class NetworkBinding(models.TextChoices):
14    """Network session binding modes"""
15
16    NO_BINDING = "no_binding"
17    BIND_ASN = "bind_asn"  # Bind to ASN only
18    BIND_ASN_NETWORK = "bind_asn_network"  # Bind to ASN and Network
19    BIND_ASN_NETWORK_IP = "bind_asn_network_ip"  # Bind to ASN, Network and IP

Network session binding modes

BIND_ASN_NETWORK = NetworkBinding.BIND_ASN_NETWORK
BIND_ASN_NETWORK_IP = NetworkBinding.BIND_ASN_NETWORK_IP
class GeoIPBinding(django.db.models.enums.TextChoices):
22class GeoIPBinding(models.TextChoices):
23    """Geo session binding modes"""
24
25    NO_BINDING = "no_binding"
26    BIND_CONTINENT = "bind_continent"  # Bind to continent only
27    BIND_CONTINENT_COUNTRY = "bind_continent_country"  # Bind to continent and country
28    BIND_CONTINENT_COUNTRY_CITY = (
29        "bind_continent_country_city"  # Bind to continent, country and city
30    )

Geo session binding modes

BIND_CONTINENT = GeoIPBinding.BIND_CONTINENT
BIND_CONTINENT_COUNTRY = GeoIPBinding.BIND_CONTINENT_COUNTRY
BIND_CONTINENT_COUNTRY_CITY = GeoIPBinding.BIND_CONTINENT_COUNTRY_CITY
class UserLoginStage(authentik.flows.models.Stage):
33class UserLoginStage(Stage):
34    """Attach the pending user to the current session."""
35
36    session_duration = models.TextField(
37        default="seconds=0",
38        validators=[timedelta_string_validator],
39        help_text=_(
40            "Determines how long a session lasts. Default of 0 means "
41            "that the sessions lasts until the browser is closed. "
42            "(Format: hours=-1;minutes=-2;seconds=-3)"
43        ),
44    )
45    network_binding = models.TextField(
46        choices=NetworkBinding.choices,
47        default=NetworkBinding.NO_BINDING,
48        help_text=_("Bind sessions created by this stage to the configured network"),
49    )
50    geoip_binding = models.TextField(
51        choices=GeoIPBinding.choices,
52        default=GeoIPBinding.NO_BINDING,
53        help_text=_("Bind sessions created by this stage to the configured GeoIP location"),
54    )
55    terminate_other_sessions = models.BooleanField(
56        default=False, help_text=_("Terminate all other sessions of the user logging in.")
57    )
58    remember_me_offset = models.TextField(
59        default="seconds=0",
60        validators=[timedelta_string_validator],
61        help_text=_(
62            "Offset the session will be extended by when the user picks the remember me option. "
63            "Default of 0 means that the remember me option will not be shown. "
64            "(Format: hours=-1;minutes=-2;seconds=-3)"
65        ),
66    )
67    remember_device = models.TextField(
68        default="days=30",
69        validators=[timedelta_string_validator],
70        help_text=_(
71            "When set to a non-zero value, authentik will save a cookie with a longer expiry,"
72            "to remember the device the user is logging in from. "
73            "(Format: hours=-1;minutes=-2;seconds=-3)"
74        ),
75    )
76
77    @property
78    def serializer(self) -> type[BaseSerializer]:
79        from authentik.stages.user_login.api import UserLoginStageSerializer
80
81        return UserLoginStageSerializer
82
83    @property
84    def view(self) -> type[View]:
85        from authentik.stages.user_login.stage import UserLoginStageView
86
87        return UserLoginStageView
88
89    @property
90    def component(self) -> str:
91        return "ak-stage-user-login-form"
92
93    class Meta:
94        verbose_name = _("User Login Stage")
95        verbose_name_plural = _("User Login Stages")

Attach the pending user to the current session.

def session_duration(unknown):

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

def network_binding(unknown):

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

def geoip_binding(unknown):

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

def terminate_other_sessions(unknown):

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

def remember_me_offset(unknown):

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

def remember_device(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]
77    @property
78    def serializer(self) -> type[BaseSerializer]:
79        from authentik.stages.user_login.api import UserLoginStageSerializer
80
81        return UserLoginStageSerializer

Get serializer for this model

view: type[django.views.generic.base.View]
83    @property
84    def view(self) -> type[View]:
85        from authentik.stages.user_login.stage import UserLoginStageView
86
87        return UserLoginStageView

Return StageView class that implements logic for this stage

component: str
89    @property
90    def component(self) -> str:
91        return "ak-stage-user-login-form"

Return component used to edit this object

def get_network_binding_display(unknown):

Method descriptor with partial application of the given arguments and keywords.

Supports wrapping existing descriptors and handles non-descriptor callables as instance methods.

def get_geoip_binding_display(unknown):

Method descriptor with partial application of the given arguments and keywords.

Supports wrapping existing descriptors and handles non-descriptor callables as instance methods.

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.

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

The requested object does not exist

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

The query returned multiple objects when only one was expected.