authentik.enterprise.stages.mtls.models

 1from django.db import models
 2from django.utils.translation import gettext_lazy as _
 3from rest_framework.serializers import Serializer
 4
 5from authentik.crypto.models import CertificateKeyPair
 6from authentik.endpoints.models import StageMode
 7from authentik.flows.models import Stage
 8from authentik.flows.stage import StageView
 9from authentik.lib.models import SimpleThroughModel
10
11
12class CertAttributes(models.TextChoices):
13    """Certificate attribute used for user matching"""
14
15    SUBJECT = "subject"
16    COMMON_NAME = "common_name"
17    EMAIL = "email"
18
19
20class UserAttributes(models.TextChoices):
21    """User attribute for user matching"""
22
23    USERNAME = "username"
24    EMAIL = "email"
25
26
27class MutualTLSStage(Stage):
28    """Authenticate/enroll users using a client-certificate."""
29
30    mode = models.TextField(choices=StageMode.choices)
31
32    certificate_authorities = models.ManyToManyField(
33        CertificateKeyPair,
34        default=None,
35        blank=True,
36        help_text=_(
37            "Configure certificate authorities to validate the certificate against. "
38            "This option has a higher priority than the `client_certificate` option on `Brand`."
39        ),
40        through="MutualTLSStageCertificateAuthority",
41    )
42
43    cert_attribute = models.TextField(choices=CertAttributes.choices)
44    user_attribute = models.TextField(choices=UserAttributes.choices)
45
46    @property
47    def view(self) -> type[StageView]:
48        from authentik.enterprise.stages.mtls.stage import MTLSStageView
49
50        return MTLSStageView
51
52    @property
53    def serializer(self) -> type[Serializer]:
54        from authentik.enterprise.stages.mtls.api import MutualTLSStageSerializer
55
56        return MutualTLSStageSerializer
57
58    @property
59    def component(self) -> str:
60        return "ak-stage-mtls-form"
61
62    class Meta:
63        verbose_name = _("Mutual TLS Stage")
64        verbose_name_plural = _("Mutual TLS Stages")
65        permissions = [
66            ("pass_outpost_certificate", _("Permissions to pass Certificates for outposts.")),
67        ]
68
69
70class MutualTLSStageCertificateAuthority(SimpleThroughModel):
71    mutual_tls_stage = models.ForeignKey(
72        MutualTLSStage,
73        on_delete=models.CASCADE,
74        db_column="mutualtlsstage_id",
75    )
76    certificate_key_pair = models.ForeignKey(
77        CertificateKeyPair, on_delete=models.CASCADE, db_column="certificatekeypair_id"
78    )
79
80    class Meta:
81        db_table = "authentik_stages_mtls_mutualtlsstage_certificate_authorities"
82        unique_together = (("mutual_tls_stage", "certificate_key_pair"),)
83        verbose_name = _("Mutual TLS Stage Certificate Authority")
84        verbose_name_plural = _("Mutual TLS Stage Certificate Authoritys")
85
86    def __str__(self):
87        return (
88            "MutualTLSStageCertificateAuthority for MutualTLSStage "
89            f"{self.mutual_tls_stage_id} "
90            f"and CertificateKeyPair {self.certificate_key_pair_id}."
91        )
class CertAttributes(django.db.models.enums.TextChoices):
13class CertAttributes(models.TextChoices):
14    """Certificate attribute used for user matching"""
15
16    SUBJECT = "subject"
17    COMMON_NAME = "common_name"
18    EMAIL = "email"

Certificate attribute used for user matching

class UserAttributes(django.db.models.enums.TextChoices):
21class UserAttributes(models.TextChoices):
22    """User attribute for user matching"""
23
24    USERNAME = "username"
25    EMAIL = "email"

User attribute for user matching

class MutualTLSStage(authentik.flows.models.Stage):
28class MutualTLSStage(Stage):
29    """Authenticate/enroll users using a client-certificate."""
30
31    mode = models.TextField(choices=StageMode.choices)
32
33    certificate_authorities = models.ManyToManyField(
34        CertificateKeyPair,
35        default=None,
36        blank=True,
37        help_text=_(
38            "Configure certificate authorities to validate the certificate against. "
39            "This option has a higher priority than the `client_certificate` option on `Brand`."
40        ),
41        through="MutualTLSStageCertificateAuthority",
42    )
43
44    cert_attribute = models.TextField(choices=CertAttributes.choices)
45    user_attribute = models.TextField(choices=UserAttributes.choices)
46
47    @property
48    def view(self) -> type[StageView]:
49        from authentik.enterprise.stages.mtls.stage import MTLSStageView
50
51        return MTLSStageView
52
53    @property
54    def serializer(self) -> type[Serializer]:
55        from authentik.enterprise.stages.mtls.api import MutualTLSStageSerializer
56
57        return MutualTLSStageSerializer
58
59    @property
60    def component(self) -> str:
61        return "ak-stage-mtls-form"
62
63    class Meta:
64        verbose_name = _("Mutual TLS Stage")
65        verbose_name_plural = _("Mutual TLS Stages")
66        permissions = [
67            ("pass_outpost_certificate", _("Permissions to pass Certificates for outposts.")),
68        ]

Authenticate/enroll users using a client-certificate.

def mode(unknown):

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

certificate_authorities

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 cert_attribute(unknown):

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

def user_attribute(unknown):

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

view: type[authentik.flows.stage.StageView]
47    @property
48    def view(self) -> type[StageView]:
49        from authentik.enterprise.stages.mtls.stage import MTLSStageView
50
51        return MTLSStageView

Return StageView class that implements logic for this stage

serializer: type[rest_framework.serializers.Serializer]
53    @property
54    def serializer(self) -> type[Serializer]:
55        from authentik.enterprise.stages.mtls.api import MutualTLSStageSerializer
56
57        return MutualTLSStageSerializer

Get serializer for this model

component: str
59    @property
60    def component(self) -> str:
61        return "ak-stage-mtls-form"

Return component used to edit this object

def get_mode_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_cert_attribute_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_user_attribute_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.

mutualtlsstagecertificateauthority_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 MutualTLSStage.DoesNotExist(authentik.flows.models.Stage.DoesNotExist):

The requested object does not exist

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

The query returned multiple objects when only one was expected.

class MutualTLSStageCertificateAuthority(authentik.lib.models.SimpleThroughModel):
71class MutualTLSStageCertificateAuthority(SimpleThroughModel):
72    mutual_tls_stage = models.ForeignKey(
73        MutualTLSStage,
74        on_delete=models.CASCADE,
75        db_column="mutualtlsstage_id",
76    )
77    certificate_key_pair = models.ForeignKey(
78        CertificateKeyPair, on_delete=models.CASCADE, db_column="certificatekeypair_id"
79    )
80
81    class Meta:
82        db_table = "authentik_stages_mtls_mutualtlsstage_certificate_authorities"
83        unique_together = (("mutual_tls_stage", "certificate_key_pair"),)
84        verbose_name = _("Mutual TLS Stage Certificate Authority")
85        verbose_name_plural = _("Mutual TLS Stage Certificate Authoritys")
86
87    def __str__(self):
88        return (
89            "MutualTLSStageCertificateAuthority for MutualTLSStage "
90            f"{self.mutual_tls_stage_id} "
91            f"and CertificateKeyPair {self.certificate_key_pair_id}."
92        )

MutualTLSStageCertificateAuthority(id, mutual_tls_stage, certificate_key_pair)

mutual_tls_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.

certificate_key_pair

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.

mutual_tls_stage_id
certificate_key_pair_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 MutualTLSStageCertificateAuthority.DoesNotExist(django.core.exceptions.ObjectDoesNotExist):

The requested object does not exist

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

The query returned multiple objects when only one was expected.