import { DEFAULT_CONFIG } from "@goauthentik/common/api/config"; import { dateTimeLocal } from "@goauthentik/common/utils"; import { Form } from "@goauthentik/elements/forms/Form"; import "@goauthentik/elements/forms/HorizontalFormElement"; import { ModalForm } from "@goauthentik/elements/forms/ModalForm"; import { msg, str } from "@lit/localize"; import { TemplateResult, html } from "lit"; import { customElement, property } from "lit/decorators.js"; import { ifDefined } from "lit/directives/if-defined.js"; import { CoreApi, Group, UserServiceAccountRequest, UserServiceAccountResponse, } from "@goauthentik/api"; @customElement("ak-user-service-account-form") export class ServiceAccountForm extends Form { @property({ attribute: false }) result?: UserServiceAccountResponse; @property({ attribute: false }) group?: Group; getSuccessMessage(): string { if (this.group) { return msg(str`Successfully created user and added to group ${this.group.name}`); } return msg("Successfully created user."); } async send(data: UserServiceAccountRequest): Promise { const result = await new CoreApi(DEFAULT_CONFIG).coreUsersServiceAccountCreate({ userServiceAccountRequest: data, }); this.result = result; (this.parentElement as ModalForm).showSubmitButton = false; if (this.group) { await new CoreApi(DEFAULT_CONFIG).coreGroupsAddUserCreate({ groupUuid: this.group.pk, userAccountRequest: { pk: this.result.userPk, }, }); } return result; } resetForm(): void { super.resetForm(); this.result = undefined; } renderForm(): TemplateResult { return html`

${msg("User's primary identifier. 150 characters or fewer.")}

${msg( "Enabling this toggle will create a group named after the user, with the user as member.", )}

${msg( "If this is selected, the token will expire. Upon expiration, the token will be rotated.", )}

`; } renderResponseForm(): TemplateResult { return html`

${msg( "Use the username and password below to authenticate. The password can be retrieved later on the Tokens page.", )}

${msg( "Valid for 360 days, after which the password will automatically rotate. You can copy the password from the Token List.", )}

`; } renderFormWrapper(): TemplateResult { if (this.result) { return this.renderResponseForm(); } return super.renderFormWrapper(); } } declare global { interface HTMLElementTagNameMap { "ak-user-service-account-form": ServiceAccountForm; } }