authentik.enterprise.models

Enterprise models

 1"""Enterprise models"""
 2
 3from datetime import timedelta
 4from typing import TYPE_CHECKING
 5from uuid import uuid4
 6
 7from django.contrib.postgres.indexes import HashIndex
 8from django.db import models
 9from django.utils.timezone import now
10from django.utils.translation import gettext_lazy as _
11from rest_framework.serializers import BaseSerializer
12
13from authentik.lib.models import ExpiringModel, InternallyManagedMixin, SerializerModel
14
15if TYPE_CHECKING:
16    from authentik.enterprise.license import LicenseKey
17
18
19def usage_expiry():
20    """Keep license usage records for 3 months"""
21    return now() + timedelta(days=30 * 3)
22
23
24THRESHOLD_WARNING_ADMIN_WEEKS = 2
25THRESHOLD_WARNING_USER_WEEKS = 4
26THRESHOLD_WARNING_EXPIRY_WEEKS = 2
27THRESHOLD_READ_ONLY_WEEKS = 6
28
29
30class License(SerializerModel):
31    """An authentik enterprise license"""
32
33    license_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
34    key = models.TextField()
35
36    name = models.TextField()
37    expiry = models.DateTimeField()
38    internal_users = models.BigIntegerField()
39    external_users = models.BigIntegerField()
40
41    @property
42    def is_valid(self) -> bool:
43        return self.expiry >= now()
44
45    @property
46    def serializer(self) -> type[BaseSerializer]:
47        from authentik.enterprise.api import LicenseSerializer
48
49        return LicenseSerializer
50
51    @property
52    def status(self) -> LicenseKey:
53        """Get parsed license status"""
54        from authentik.enterprise.license import LicenseKey
55
56        return LicenseKey.validate(self.key, check_expiry=False)
57
58    class Meta:
59        indexes = (HashIndex(fields=("key",)),)
60        verbose_name = _("License")
61        verbose_name_plural = _("Licenses")
62
63
64class LicenseUsageStatus(models.TextChoices):
65    """License states an instance can be in"""
66
67    UNLICENSED = "unlicensed"
68    VALID = "valid"
69    EXPIRED = "expired"
70    EXPIRY_SOON = "expiry_soon"
71    # User limit exceeded, 2 week threshold, show message in admin interface
72    LIMIT_EXCEEDED_ADMIN = "limit_exceeded_admin"
73    # User limit exceeded, 4 week threshold, show message in user interface
74    LIMIT_EXCEEDED_USER = "limit_exceeded_user"
75    READ_ONLY = "read_only"
76
77    @property
78    def is_valid(self) -> bool:
79        """Quickly check if a license is valid"""
80        return self in [LicenseUsageStatus.VALID, LicenseUsageStatus.EXPIRY_SOON]
81
82
83class LicenseUsage(InternallyManagedMixin, ExpiringModel):
84    """a single license usage record"""
85
86    expires = models.DateTimeField(default=usage_expiry)
87
88    usage_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
89
90    internal_user_count = models.BigIntegerField()
91    external_user_count = models.BigIntegerField()
92    status = models.TextField(choices=LicenseUsageStatus.choices)
93
94    record_date = models.DateTimeField(auto_now_add=True)
95
96    class Meta:
97        verbose_name = _("License Usage")
98        verbose_name_plural = _("License Usage Records")
99        indexes = ExpiringModel.Meta.indexes
def usage_expiry():
20def usage_expiry():
21    """Keep license usage records for 3 months"""
22    return now() + timedelta(days=30 * 3)

Keep license usage records for 3 months

THRESHOLD_WARNING_ADMIN_WEEKS = 2
THRESHOLD_WARNING_USER_WEEKS = 4
THRESHOLD_WARNING_EXPIRY_WEEKS = 2
THRESHOLD_READ_ONLY_WEEKS = 6
class License(authentik.lib.models.SerializerModel):
31class License(SerializerModel):
32    """An authentik enterprise license"""
33
34    license_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
35    key = models.TextField()
36
37    name = models.TextField()
38    expiry = models.DateTimeField()
39    internal_users = models.BigIntegerField()
40    external_users = models.BigIntegerField()
41
42    @property
43    def is_valid(self) -> bool:
44        return self.expiry >= now()
45
46    @property
47    def serializer(self) -> type[BaseSerializer]:
48        from authentik.enterprise.api import LicenseSerializer
49
50        return LicenseSerializer
51
52    @property
53    def status(self) -> LicenseKey:
54        """Get parsed license status"""
55        from authentik.enterprise.license import LicenseKey
56
57        return LicenseKey.validate(self.key, check_expiry=False)
58
59    class Meta:
60        indexes = (HashIndex(fields=("key",)),)
61        verbose_name = _("License")
62        verbose_name_plural = _("Licenses")

An authentik enterprise license

def license_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 key(unknown):

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

def name(unknown):

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

def expiry(unknown):

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

def internal_users(unknown):

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

def external_users(unknown):

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

is_valid: bool
42    @property
43    def is_valid(self) -> bool:
44        return self.expiry >= now()
serializer: type[rest_framework.serializers.BaseSerializer]
46    @property
47    def serializer(self) -> type[BaseSerializer]:
48        from authentik.enterprise.api import LicenseSerializer
49
50        return LicenseSerializer

Get serializer for this model

status
52    @property
53    def status(self) -> LicenseKey:
54        """Get parsed license status"""
55        from authentik.enterprise.license import LicenseKey
56
57        return LicenseKey.validate(self.key, check_expiry=False)

Get parsed license status

def get_next_by_expiry(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_previous_by_expiry(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 objects(unknown):

The type of the None singleton.

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

The requested object does not exist

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

The query returned multiple objects when only one was expected.

class LicenseUsageStatus(django.db.models.enums.TextChoices):
65class LicenseUsageStatus(models.TextChoices):
66    """License states an instance can be in"""
67
68    UNLICENSED = "unlicensed"
69    VALID = "valid"
70    EXPIRED = "expired"
71    EXPIRY_SOON = "expiry_soon"
72    # User limit exceeded, 2 week threshold, show message in admin interface
73    LIMIT_EXCEEDED_ADMIN = "limit_exceeded_admin"
74    # User limit exceeded, 4 week threshold, show message in user interface
75    LIMIT_EXCEEDED_USER = "limit_exceeded_user"
76    READ_ONLY = "read_only"
77
78    @property
79    def is_valid(self) -> bool:
80        """Quickly check if a license is valid"""
81        return self in [LicenseUsageStatus.VALID, LicenseUsageStatus.EXPIRY_SOON]

License states an instance can be in

is_valid: bool
78    @property
79    def is_valid(self) -> bool:
80        """Quickly check if a license is valid"""
81        return self in [LicenseUsageStatus.VALID, LicenseUsageStatus.EXPIRY_SOON]

Quickly check if a license is valid

 84class LicenseUsage(InternallyManagedMixin, ExpiringModel):
 85    """a single license usage record"""
 86
 87    expires = models.DateTimeField(default=usage_expiry)
 88
 89    usage_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
 90
 91    internal_user_count = models.BigIntegerField()
 92    external_user_count = models.BigIntegerField()
 93    status = models.TextField(choices=LicenseUsageStatus.choices)
 94
 95    record_date = models.DateTimeField(auto_now_add=True)
 96
 97    class Meta:
 98        verbose_name = _("License Usage")
 99        verbose_name_plural = _("License Usage Records")
100        indexes = ExpiringModel.Meta.indexes

a single license usage record

def expires(unknown):

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

def usage_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 internal_user_count(unknown):

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

def external_user_count(unknown):

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

def status(unknown):

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

def record_date(unknown):

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

def expiring(unknown):

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

def get_next_by_expires(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_previous_by_expires(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_status_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_next_by_record_date(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_previous_by_record_date(unknown):

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

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

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

The requested object does not exist

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

The query returned multiple objects when only one was expected.