authentik.brands.models

brand models

  1"""brand models"""
  2
  3from uuid import uuid4
  4
  5from django.db import models
  6from django.http import HttpRequest
  7from django.utils.translation import gettext_lazy as _
  8from rest_framework.serializers import Serializer
  9from structlog.stdlib import get_logger
 10
 11from authentik.admin.files.fields import FileField
 12from authentik.admin.files.manager import get_file_manager
 13from authentik.admin.files.usage import FileUsage
 14from authentik.crypto.models import CertificateKeyPair
 15from authentik.flows.models import Flow
 16from authentik.lib.models import SerializerModel
 17
 18LOGGER = get_logger()
 19
 20
 21class Brand(SerializerModel):
 22    """Single brand"""
 23
 24    brand_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
 25    domain = models.TextField(
 26        help_text=_(
 27            "Domain that activates this brand. Can be a superset, i.e. `a.b` for `aa.b` and `ba.b`"
 28        )
 29    )
 30    default = models.BooleanField(
 31        default=False,
 32    )
 33
 34    branding_title = models.TextField(default="authentik")
 35
 36    branding_logo = FileField(default="/static/dist/assets/icons/icon_left_brand.svg")
 37    branding_favicon = FileField(default="/static/dist/assets/icons/icon.png")
 38    branding_custom_css = models.TextField(default="", blank=True)
 39    branding_default_flow_background = FileField(
 40        default="/static/dist/assets/images/flow_background.jpg",
 41    )
 42
 43    flow_authentication = models.ForeignKey(
 44        Flow, null=True, on_delete=models.SET_NULL, related_name="brand_authentication"
 45    )
 46    flow_invalidation = models.ForeignKey(
 47        Flow, null=True, on_delete=models.SET_NULL, related_name="brand_invalidation"
 48    )
 49    flow_recovery = models.ForeignKey(
 50        Flow, null=True, on_delete=models.SET_NULL, related_name="brand_recovery"
 51    )
 52    flow_unenrollment = models.ForeignKey(
 53        Flow, null=True, on_delete=models.SET_NULL, related_name="brand_unenrollment"
 54    )
 55    flow_user_settings = models.ForeignKey(
 56        Flow, null=True, on_delete=models.SET_NULL, related_name="brand_user_settings"
 57    )
 58    flow_device_code = models.ForeignKey(
 59        Flow, null=True, on_delete=models.SET_NULL, related_name="brand_device_code"
 60    )
 61    flow_lockdown = models.ForeignKey(
 62        Flow, null=True, on_delete=models.SET_NULL, related_name="brand_lockdown"
 63    )
 64
 65    default_application = models.ForeignKey(
 66        "authentik_core.Application",
 67        null=True,
 68        default=None,
 69        on_delete=models.SET_DEFAULT,
 70        help_text=_(
 71            "When set, external users will be redirected to this application after authenticating."
 72        ),
 73    )
 74
 75    web_certificate = models.ForeignKey(
 76        CertificateKeyPair,
 77        null=True,
 78        default=None,
 79        on_delete=models.SET_DEFAULT,
 80        help_text=_("Web Certificate used by the authentik Core webserver."),
 81        related_name="+",
 82    )
 83    client_certificates = models.ManyToManyField(
 84        CertificateKeyPair,
 85        default=None,
 86        blank=True,
 87        help_text=_("Certificates used for client authentication."),
 88    )
 89    attributes = models.JSONField(default=dict, blank=True)
 90
 91    def branding_logo_url(self) -> str:
 92        """Get branding_logo URL"""
 93        return get_file_manager(FileUsage.MEDIA).file_url(self.branding_logo)
 94
 95    def branding_logo_themed_urls(self) -> dict[str, str] | None:
 96        """Get themed URLs for branding_logo if it contains %(theme)s"""
 97        return get_file_manager(FileUsage.MEDIA).themed_urls(self.branding_logo)
 98
 99    def branding_favicon_url(self) -> str:
100        """Get branding_favicon URL"""
101        return get_file_manager(FileUsage.MEDIA).file_url(self.branding_favicon)
102
103    def branding_favicon_themed_urls(self) -> dict[str, str] | None:
104        """Get themed URLs for branding_favicon if it contains %(theme)s"""
105        return get_file_manager(FileUsage.MEDIA).themed_urls(self.branding_favicon)
106
107    def branding_default_flow_background_url(self, request=None, use_cache: bool = True) -> str:
108        """Get branding_default_flow_background URL"""
109        return get_file_manager(FileUsage.MEDIA).file_url(
110            self.branding_default_flow_background,
111            request,
112            use_cache=use_cache,
113        )
114
115    def branding_default_flow_background_themed_urls(
116        self, request=None, use_cache: bool = True
117    ) -> dict[str, str] | None:
118        """Get themed URLs for branding_default_flow_background if it contains %(theme)s"""
119        return get_file_manager(FileUsage.MEDIA).themed_urls(
120            self.branding_default_flow_background,
121            request,
122            use_cache=use_cache,
123        )
124
125    @property
126    def serializer(self) -> type[Serializer]:
127        from authentik.brands.api import BrandSerializer
128
129        return BrandSerializer
130
131    @property
132    def default_locale(self) -> str:
133        """Get default locale"""
134        try:
135            return self.attributes.get("settings", {}).get("locale", "")
136
137        except Exception as exc:  # noqa
138            LOGGER.warning("Failed to get default locale", exc=exc)
139            return ""
140
141    def __str__(self) -> str:
142        if self.default:
143            return "Default brand"
144        return f"Brand {self.domain}"
145
146    class Meta:
147        verbose_name = _("Brand")
148        verbose_name_plural = _("Brands")
149        indexes = [
150            models.Index(fields=["domain"]),
151            models.Index(fields=["default"]),
152        ]
153
154
155class WebfingerProvider(models.Model):
156    """Provider which supports webfinger discovery"""
157
158    class Meta:
159        abstract = True
160
161    def webfinger(self, resource: str, request: HttpRequest) -> dict:
162        raise NotImplementedError()
LOGGER = <BoundLoggerLazyProxy(logger=None, wrapper_class=None, processors=None, context_class=None, initial_values={}, logger_factory_args=())>
class Brand(authentik.lib.models.SerializerModel):
 22class Brand(SerializerModel):
 23    """Single brand"""
 24
 25    brand_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
 26    domain = models.TextField(
 27        help_text=_(
 28            "Domain that activates this brand. Can be a superset, i.e. `a.b` for `aa.b` and `ba.b`"
 29        )
 30    )
 31    default = models.BooleanField(
 32        default=False,
 33    )
 34
 35    branding_title = models.TextField(default="authentik")
 36
 37    branding_logo = FileField(default="/static/dist/assets/icons/icon_left_brand.svg")
 38    branding_favicon = FileField(default="/static/dist/assets/icons/icon.png")
 39    branding_custom_css = models.TextField(default="", blank=True)
 40    branding_default_flow_background = FileField(
 41        default="/static/dist/assets/images/flow_background.jpg",
 42    )
 43
 44    flow_authentication = models.ForeignKey(
 45        Flow, null=True, on_delete=models.SET_NULL, related_name="brand_authentication"
 46    )
 47    flow_invalidation = models.ForeignKey(
 48        Flow, null=True, on_delete=models.SET_NULL, related_name="brand_invalidation"
 49    )
 50    flow_recovery = models.ForeignKey(
 51        Flow, null=True, on_delete=models.SET_NULL, related_name="brand_recovery"
 52    )
 53    flow_unenrollment = models.ForeignKey(
 54        Flow, null=True, on_delete=models.SET_NULL, related_name="brand_unenrollment"
 55    )
 56    flow_user_settings = models.ForeignKey(
 57        Flow, null=True, on_delete=models.SET_NULL, related_name="brand_user_settings"
 58    )
 59    flow_device_code = models.ForeignKey(
 60        Flow, null=True, on_delete=models.SET_NULL, related_name="brand_device_code"
 61    )
 62    flow_lockdown = models.ForeignKey(
 63        Flow, null=True, on_delete=models.SET_NULL, related_name="brand_lockdown"
 64    )
 65
 66    default_application = models.ForeignKey(
 67        "authentik_core.Application",
 68        null=True,
 69        default=None,
 70        on_delete=models.SET_DEFAULT,
 71        help_text=_(
 72            "When set, external users will be redirected to this application after authenticating."
 73        ),
 74    )
 75
 76    web_certificate = models.ForeignKey(
 77        CertificateKeyPair,
 78        null=True,
 79        default=None,
 80        on_delete=models.SET_DEFAULT,
 81        help_text=_("Web Certificate used by the authentik Core webserver."),
 82        related_name="+",
 83    )
 84    client_certificates = models.ManyToManyField(
 85        CertificateKeyPair,
 86        default=None,
 87        blank=True,
 88        help_text=_("Certificates used for client authentication."),
 89    )
 90    attributes = models.JSONField(default=dict, blank=True)
 91
 92    def branding_logo_url(self) -> str:
 93        """Get branding_logo URL"""
 94        return get_file_manager(FileUsage.MEDIA).file_url(self.branding_logo)
 95
 96    def branding_logo_themed_urls(self) -> dict[str, str] | None:
 97        """Get themed URLs for branding_logo if it contains %(theme)s"""
 98        return get_file_manager(FileUsage.MEDIA).themed_urls(self.branding_logo)
 99
100    def branding_favicon_url(self) -> str:
101        """Get branding_favicon URL"""
102        return get_file_manager(FileUsage.MEDIA).file_url(self.branding_favicon)
103
104    def branding_favicon_themed_urls(self) -> dict[str, str] | None:
105        """Get themed URLs for branding_favicon if it contains %(theme)s"""
106        return get_file_manager(FileUsage.MEDIA).themed_urls(self.branding_favicon)
107
108    def branding_default_flow_background_url(self, request=None, use_cache: bool = True) -> str:
109        """Get branding_default_flow_background URL"""
110        return get_file_manager(FileUsage.MEDIA).file_url(
111            self.branding_default_flow_background,
112            request,
113            use_cache=use_cache,
114        )
115
116    def branding_default_flow_background_themed_urls(
117        self, request=None, use_cache: bool = True
118    ) -> dict[str, str] | None:
119        """Get themed URLs for branding_default_flow_background if it contains %(theme)s"""
120        return get_file_manager(FileUsage.MEDIA).themed_urls(
121            self.branding_default_flow_background,
122            request,
123            use_cache=use_cache,
124        )
125
126    @property
127    def serializer(self) -> type[Serializer]:
128        from authentik.brands.api import BrandSerializer
129
130        return BrandSerializer
131
132    @property
133    def default_locale(self) -> str:
134        """Get default locale"""
135        try:
136            return self.attributes.get("settings", {}).get("locale", "")
137
138        except Exception as exc:  # noqa
139            LOGGER.warning("Failed to get default locale", exc=exc)
140            return ""
141
142    def __str__(self) -> str:
143        if self.default:
144            return "Default brand"
145        return f"Brand {self.domain}"
146
147    class Meta:
148        verbose_name = _("Brand")
149        verbose_name_plural = _("Brands")
150        indexes = [
151            models.Index(fields=["domain"]),
152            models.Index(fields=["default"]),
153        ]

Single brand

def brand_uuid(unknown):

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

def domain(unknown):

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

def default(unknown):

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

def branding_title(unknown):

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

def branding_favicon(unknown):

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

def branding_custom_css(unknown):

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

def branding_default_flow_background(unknown):

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

flow_authentication

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.

flow_invalidation

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.

flow_recovery

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.

flow_unenrollment

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.

flow_user_settings

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.

flow_device_code

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.

flow_lockdown

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.

default_application

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.

web_certificate

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.

client_certificates

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

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

def branding_logo_url(self) -> str:
92    def branding_logo_url(self) -> str:
93        """Get branding_logo URL"""
94        return get_file_manager(FileUsage.MEDIA).file_url(self.branding_logo)

Get branding_logo URL

def branding_logo_themed_urls(self) -> dict[str, str] | None:
96    def branding_logo_themed_urls(self) -> dict[str, str] | None:
97        """Get themed URLs for branding_logo if it contains %(theme)s"""
98        return get_file_manager(FileUsage.MEDIA).themed_urls(self.branding_logo)

Get themed URLs for branding_logo if it contains %(theme)s

def branding_favicon_url(self) -> str:
100    def branding_favicon_url(self) -> str:
101        """Get branding_favicon URL"""
102        return get_file_manager(FileUsage.MEDIA).file_url(self.branding_favicon)

Get branding_favicon URL

def branding_favicon_themed_urls(self) -> dict[str, str] | None:
104    def branding_favicon_themed_urls(self) -> dict[str, str] | None:
105        """Get themed URLs for branding_favicon if it contains %(theme)s"""
106        return get_file_manager(FileUsage.MEDIA).themed_urls(self.branding_favicon)

Get themed URLs for branding_favicon if it contains %(theme)s

def branding_default_flow_background_url(self, request=None, use_cache: bool = True) -> str:
108    def branding_default_flow_background_url(self, request=None, use_cache: bool = True) -> str:
109        """Get branding_default_flow_background URL"""
110        return get_file_manager(FileUsage.MEDIA).file_url(
111            self.branding_default_flow_background,
112            request,
113            use_cache=use_cache,
114        )

Get branding_default_flow_background URL

def branding_default_flow_background_themed_urls(self, request=None, use_cache: bool = True) -> dict[str, str] | None:
116    def branding_default_flow_background_themed_urls(
117        self, request=None, use_cache: bool = True
118    ) -> dict[str, str] | None:
119        """Get themed URLs for branding_default_flow_background if it contains %(theme)s"""
120        return get_file_manager(FileUsage.MEDIA).themed_urls(
121            self.branding_default_flow_background,
122            request,
123            use_cache=use_cache,
124        )

Get themed URLs for branding_default_flow_background if it contains %(theme)s

serializer: type[rest_framework.serializers.Serializer]
126    @property
127    def serializer(self) -> type[Serializer]:
128        from authentik.brands.api import BrandSerializer
129
130        return BrandSerializer

Get serializer for this model

default_locale: str
132    @property
133    def default_locale(self) -> str:
134        """Get default locale"""
135        try:
136            return self.attributes.get("settings", {}).get("locale", "")
137
138        except Exception as exc:  # noqa
139            LOGGER.warning("Failed to get default locale", exc=exc)
140            return ""

Get default locale

flow_authentication_id
flow_invalidation_id
flow_recovery_id
flow_unenrollment_id
flow_user_settings_id
flow_device_code_id
flow_lockdown_id
default_application_id
web_certificate_id
def objects(unknown):

The type of the None singleton.

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

The requested object does not exist

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

The query returned multiple objects when only one was expected.

class WebfingerProvider(django.db.models.base.Model):
156class WebfingerProvider(models.Model):
157    """Provider which supports webfinger discovery"""
158
159    class Meta:
160        abstract = True
161
162    def webfinger(self, resource: str, request: HttpRequest) -> dict:
163        raise NotImplementedError()

Provider which supports webfinger discovery

def webfinger(self, resource: str, request: django.http.request.HttpRequest) -> dict:
162    def webfinger(self, resource: str, request: HttpRequest) -> dict:
163        raise NotImplementedError()
class WebfingerProvider.Meta:
159    class Meta:
160        abstract = True
abstract = False