Skip to content

Commit

Permalink
enterprise: add support for license flags (#10842)
Browse files Browse the repository at this point in the history
Signed-off-by: Jens Langhammer <[email protected]>
  • Loading branch information
BeryJu authored Aug 9, 2024
1 parent 25a0671 commit a073b7a
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 38 deletions.
4 changes: 1 addition & 3 deletions authentik/enterprise/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Enterprise API Views"""

from dataclasses import asdict
from datetime import timedelta

from django.utils.timezone import now
Expand Down Expand Up @@ -104,8 +103,7 @@ def get_install_id(self, request: Request) -> Response:
@action(detail=False, methods=["GET"], permission_classes=[IsAuthenticated])
def summary(self, request: Request) -> Response:
"""Get the total license status"""
response = LicenseSummarySerializer(data=asdict(LicenseKey.cached_summary()))
response.is_valid(raise_exception=True)
response = LicenseSummarySerializer(instance=LicenseKey.cached_summary())
return Response(response.data)

@permission_required(None, ["authentik_enterprise.view_license"])
Expand Down
9 changes: 7 additions & 2 deletions authentik/enterprise/license.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
ChoiceField,
DateTimeField,
IntegerField,
ListField,
)

from authentik.core.api.utils import PassiveSerializer
Expand Down Expand Up @@ -55,6 +56,7 @@ class LicenseFlags(Enum):
"""License flags"""

TRIAL = "trial"
NON_PRODUCTION = "non_production"


@dataclass
Expand All @@ -65,6 +67,7 @@ class LicenseSummary:
external_users: int
status: LicenseUsageStatus
latest_valid: datetime
license_flags: list[LicenseFlags]


class LicenseSummarySerializer(PassiveSerializer):
Expand All @@ -74,6 +77,7 @@ class LicenseSummarySerializer(PassiveSerializer):
external_users = IntegerField(required=True)
status = ChoiceField(choices=LicenseUsageStatus.choices)
latest_valid = DateTimeField()
license_flags = ListField(child=ChoiceField(choices=tuple(x.value for x in LicenseFlags)))


@dataclass
Expand All @@ -86,7 +90,7 @@ class LicenseKey:
name: str
internal_users: int = 0
external_users: int = 0
flags: list[LicenseFlags] = field(default_factory=list)
license_flags: list[LicenseFlags] = field(default_factory=list)

@staticmethod
def validate(jwt: str, check_expiry=True) -> "LicenseKey":
Expand Down Expand Up @@ -132,7 +136,7 @@ def get_total() -> "LicenseKey":
total.exp = exp_ts
if exp_ts <= total.exp:
total.exp = exp_ts
total.flags.extend(lic.status.flags)
total.license_flags.extend(lic.status.license_flags)
return total

@staticmethod
Expand Down Expand Up @@ -216,6 +220,7 @@ def summary(self) -> LicenseSummary:
internal_users=self.internal_users,
external_users=self.external_users,
status=status,
license_flags=self.license_flags,
)

@staticmethod
Expand Down
8 changes: 0 additions & 8 deletions blueprints/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6357,13 +6357,9 @@
"authentik_sources_plex.change_plexsourcepropertymapping",
"authentik_sources_plex.delete_plexsourcepropertymapping",
"authentik_sources_plex.view_plexsourcepropertymapping",
"authentik_sources_plex.add_plexsourceconnection",
"authentik_sources_plex.add_userplexsourceconnection",
"authentik_sources_plex.change_plexsourceconnection",
"authentik_sources_plex.change_userplexsourceconnection",
"authentik_sources_plex.delete_plexsourceconnection",
"authentik_sources_plex.delete_userplexsourceconnection",
"authentik_sources_plex.view_plexsourceconnection",
"authentik_sources_plex.view_userplexsourceconnection",
"authentik_sources_saml.add_groupsamlsourceconnection",
"authentik_sources_saml.change_groupsamlsourceconnection",
Expand Down Expand Up @@ -12016,13 +12012,9 @@
"authentik_sources_plex.change_plexsourcepropertymapping",
"authentik_sources_plex.delete_plexsourcepropertymapping",
"authentik_sources_plex.view_plexsourcepropertymapping",
"authentik_sources_plex.add_plexsourceconnection",
"authentik_sources_plex.add_userplexsourceconnection",
"authentik_sources_plex.change_plexsourceconnection",
"authentik_sources_plex.change_userplexsourceconnection",
"authentik_sources_plex.delete_plexsourceconnection",
"authentik_sources_plex.delete_userplexsourceconnection",
"authentik_sources_plex.view_plexsourceconnection",
"authentik_sources_plex.view_userplexsourceconnection",
"authentik_sources_saml.add_groupsamlsourceconnection",
"authentik_sources_saml.change_groupsamlsourceconnection",
Expand Down
10 changes: 10 additions & 0 deletions schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41352,6 +41352,11 @@ components:
- key
- license_uuid
- name
LicenseFlagsEnum:
enum:
- trial
- non_production
type: string
LicenseForecast:
type: object
description: Serializer for license forecast
Expand Down Expand Up @@ -41391,10 +41396,15 @@ components:
latest_valid:
type: string
format: date-time
license_flags:
type: array
items:
$ref: '#/components/schemas/LicenseFlagsEnum'
required:
- external_users
- internal_users
- latest_valid
- license_flags
- status
LicenseSummaryStatusEnum:
enum:
Expand Down
3 changes: 2 additions & 1 deletion web/src/admin/enterprise/EnterpriseLicenseListPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ export class EnterpriseLicenseListPage extends TablePage<License> {
header=${msg("Expiry")}
subtext=${msg("Cumulative license expiry")}
>
${this.summary?.status === LicenseSummaryStatusEnum.Unlicensed
${this.summary &&
this.summary?.status !== LicenseSummaryStatusEnum.Unlicensed
? html`<div>${getRelativeTime(this.summary.latestValid)}</div>
<small>${this.summary.latestValid.toLocaleString()}</small>`
: "-"}
Expand Down
68 changes: 44 additions & 24 deletions web/src/elements/enterprise/EnterpriseStatusBanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,45 @@ import { AKElement } from "@goauthentik/elements/Base";
import { WithLicenseSummary } from "@goauthentik/elements/Interface/licenseSummaryProvider";

import { msg } from "@lit/localize";
import { CSSResult, TemplateResult, html } from "lit";
import { CSSResult, TemplateResult, html, nothing } from "lit";
import { customElement, property } from "lit/decorators.js";

import PFBanner from "@patternfly/patternfly/components/Banner/banner.css";

import { LicenseSummaryStatusEnum } from "@goauthentik/api";
import { LicenseFlagsEnum, LicenseSummaryStatusEnum } from "@goauthentik/api";

@customElement("ak-enterprise-status")
export class EnterpriseStatusBanner extends WithLicenseSummary(AKElement) {
@property()
interface: "admin" | "user" | "" = "";
interface: "admin" | "user" | "flow" | "" = "";

static get styles(): CSSResult[] {
return [PFBanner];
}

renderBanner(): TemplateResult {
renderStatusBanner() {
// Check if we're in the correct interface to render a banner
switch (this.licenseSummary.status) {
// user warning is both on admin interface and user interface
case LicenseSummaryStatusEnum.LimitExceededUser:
if (
this.interface.toLowerCase() !== "user" &&
this.interface.toLowerCase() !== "admin"
) {
return nothing;
}
break;
case LicenseSummaryStatusEnum.ExpirySoon:
case LicenseSummaryStatusEnum.Expired:
case LicenseSummaryStatusEnum.LimitExceededAdmin:
if (this.interface.toLowerCase() !== "admin") {
return nothing;
}
break;
case LicenseSummaryStatusEnum.ReadOnly:
default:
break;
}
let message = "";
switch (this.licenseSummary.status) {
case LicenseSummaryStatusEnum.LimitExceededAdmin:
Expand All @@ -44,7 +66,8 @@ export class EnterpriseStatusBanner extends WithLicenseSummary(AKElement) {
break;
}
return html`<div
class="pf-c-banner ${this.licenseSummary?.status === LicenseSummaryStatusEnum.ReadOnly
class="pf-c-banner pf-m-sticky ${this.licenseSummary?.status ===
LicenseSummaryStatusEnum.ReadOnly
? "pf-m-red"
: "pf-m-gold"}"
>
Expand All @@ -53,26 +76,23 @@ export class EnterpriseStatusBanner extends WithLicenseSummary(AKElement) {
</div>`;
}

renderFlagBanner(): TemplateResult {
return html`
${this.licenseSummary.licenseFlags.includes(LicenseFlagsEnum.Trial)
? html`<div class="pf-c-banner pf-m-sticky pf-m-gold">
${msg("This authentik instance uses a Trial license.")}
</div>`
: nothing}
${this.licenseSummary.licenseFlags.includes(LicenseFlagsEnum.NonProduction)
? html`<div class="pf-c-banner pf-m-sticky pf-m-gold">
${msg("This authentik instance uses a Non-production license.")}
</div>`
: nothing}
`;
}

render(): TemplateResult {
switch (this.licenseSummary.status) {
case LicenseSummaryStatusEnum.LimitExceededUser:
if (this.interface.toLowerCase() === "user") {
return this.renderBanner();
}
break;
case LicenseSummaryStatusEnum.ExpirySoon:
case LicenseSummaryStatusEnum.Expired:
case LicenseSummaryStatusEnum.LimitExceededAdmin:
if (this.interface.toLowerCase() === "admin") {
return this.renderBanner();
}
break;
case LicenseSummaryStatusEnum.ReadOnly:
return this.renderBanner();
default:
break;
}
return html``;
return html`${this.renderFlagBanner()}${this.renderStatusBanner()}`;
}
}

Expand Down

0 comments on commit a073b7a

Please sign in to comment.