stages/authenticator_static: migrate to web

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-04-03 01:03:43 +02:00
parent ab7f4c5ba2
commit 995f3a13d1
3 changed files with 93 additions and 23 deletions

View File

@ -1,17 +0,0 @@
"""Static Authenticator forms"""
from django import forms
from authentik.stages.authenticator_static.models import AuthenticatorStaticStage
class AuthenticatorStaticStageForm(forms.ModelForm):
"""Static Authenticator Stage setup form"""
class Meta:
model = AuthenticatorStaticStage
fields = ["name", "configure_flow", "token_count"]
widgets = {
"name": forms.TextInput(),
}

View File

@ -33,12 +33,8 @@ class AuthenticatorStaticStage(ConfigurableStage, Stage):
return AuthenticatorStaticStageView return AuthenticatorStaticStageView
@property @property
def form(self) -> Type[ModelForm]: def component(self) -> str:
from authentik.stages.authenticator_static.forms import ( return "ak-stage-authenticator-static-form"
AuthenticatorStaticStageForm,
)
return AuthenticatorStaticStageForm
@property @property
def ui_user_settings(self) -> Optional[UserSettingSerializer]: def ui_user_settings(self) -> Optional[UserSettingSerializer]:

View File

@ -0,0 +1,91 @@
import { FlowDesignationEnum, FlowsApi, AuthenticatorStaticStage, 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";
import "../../../elements/forms/FormGroup";
import { until } from "lit-html/directives/until";
@customElement("ak-stage-authenticator-static-form")
export class AuthenticatorStaticStageForm extends Form<AuthenticatorStaticStage> {
set stageUUID(value: string) {
new StagesApi(DEFAULT_CONFIG).stagesAuthenticatorStaticRead({
stageUuid: value,
}).then(stage => {
this.stage = stage;
});
}
@property({attribute: false})
stage?: AuthenticatorStaticStage;
getSuccessMessage(): string {
if (this.stage) {
return gettext("Successfully updated stage.");
} else {
return gettext("Successfully created stage.");
}
}
send = (data: AuthenticatorStaticStage): Promise<AuthenticatorStaticStage> => {
if (this.stage) {
return new StagesApi(DEFAULT_CONFIG).stagesAuthenticatorStaticUpdate({
stageUuid: this.stage.pk || "",
data: data
});
} else {
return new StagesApi(DEFAULT_CONFIG).stagesUserWriteCreate({
data: data
});
}
};
renderForm(): TemplateResult {
return html`<form class="pf-c-form pf-m-horizontal">
<ak-form-element-horizontal
label=${gettext("Name")}
?required=${true}
name="name">
<input type="text" value="${ifDefined(this.stage?.name || "")}" class="pf-c-form-control" required>
</ak-form-element-horizontal>
<ak-form-group .expanded=${true}>
<span slot="header">
${gettext("Stage-specific settings")}
</span>
<div slot="body" class="pf-c-form">
<ak-form-element-horizontal
label=${gettext("Token count")}
?required=${true}
name="tokenCount">
<input type="text" value="${ifDefined(this.stage?.tokenCount || 6)}" class="pf-c-form-control" required>
</ak-form-element-horizontal>
<ak-form-element-horizontal
label=${gettext("Configuration flow")}
name="configureFlow">
<select class="pf-c-form-control">
<option value="" ?selected=${this.stage?.configureFlow === undefined}>---------</option>
${until(new FlowsApi(DEFAULT_CONFIG).flowsInstancesList({
ordering: "pk",
designation: FlowDesignationEnum.StageConfiguration,
}).then(flows => {
return flows.results.map(flow => {
let selected = this.stage?.configureFlow === flow.pk;
if (!this.stage?.configureFlow && flow.slug === "default-otp-time-configure") {
selected = true;
}
return html`<option value=${ifDefined(flow.pk)} ?selected=${selected}>${flow.name} (${flow.slug})</option>`;
});
}))}
</select>
<p class="pf-c-form__helper-text">${gettext("Flow used by an authenticated user to configure this Stage. If empty, user will not be able to configure this stage.")}</p>
</ak-form-element-horizontal>
</div>
</ak-form-group>
</form>`;
}
}