web/user: rework MFA Device UI to support multiple devices

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer
2021-10-30 14:56:14 +02:00
parent c934915776
commit 40f8ce3c4c
13 changed files with 623 additions and 866 deletions

View File

@ -1,7 +1,7 @@
import { t } from "@lingui/macro";
import { CSSResult, LitElement, TemplateResult, html } from "lit";
import { customElement } from "lit/decorators";
import { customElement, state } from "lit/decorators";
import { until } from "lit/directives/until";
import AKGlobal from "../../authentik.css";
@ -12,18 +12,24 @@ import PFForm from "@patternfly/patternfly/components/Form/form.css";
import PFFormControl from "@patternfly/patternfly/components/FormControl/form-control.css";
import PFPage from "@patternfly/patternfly/components/Page/page.css";
import PFGallery from "@patternfly/patternfly/layouts/Gallery/gallery.css";
import PFStack from "@patternfly/patternfly/layouts/Stack/stack.css";
import PFBase from "@patternfly/patternfly/patternfly-base.css";
import PFDisplay from "@patternfly/patternfly/utilities/Display/display.css";
import PFFlex from "@patternfly/patternfly/utilities/Flex/flex.css";
import PFSizing from "@patternfly/patternfly/utilities/Sizing/sizing.css";
import { StagesApi, UserSetting } from "@goauthentik/api";
import { DEFAULT_CONFIG } from "../../api/Config";
import { me } from "../../api/Users";
import { EVENT_REFRESH } from "../../constants";
import "../../elements/Tabs";
import "../../elements/user/SessionList";
import "../../elements/user/UserConsentList";
import "./UserSelfForm";
import "./details/UserDetailsForm";
import "./details/UserPassword";
import "./mfa/MFADevicesPage";
import "./sources/SourceSettings";
import "./stages/StageSettings";
import "./tokens/UserTokenList";
@customElement("ak-user-settings")
@ -41,10 +47,25 @@ export class UserSettingsPage extends LitElement {
PFSizing,
PFForm,
PFFormControl,
PFStack,
AKGlobal,
];
}
@state()
userSettings!: Promise<UserSetting[]>;
constructor() {
super();
this.addEventListener(EVENT_REFRESH, () => {
this.firstUpdated();
});
}
firstUpdated(): void {
this.userSettings = new StagesApi(DEFAULT_CONFIG).stagesAllUserSettingsList();
}
render(): TemplateResult {
return html`<div class="pf-c-page">
<main role="main" class="pf-c-page__main" tabindex="-1">
@ -54,10 +75,30 @@ export class UserSettingsPage extends LitElement {
data-tab-title="${t`User details`}"
class="pf-c-page__main-section pf-m-no-padding-mobile"
>
<div class="pf-c-card">
<div class="pf-c-card__title">${t`Update details`}</div>
<div class="pf-c-card__body">
<ak-user-self-form .instancePk=${1}></ak-user-self-form>
<div class="pf-l-stack pf-m-gutter">
<div class="pf-l-stack__item">
<div class="pf-c-card">
<div class="pf-c-card__title">${t`Update details`}</div>
<div class="pf-c-card__body">
<ak-user-details-form
.instancePk=${1}
></ak-user-details-form>
</div>
</div>
</div>
<div class="pf-l-stack__item">
${until(
this.userSettings?.then((settings) => {
if (
settings.filter(
(stage) =>
stage.component === "ak-user-settings-password",
).length > 0
) {
return html`<ak-user-settings-password></ak-user-settings-password>`;
}
}),
)}
</div>
</div>
</section>
@ -88,11 +129,13 @@ export class UserSettingsPage extends LitElement {
)}
</section>
<section
slot="page-stages"
data-tab-title="${t`Password, 2FA, etc`}"
slot="page-mfa"
data-tab-title="${t`MFA Devices`}"
class="pf-c-page__main-section pf-m-no-padding-mobile"
>
<ak-user-settings-stage></ak-user-settings-stage>
<ak-user-settings-mfa
.userSettings=${this.userSettings}
></ak-user-settings-mfa>
</section>
<section
slot="page-sources"

View File

@ -7,21 +7,22 @@ import { until } from "lit/directives/until";
import { CoreApi, UserSelf } from "@goauthentik/api";
import { DEFAULT_CONFIG, tenant } from "../../api/Config";
import "../../elements/EmptyState";
import "../../elements/forms/Form";
import "../../elements/forms/FormElement";
import "../../elements/forms/HorizontalFormElement";
import { ModelForm } from "../../elements/forms/ModelForm";
import { DEFAULT_CONFIG, tenant } from "../../../api/Config";
import { me } from "../../../api/Users";
import "../../../elements/EmptyState";
import "../../../elements/forms/Form";
import "../../../elements/forms/FormElement";
import "../../../elements/forms/HorizontalFormElement";
import { ModelForm } from "../../../elements/forms/ModelForm";
@customElement("ak-user-self-form")
export class UserSelfForm extends ModelForm<UserSelf, number> {
@customElement("ak-user-details-form")
export class UserDetailsForm extends ModelForm<UserSelf, number> {
viewportCheck = false;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
loadInstance(pk: number): Promise<UserSelf> {
return new CoreApi(DEFAULT_CONFIG).coreUsersMeRetrieve().then((su) => {
return su.user;
return me().then((user) => {
return user.user;
});
}

View File

@ -1,13 +1,30 @@
import { t } from "@lingui/macro";
import { TemplateResult, html } from "lit";
import { CSSResult, LitElement } from "lit";
import { customElement } from "lit/decorators";
import { property } from "lit/decorators";
import { ifDefined } from "lit/directives/if-defined";
import { BaseUserSettings } from "../BaseUserSettings";
import AKGlobal from "../../../authentik.css";
import PFButton from "@patternfly/patternfly/components/Button/button.css";
import PFCard from "@patternfly/patternfly/components/Card/card.css";
import PFForm from "@patternfly/patternfly/components/Form/form.css";
import PFFormControl from "@patternfly/patternfly/components/FormControl/form-control.css";
import PFBase from "@patternfly/patternfly/patternfly-base.css";
@customElement("ak-user-settings-password")
export class UserSettingsPassword extends BaseUserSettings {
export class UserSettingsPassword extends LitElement {
@property()
objectId!: string;
@property()
configureUrl?: string;
static get styles(): CSSResult[] {
return [PFBase, PFCard, PFButton, PFForm, PFFormControl, AKGlobal];
}
render(): TemplateResult {
// For this stage we don't need to check for a configureFlow,
// as the stage won't return any UI Elements if no configureFlow is set.

View File

@ -0,0 +1,155 @@
import { t } from "@lingui/macro";
import { TemplateResult, html } from "lit";
import { customElement, property } from "lit/decorators";
import { until } from "lit/directives/until";
import { AuthenticatorsApi, Device, UserSetting } from "@goauthentik/api";
import { AKResponse } from "../../../api/Client";
import { DEFAULT_CONFIG } from "../../../api/Config";
import "../../../elements/buttons/Dropdown";
import "../../../elements/buttons/ModalButton";
import "../../../elements/buttons/TokenCopyButton";
import "../../../elements/forms/DeleteBulkForm";
import "../../../elements/forms/ModalForm";
import { Table, TableColumn } from "../../../elements/table/Table";
export function stageToAuthenticatorName(stage: UserSetting): string {
switch (stage.component) {
case "ak-user-settings-authenticator-duo":
return t`Duo authenticator`;
case "ak-user-settings-authenticator-sms":
return t`SMS authenticator`;
case "ak-user-settings-authenticator-static":
return t`Static authenticator`;
case "ak-user-settings-authenticator-totp":
return t`TOTP authenticator`;
case "ak-user-settings-authenticator-webauthn":
return t`Security key authenticator`;
}
return `Invalid stage component ${stage.component}`;
}
@customElement("ak-user-settings-mfa")
export class MFADevicesPage extends Table<Device> {
@property({ attribute: false })
userSettings?: Promise<UserSetting[]>;
checkbox = true;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async apiEndpoint(page: number): Promise<AKResponse<Device>> {
const devices = await new AuthenticatorsApi(DEFAULT_CONFIG).authenticatorsAllList();
return {
pagination: {
current: 0,
count: devices.length,
totalPages: 1,
startIndex: 1,
endIndex: devices.length,
},
results: devices,
};
}
columns(): TableColumn[] {
return [new TableColumn(t`Name`), new TableColumn(t`Type`), new TableColumn("")];
}
renderToolbar(): TemplateResult {
return html`<ak-dropdown class="pf-c-dropdown">
<button class="pf-m-primary pf-c-dropdown__toggle" type="button">
<span class="pf-c-dropdown__toggle-text">${t`Enroll`}</span>
<i class="fas fa-caret-down pf-c-dropdown__toggle-icon" aria-hidden="true"></i>
</button>
<ul class="pf-c-dropdown__menu" hidden>
${until(
this.userSettings?.then((stages) => {
return stages
.filter((stage) => {
if (stage.component === "ak-user-settings-password") {
return false;
}
return stage.configureUrl;
})
.map((stage) => {
return html`<li>
<a
href="${stage.configureUrl}"
class="pf-c-dropdown__menu-item"
>
${stageToAuthenticatorName(stage)}
</a>
</li>`;
});
}),
html`<ak-empty-state
?loading="${true}"
header=${t`Loading`}
></ak-empty-state>`,
)}
</ul>
</ak-dropdown>
${super.renderToolbar()}`;
}
async deleteWrapper(device: Device) {
switch (device.type) {
case "authentik_stages_authenticator_duo.DuoDevice":
return new AuthenticatorsApi(DEFAULT_CONFIG).authenticatorsDuoDestroy({
id: device.pk,
});
case "authentik_stages_authenticator_sms.SMSDevice":
return new AuthenticatorsApi(DEFAULT_CONFIG).authenticatorsSmsDestroy({
id: device.pk,
});
case "otp_totp.TOTPDevice":
return new AuthenticatorsApi(DEFAULT_CONFIG).authenticatorsTotpDestroy({
id: device.pk,
});
case "otp_static.StaticDevice":
return new AuthenticatorsApi(DEFAULT_CONFIG).authenticatorsStaticDestroy({
id: device.pk,
});
case "authentik_stages_authenticator_webauthn.WebAuthnDevice":
return new AuthenticatorsApi(DEFAULT_CONFIG).authenticatorsWebauthnDestroy({
id: device.pk,
});
default:
break;
}
}
renderToolbarSelected(): TemplateResult {
const disabled = this.selectedElements.length < 1;
return html`<ak-forms-delete-bulk
objectLabel=${t`Device(s)`}
.objects=${this.selectedElements}
.delete=${(item: Device) => {
return this.deleteWrapper(item);
}}
>
<button ?disabled=${disabled} slot="trigger" class="pf-c-button pf-m-danger">
${t`Delete`}
</button>
</ak-forms-delete-bulk>`;
}
row(item: Device): TemplateResult[] {
return [
html`${item.name}`,
html`${item.verboseName}`,
html`
<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>
<button slot="trigger" class="pf-c-button pf-m-plain">
<i class="fas fa-edit"></i>
</button>
</ak-forms-modal>
`,
];
}
}

View File

@ -1,98 +0,0 @@
import { t } from "@lingui/macro";
import { CSSResult, LitElement, TemplateResult, html } from "lit";
import { customElement, property } from "lit/decorators";
import { until } from "lit/directives/until";
import PFStack from "@patternfly/patternfly/layouts/Stack/stack.css";
import { StagesApi, UserSetting } from "@goauthentik/api";
import { DEFAULT_CONFIG } from "../../../api/Config";
import { EVENT_REFRESH } from "../../../constants";
import "../../../elements/EmptyState";
import "./UserSettingsAuthenticatorDuo";
import "./UserSettingsAuthenticatorSMS";
import "./UserSettingsAuthenticatorStatic";
import "./UserSettingsAuthenticatorTOTP";
import "./UserSettingsAuthenticatorWebAuthn";
import "./UserSettingsPassword";
@customElement("ak-user-settings-stage")
export class UserStageSettingsPage extends LitElement {
@property({ attribute: false })
userSettings?: Promise<UserSetting[]>;
static get styles(): CSSResult[] {
return [PFStack];
}
constructor() {
super();
this.addEventListener(EVENT_REFRESH, () => {
this.firstUpdated();
});
}
firstUpdated(): void {
this.userSettings = new StagesApi(DEFAULT_CONFIG).stagesAllUserSettingsList();
}
renderStageSettings(stage: UserSetting): TemplateResult {
switch (stage.component) {
case "ak-user-settings-authenticator-webauthn":
return html`<ak-user-settings-authenticator-webauthn
objectId=${stage.objectUid}
.configureUrl=${stage.configureUrl}
>
</ak-user-settings-authenticator-webauthn>`;
case "ak-user-settings-password":
return html`<ak-user-settings-password
objectId=${stage.objectUid}
.configureUrl=${stage.configureUrl}
>
</ak-user-settings-password>`;
case "ak-user-settings-authenticator-totp":
return html`<ak-user-settings-authenticator-totp
objectId=${stage.objectUid}
.configureUrl=${stage.configureUrl}
>
</ak-user-settings-authenticator-totp>`;
case "ak-user-settings-authenticator-static":
return html`<ak-user-settings-authenticator-static
objectId=${stage.objectUid}
.configureUrl=${stage.configureUrl}
>
</ak-user-settings-authenticator-static>`;
case "ak-user-settings-authenticator-duo":
return html`<ak-user-settings-authenticator-duo
objectId=${stage.objectUid}
.configureUrl=${stage.configureUrl}
>
</ak-user-settings-authenticator-duo>`;
case "ak-user-settings-authenticator-sms":
return html`<ak-user-settings-authenticator-sms
objectId=${stage.objectUid}
.configureUrl=${stage.configureUrl}
>
</ak-user-settings-authenticator-sms>`;
default:
return html`<p>${t`Error: unsupported stage settings: ${stage.component}`}</p>`;
}
}
render(): TemplateResult {
return html`<div class="pf-l-stack pf-m-gutter">
${until(
this.userSettings?.then((stages) => {
return stages.map((stage) => {
return html`<div class="pf-l-stack__item">
${this.renderStageSettings(stage)}
</div>`;
});
}),
html`<ak-empty-state ?loading="${true}" header=${t`Loading`}></ak-empty-state>`,
)}
</div>`;
}
}

View File

@ -1,85 +0,0 @@
import { t } from "@lingui/macro";
import { TemplateResult, html } from "lit";
import { customElement } from "lit/decorators";
import { until } from "lit/directives/until";
import { AuthenticatorsApi } from "@goauthentik/api";
import { DEFAULT_CONFIG } from "../../../api/Config";
import { EVENT_REFRESH } from "../../../constants";
import { BaseUserSettings } from "../BaseUserSettings";
@customElement("ak-user-settings-authenticator-duo")
export class UserSettingsAuthenticatorDuo extends BaseUserSettings {
renderEnabled(): TemplateResult {
return html`<div class="pf-c-card__body">
<p>
${t`Status: Enabled`}
<i class="pf-icon pf-icon-ok"></i>
</p>
</div>
<div class="pf-c-card__footer">
<button
class="pf-c-button pf-m-danger"
@click=${() => {
return new AuthenticatorsApi(DEFAULT_CONFIG)
.authenticatorsDuoList({})
.then((devices) => {
if (devices.results.length < 1) {
return;
}
// TODO: Handle multiple devices, currently we assume only one TOTP Device
return new AuthenticatorsApi(DEFAULT_CONFIG)
.authenticatorsDuoDestroy({
id: devices.results[0].pk || 0,
})
.then(() => {
this.dispatchEvent(
new CustomEvent(EVENT_REFRESH, {
bubbles: true,
composed: true,
}),
);
});
});
}}
>
${t`Disable Duo authenticator`}
</button>
</div>`;
}
renderDisabled(): TemplateResult {
return html` <div class="pf-c-card__body">
<p>
${t`Status: Disabled`}
<i class="pf-icon pf-icon-error-circle-o"></i>
</p>
</div>
<div class="pf-c-card__footer">
${this.configureUrl
? html`<a
href="${this.configureUrl}?next=/${encodeURIComponent(
"#/settings;page-stages",
)}"
class="pf-c-button pf-m-primary"
>${t`Enable Duo authenticator`}
</a>`
: html``}
</div>`;
}
render(): TemplateResult {
return html`<div class="pf-c-card">
<div class="pf-c-card__title">${t`Duo`}</div>
${until(
new AuthenticatorsApi(DEFAULT_CONFIG).authenticatorsDuoList({}).then((devices) => {
return devices.results.length > 0
? this.renderEnabled()
: this.renderDisabled();
}),
)}
</div>`;
}
}

View File

@ -1,85 +0,0 @@
import { t } from "@lingui/macro";
import { TemplateResult, html } from "lit";
import { customElement } from "lit/decorators";
import { until } from "lit/directives/until";
import { AuthenticatorsApi } from "@goauthentik/api";
import { DEFAULT_CONFIG } from "../../../api/Config";
import { EVENT_REFRESH } from "../../../constants";
import { BaseUserSettings } from "../BaseUserSettings";
@customElement("ak-user-settings-authenticator-sms")
export class UserSettingsAuthenticatorSMS extends BaseUserSettings {
renderEnabled(): TemplateResult {
return html`<div class="pf-c-card__body">
<p>
${t`Status: Enabled`}
<i class="pf-icon pf-icon-ok"></i>
</p>
</div>
<div class="pf-c-card__footer">
<button
class="pf-c-button pf-m-danger"
@click=${() => {
return new AuthenticatorsApi(DEFAULT_CONFIG)
.authenticatorsSmsList({})
.then((devices) => {
if (devices.results.length < 1) {
return;
}
// TODO: Handle multiple devices, currently we assume only one TOTP Device
return new AuthenticatorsApi(DEFAULT_CONFIG)
.authenticatorsSmsDestroy({
id: devices.results[0].pk || 0,
})
.then(() => {
this.dispatchEvent(
new CustomEvent(EVENT_REFRESH, {
bubbles: true,
composed: true,
}),
);
});
});
}}
>
${t`Disable SMS authenticator`}
</button>
</div>`;
}
renderDisabled(): TemplateResult {
return html` <div class="pf-c-card__body">
<p>
${t`Status: Disabled`}
<i class="pf-icon pf-icon-error-circle-o"></i>
</p>
</div>
<div class="pf-c-card__footer">
${this.configureUrl
? html`<a
href="${this.configureUrl}?next=/${encodeURIComponent(
"#/settings;page-stages",
)}"
class="pf-c-button pf-m-primary"
>${t`Enable SMS authenticator`}
</a>`
: html``}
</div>`;
}
render(): TemplateResult {
return html`<div class="pf-c-card">
<div class="pf-c-card__title">${t`SMS`}</div>
${until(
new AuthenticatorsApi(DEFAULT_CONFIG).authenticatorsSmsList({}).then((devices) => {
return devices.results.length > 0
? this.renderEnabled()
: this.renderDisabled();
}),
)}
</div>`;
}
}

View File

@ -1,106 +0,0 @@
import { t } from "@lingui/macro";
import { CSSResult, TemplateResult, html } from "lit";
import { customElement } from "lit/decorators";
import { until } from "lit/directives/until";
import { AuthenticatorsApi } from "@goauthentik/api";
import { DEFAULT_CONFIG } from "../../../api/Config";
import { EVENT_REFRESH } from "../../../constants";
import { STATIC_TOKEN_STYLE } from "../../../flows/stages/authenticator_static/AuthenticatorStaticStage";
import { BaseUserSettings } from "../BaseUserSettings";
@customElement("ak-user-settings-authenticator-static")
export class UserSettingsAuthenticatorStatic extends BaseUserSettings {
static get styles(): CSSResult[] {
return super.styles.concat(STATIC_TOKEN_STYLE);
}
renderEnabled(): TemplateResult {
return html`<div class="pf-c-card__body">
<p>
${t`Status: Enabled`}
<i class="pf-icon pf-icon-ok"></i>
</p>
<ul class="ak-otp-tokens">
${until(
new AuthenticatorsApi(DEFAULT_CONFIG)
.authenticatorsStaticList({})
.then((devices) => {
if (devices.results.length < 1) {
return;
}
return devices.results[0].tokenSet?.map((token) => {
return html`<li>${token.token}</li>`;
});
}),
)}
</ul>
</div>
<div class="pf-c-card__footer">
<button
class="pf-c-button pf-m-danger"
@click=${() => {
return new AuthenticatorsApi(DEFAULT_CONFIG)
.authenticatorsStaticList({})
.then((devices) => {
if (devices.results.length < 1) {
return;
}
// TODO: Handle multiple devices, currently we assume only one TOTP Device
return new AuthenticatorsApi(DEFAULT_CONFIG)
.authenticatorsStaticDestroy({
id: devices.results[0].pk || 0,
})
.then(() => {
this.dispatchEvent(
new CustomEvent(EVENT_REFRESH, {
bubbles: true,
composed: true,
}),
);
});
});
}}
>
${t`Disable Static Tokens`}
</button>
</div>`;
}
renderDisabled(): TemplateResult {
return html` <div class="pf-c-card__body">
<p>
${t`Status: Disabled`}
<i class="pf-icon pf-icon-error-circle-o"></i>
</p>
</div>
<div class="pf-c-card__footer">
${this.configureUrl
? html`<a
href="${this.configureUrl}?next=/${encodeURIComponent(
"#/settings;page-stages",
)}"
class="pf-c-button pf-m-primary"
>${t`Enable Static Tokens`}
</a>`
: html``}
</div>`;
}
render(): TemplateResult {
return html`<div class="pf-c-card">
<div class="pf-c-card__title">${t`Static tokens`}</div>
${until(
new AuthenticatorsApi(DEFAULT_CONFIG)
.authenticatorsStaticList({})
.then((devices) => {
return devices.results.length > 0
? this.renderEnabled()
: this.renderDisabled();
}),
)}
</div>`;
}
}

View File

@ -1,85 +0,0 @@
import { t } from "@lingui/macro";
import { TemplateResult, html } from "lit";
import { customElement } from "lit/decorators";
import { until } from "lit/directives/until";
import { AuthenticatorsApi } from "@goauthentik/api";
import { DEFAULT_CONFIG } from "../../../api/Config";
import { EVENT_REFRESH } from "../../../constants";
import { BaseUserSettings } from "../BaseUserSettings";
@customElement("ak-user-settings-authenticator-totp")
export class UserSettingsAuthenticatorTOTP extends BaseUserSettings {
renderEnabled(): TemplateResult {
return html`<div class="pf-c-card__body">
<p>
${t`Status: Enabled`}
<i class="pf-icon pf-icon-ok"></i>
</p>
</div>
<div class="pf-c-card__footer">
<button
class="pf-c-button pf-m-danger"
@click=${() => {
return new AuthenticatorsApi(DEFAULT_CONFIG)
.authenticatorsTotpList({})
.then((devices) => {
if (devices.results.length < 1) {
return;
}
// TODO: Handle multiple devices, currently we assume only one TOTP Device
return new AuthenticatorsApi(DEFAULT_CONFIG)
.authenticatorsTotpDestroy({
id: devices.results[0].pk || 0,
})
.then(() => {
this.dispatchEvent(
new CustomEvent(EVENT_REFRESH, {
bubbles: true,
composed: true,
}),
);
});
});
}}
>
${t`Disable Time-based OTP`}
</button>
</div>`;
}
renderDisabled(): TemplateResult {
return html` <div class="pf-c-card__body">
<p>
${t`Status: Disabled`}
<i class="pf-icon pf-icon-error-circle-o"></i>
</p>
</div>
<div class="pf-c-card__footer">
${this.configureUrl
? html`<a
href="${this.configureUrl}?next=/${encodeURIComponent(
"#/settings;page-stages",
)}"
class="pf-c-button pf-m-primary"
>${t`Enable TOTP`}
</a>`
: html``}
</div>`;
}
render(): TemplateResult {
return html`<div class="pf-c-card">
<div class="pf-c-card__title">${t`Time-based One-Time Passwords`}</div>
${until(
new AuthenticatorsApi(DEFAULT_CONFIG).authenticatorsTotpList({}).then((devices) => {
return devices.results.length > 0
? this.renderEnabled()
: this.renderDisabled();
}),
)}
</div>`;
}
}

View File

@ -1,132 +0,0 @@
import { t } from "@lingui/macro";
import { CSSResult, TemplateResult, html } from "lit";
import { customElement } from "lit/decorators";
import { ifDefined } from "lit/directives/if-defined";
import { until } from "lit/directives/until";
import PFDataList from "@patternfly/patternfly/components/DataList/data-list.css";
import { AuthenticatorsApi, WebAuthnDevice } from "@goauthentik/api";
import { DEFAULT_CONFIG } from "../../../api/Config";
import { EVENT_REFRESH } from "../../../constants";
import "../../../elements/buttons/ModalButton";
import "../../../elements/buttons/SpinnerButton";
import "../../../elements/forms/DeleteForm";
import "../../../elements/forms/Form";
import "../../../elements/forms/HorizontalFormElement";
import "../../../elements/forms/ModalForm";
import { BaseUserSettings } from "../BaseUserSettings";
@customElement("ak-user-settings-authenticator-webauthn")
export class UserSettingsAuthenticatorWebAuthn extends BaseUserSettings {
static get styles(): CSSResult[] {
return super.styles.concat(PFDataList);
}
renderDelete(device: WebAuthnDevice): TemplateResult {
return html`<ak-forms-delete
.obj=${device}
objectLabel=${t`Authenticator`}
.delete=${() => {
return new AuthenticatorsApi(DEFAULT_CONFIG)
.authenticatorsWebauthnDestroy({
id: device.pk || 0,
})
.then(() => {
this.dispatchEvent(
new CustomEvent(EVENT_REFRESH, {
bubbles: true,
composed: true,
}),
);
});
}}
>
<button slot="trigger" class="pf-c-button pf-m-danger">${t`Delete`}</button>
</ak-forms-delete>`;
}
renderUpdate(device: WebAuthnDevice): TemplateResult {
return html`<ak-forms-modal>
<span slot="submit"> ${t`Update`} </span>
<span slot="header"> ${t`Update`} </span>
<ak-form
slot="form"
successMessage=${t`Successfully updated device.`}
.send=${(data: unknown) => {
return new AuthenticatorsApi(DEFAULT_CONFIG)
.authenticatorsWebauthnUpdate({
id: device.pk || 0,
webAuthnDeviceRequest: data as WebAuthnDevice,
})
.then(() => {
this.requestUpdate();
});
}}
>
<form class="pf-c-form pf-m-horizontal">
<ak-form-element-horizontal
label=${t`Device name`}
?required=${true}
name="name"
>
<input
type="text"
value="${ifDefined(device.name)}"
class="pf-c-form-control"
required
/>
</ak-form-element-horizontal>
</form>
</ak-form>
<button slot="trigger" class="pf-c-button pf-m-primary">${t`Update`}</button>
</ak-forms-modal>`;
}
render(): TemplateResult {
return html`<div class="pf-c-card">
<div class="pf-c-card__title">${t`WebAuthn Devices`}</div>
<div class="pf-c-card__body">
<ul class="pf-c-data-list" role="list">
${until(
new AuthenticatorsApi(DEFAULT_CONFIG)
.authenticatorsWebauthnList({})
.then((devices) => {
return devices.results.map((device) => {
return html`<li class="pf-c-data-list__item">
<div class="pf-c-data-list__item-row">
<div class="pf-c-data-list__item-content">
<div class="pf-c-data-list__cell">
${device.name || t`-`}
</div>
<div class="pf-c-data-list__cell">
${t`Created ${device.createdOn?.toLocaleString()}`}
</div>
<div class="pf-c-data-list__cell">
${this.renderUpdate(device)}
${this.renderDelete(device)}
</div>
</div>
</div>
</li>`;
});
}),
)}
</ul>
</div>
<div class="pf-c-card__footer">
${this.configureUrl
? html`<a
href="${this.configureUrl}?next=/${encodeURIComponent(
"#/settings;page-stages",
)}"
class="pf-c-button pf-m-primary"
>${t`Configure WebAuthn`}
</a>`
: html``}
</div>
</div>`;
}
}