From 0b057ccb348a046a183c4e921a304bd09d359414 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sat, 3 Apr 2021 01:06:41 +0200 Subject: [PATCH] stages/authenticator_webauthn: migrate to web Signed-off-by: Jens Langhammer --- .../stages/authenticator_static/models.py | 1 - authentik/stages/authenticator_totp/models.py | 1 - .../stages/authenticator_webauthn/forms.py | 33 ----------- .../stages/authenticator_webauthn/models.py | 9 +-- authentik/stages/deny/models.py | 1 - authentik/stages/dummy/models.py | 1 - authentik/stages/password/models.py | 1 - .../AuthenticateWebAuthnStageForm.ts | 56 +++++++++++++++++++ 8 files changed, 58 insertions(+), 45 deletions(-) delete mode 100644 authentik/stages/authenticator_webauthn/forms.py create mode 100644 web/src/pages/stages/authenticator_webauthn/AuthenticateWebAuthnStageForm.ts diff --git a/authentik/stages/authenticator_static/models.py b/authentik/stages/authenticator_static/models.py index 63bff7e47..125abb02f 100644 --- a/authentik/stages/authenticator_static/models.py +++ b/authentik/stages/authenticator_static/models.py @@ -2,7 +2,6 @@ from typing import Optional, Type from django.db import models -from django.forms import ModelForm from django.utils.translation import gettext_lazy as _ from django.views import View from rest_framework.serializers import BaseSerializer diff --git a/authentik/stages/authenticator_totp/models.py b/authentik/stages/authenticator_totp/models.py index c7ec6e62f..daf69d015 100644 --- a/authentik/stages/authenticator_totp/models.py +++ b/authentik/stages/authenticator_totp/models.py @@ -2,7 +2,6 @@ from typing import Optional, Type from django.db import models -from django.forms import ModelForm from django.utils.translation import gettext_lazy as _ from django.views import View from rest_framework.serializers import BaseSerializer diff --git a/authentik/stages/authenticator_webauthn/forms.py b/authentik/stages/authenticator_webauthn/forms.py deleted file mode 100644 index 881bf54d7..000000000 --- a/authentik/stages/authenticator_webauthn/forms.py +++ /dev/null @@ -1,33 +0,0 @@ -"""Webauthn stage forms""" -from django import forms - -from authentik.stages.authenticator_webauthn.models import ( - AuthenticateWebAuthnStage, - WebAuthnDevice, -) - - -class AuthenticateWebAuthnStageForm(forms.ModelForm): - """OTP Time-based Stage setup form""" - - class Meta: - - model = AuthenticateWebAuthnStage - fields = ["name"] - - widgets = { - "name": forms.TextInput(), - } - - -class DeviceEditForm(forms.ModelForm): - """Form to edit webauthn device""" - - class Meta: - - model = WebAuthnDevice - fields = ["name"] - - widgets = { - "name": forms.TextInput(), - } diff --git a/authentik/stages/authenticator_webauthn/models.py b/authentik/stages/authenticator_webauthn/models.py index fdae8dd28..8358653fc 100644 --- a/authentik/stages/authenticator_webauthn/models.py +++ b/authentik/stages/authenticator_webauthn/models.py @@ -3,7 +3,6 @@ from typing import Optional, Type from django.contrib.auth import get_user_model from django.db import models -from django.forms import ModelForm from django.utils.timezone import now from django.utils.translation import gettext_lazy as _ from django.views import View @@ -34,12 +33,8 @@ class AuthenticateWebAuthnStage(ConfigurableStage, Stage): return AuthenticatorWebAuthnStageView @property - def form(self) -> Type[ModelForm]: - from authentik.stages.authenticator_webauthn.forms import ( - AuthenticateWebAuthnStageForm, - ) - - return AuthenticateWebAuthnStageForm + def component(self) -> str: + return "ak-stage-authenticator-webauthn-form" @property def ui_user_settings(self) -> Optional[UserSettingSerializer]: diff --git a/authentik/stages/deny/models.py b/authentik/stages/deny/models.py index b123674f3..b403679a0 100644 --- a/authentik/stages/deny/models.py +++ b/authentik/stages/deny/models.py @@ -1,7 +1,6 @@ """deny stage models""" from typing import Type -from django.forms import ModelForm from django.utils.translation import gettext_lazy as _ from django.views import View from rest_framework.serializers import BaseSerializer diff --git a/authentik/stages/dummy/models.py b/authentik/stages/dummy/models.py index 2790ae3a5..2e19ec22a 100644 --- a/authentik/stages/dummy/models.py +++ b/authentik/stages/dummy/models.py @@ -1,7 +1,6 @@ """dummy stage models""" from typing import Type -from django.forms import ModelForm from django.utils.translation import gettext as _ from django.views import View from rest_framework.serializers import BaseSerializer diff --git a/authentik/stages/password/models.py b/authentik/stages/password/models.py index c3db3b174..a4e14e51f 100644 --- a/authentik/stages/password/models.py +++ b/authentik/stages/password/models.py @@ -3,7 +3,6 @@ from typing import Optional, Type from django.contrib.postgres.fields import ArrayField from django.db import models -from django.forms import ModelForm from django.utils.translation import gettext_lazy as _ from django.views import View from rest_framework.serializers import BaseSerializer diff --git a/web/src/pages/stages/authenticator_webauthn/AuthenticateWebAuthnStageForm.ts b/web/src/pages/stages/authenticator_webauthn/AuthenticateWebAuthnStageForm.ts new file mode 100644 index 000000000..827aee511 --- /dev/null +++ b/web/src/pages/stages/authenticator_webauthn/AuthenticateWebAuthnStageForm.ts @@ -0,0 +1,56 @@ +import { AuthenticateWebAuthnStage, StagesApi } from "authentik-api"; +import { gettext } from "django"; +import { customElement, property } from "lit-element"; +import { html, TemplateResult } from "lit-html"; +import { DEFAULT_CONFIG } from "../../../api/Config"; +import { Form } from "../../../elements/forms/Form"; +import { ifDefined } from "lit-html/directives/if-defined"; +import "../../../elements/forms/HorizontalFormElement"; + +@customElement("ak-stage-authenticator-webauthn-form") +export class AuthenticateWebAuthnStageForm extends Form { + + set stageUUID(value: string) { + new StagesApi(DEFAULT_CONFIG).stagesAuthenticatorWebauthnRead({ + stageUuid: value, + }).then(stage => { + this.stage = stage; + }); + } + + @property({attribute: false}) + stage?: AuthenticateWebAuthnStage; + + getSuccessMessage(): string { + if (this.stage) { + return gettext("Successfully updated stage."); + } else { + return gettext("Successfully created stage."); + } + } + + send = (data: AuthenticateWebAuthnStage): Promise => { + if (this.stage) { + return new StagesApi(DEFAULT_CONFIG).stagesAuthenticatorWebauthnUpdate({ + stageUuid: this.stage.pk || "", + data: data + }); + } else { + return new StagesApi(DEFAULT_CONFIG).stagesAuthenticatorWebauthnCreate({ + data: data + }); + } + }; + + renderForm(): TemplateResult { + return html`
+ + + +
`; + } + +}