import { dateTimeLocal } from "@goauthentik/authentik/common/utils"; import { DEFAULT_CONFIG } from "@goauthentik/common/api/config"; import "@goauthentik/elements/forms/HorizontalFormElement"; import { ModelForm } from "@goauthentik/elements/forms/ModelForm"; import { msg } 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, IntentEnum, Token } from "@goauthentik/api"; @customElement("ak-user-token-form") export class UserTokenForm extends ModelForm { @property() intent: IntentEnum = IntentEnum.Api; loadInstance(pk: string): Promise { return new CoreApi(DEFAULT_CONFIG).coreTokensRetrieve({ identifier: pk, }); } getSuccessMessage(): string { return this.instance ? msg("Successfully updated token.") : msg("Successfully created token."); } async send(data: Token): Promise { if (this.instance) { data.intent = this.instance.intent; return new CoreApi(DEFAULT_CONFIG).coreTokensUpdate({ identifier: this.instance.identifier, tokenRequest: data, }); } else { data.intent = this.intent; return new CoreApi(DEFAULT_CONFIG).coreTokensCreate({ tokenRequest: data, }); } } renderForm(): TemplateResult { const now = new Date(); const expiringDate = this.instance?.expires ? new Date(this.instance.expires.getTime()) : new Date(now.getTime() + 30 * 60000); return html` ${this.intent == IntentEnum.AppPassword ? html` ` : html``}`; } }