web/user: update form to update mfa devices

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer
2021-10-30 15:16:27 +02:00
parent f5dc81907a
commit 06766bdb25
5 changed files with 100 additions and 38 deletions

View File

@ -0,0 +1,75 @@
import { t } from "@lingui/macro";
import { TemplateResult, html } from "lit";
import { customElement } from "lit/decorators";
import { ifDefined } from "lit/directives/if-defined";
import { AuthenticatorsApi, Device } from "@goauthentik/api";
import { DEFAULT_CONFIG } from "../../../api/Config";
import "../../../elements/forms/HorizontalFormElement";
import { ModelForm } from "../../../elements/forms/ModelForm";
@customElement("ak-user-mfa-form")
export class MFADeviceForm extends ModelForm<Device, number> {
loadInstance(pk: number): Promise<Device> {
return new AuthenticatorsApi(DEFAULT_CONFIG).authenticatorsAllList().then((devices) => {
return devices.filter((device) => device.pk === pk)[0];
});
}
getSuccessMessage(): string {
return t`Successfully updated device.`;
}
send = async (device: Device): Promise<Device> => {
switch (this.instance?.type) {
case "authentik_stages_authenticator_duo.DuoDevice":
await new AuthenticatorsApi(DEFAULT_CONFIG).authenticatorsDuoUpdate({
id: this.instance?.pk,
duoDeviceRequest: device,
});
break;
case "authentik_stages_authenticator_sms.SMSDevice":
await new AuthenticatorsApi(DEFAULT_CONFIG).authenticatorsSmsUpdate({
id: this.instance?.pk,
sMSDeviceRequest: device,
});
break;
case "otp_totp.TOTPDevice":
await new AuthenticatorsApi(DEFAULT_CONFIG).authenticatorsTotpUpdate({
id: this.instance?.pk,
tOTPDeviceRequest: device,
});
break;
case "otp_static.StaticDevice":
await new AuthenticatorsApi(DEFAULT_CONFIG).authenticatorsStaticUpdate({
id: this.instance?.pk,
staticDeviceRequest: device,
});
break;
case "authentik_stages_authenticator_webauthn.WebAuthnDevice":
await new AuthenticatorsApi(DEFAULT_CONFIG).authenticatorsWebauthnUpdate({
id: this.instance?.pk,
webAuthnDeviceRequest: device,
});
break;
default:
break;
}
return device;
};
renderForm(): TemplateResult {
return html`<form class="pf-c-form pf-m-horizontal">
<ak-form-element-horizontal label=${t`Name`} ?required=${true} name="name">
<input
type="text"
value="${ifDefined(this.instance?.name)}"
class="pf-c-form-control"
required
/>
</ak-form-element-horizontal>
</form>`;
}
}

View File

@ -2,6 +2,7 @@ import { t } from "@lingui/macro";
import { TemplateResult, html } from "lit";
import { customElement, property } from "lit/decorators";
import { ifDefined } from "lit/directives/if-defined";
import { until } from "lit/directives/until";
import { AuthenticatorsApi, Device, UserSetting } from "@goauthentik/api";
@ -14,6 +15,7 @@ import "../../../elements/buttons/TokenCopyButton";
import "../../../elements/forms/DeleteBulkForm";
import "../../../elements/forms/ModalForm";
import { Table, TableColumn } from "../../../elements/table/Table";
import "./MFADeviceForm";
export function stageToAuthenticatorName(stage: UserSetting): string {
switch (stage.component) {
@ -76,7 +78,7 @@ export class MFADevicesPage extends Table<Device> {
.map((stage) => {
return html`<li>
<a
href="${stage.configureUrl}"
href="${ifDefined(stage.configureUrl)}"
class="pf-c-dropdown__menu-item"
>
${stageToAuthenticatorName(stage)}
@ -144,7 +146,7 @@ export class MFADevicesPage extends Table<Device> {
<ak-forms-modal>
<span slot="submit">${t`Update`}</span>
<span slot="header">${t`Update Device`}</span>
<ak-user-token-form slot="form" .instancePk=${item.pk}> </ak-user-token-form>
<ak-user-mfa-form slot="form" .instancePk=${item.pk}> </ak-user-mfa-form>
<button slot="trigger" class="pf-c-button pf-m-plain">
<i class="fas fa-edit"></i>
</button>