2021-08-25 19:22:24 +00:00
|
|
|
import { t } from "@lingui/macro";
|
2021-09-21 09:31:37 +00:00
|
|
|
|
2021-10-28 07:48:51 +00:00
|
|
|
import { TemplateResult, html } from "lit";
|
2021-11-04 21:34:48 +00:00
|
|
|
import { customElement } from "lit/decorators.js";
|
|
|
|
import { until } from "lit/directives/until.js";
|
2021-09-21 09:31:37 +00:00
|
|
|
|
|
|
|
import { CoreApi, IntentEnum, Token } from "@goauthentik/api";
|
|
|
|
|
2021-08-25 19:22:24 +00:00
|
|
|
import { DEFAULT_CONFIG } from "../../api/Config";
|
|
|
|
import "../../elements/forms/FormGroup";
|
2021-09-21 09:31:37 +00:00
|
|
|
import "../../elements/forms/HorizontalFormElement";
|
2021-08-25 19:22:24 +00:00
|
|
|
import { ModelForm } from "../../elements/forms/ModelForm";
|
2021-11-18 19:44:15 +00:00
|
|
|
import { UserOption } from "../../elements/user/utils";
|
2021-09-21 09:31:37 +00:00
|
|
|
import { dateTimeLocal, first } from "../../utils";
|
2021-08-25 19:22:24 +00:00
|
|
|
|
|
|
|
@customElement("ak-token-form")
|
|
|
|
export class TokenForm extends ModelForm<Token, string> {
|
|
|
|
loadInstance(pk: string): Promise<Token> {
|
|
|
|
return new CoreApi(DEFAULT_CONFIG).coreTokensRetrieve({
|
|
|
|
identifier: pk,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getSuccessMessage(): string {
|
|
|
|
if (this.instance) {
|
|
|
|
return t`Successfully updated token.`;
|
|
|
|
} else {
|
|
|
|
return t`Successfully created token.`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
send = (data: Token): Promise<Token> => {
|
|
|
|
if (this.instance?.identifier) {
|
|
|
|
return new CoreApi(DEFAULT_CONFIG).coreTokensUpdate({
|
|
|
|
identifier: this.instance.identifier,
|
|
|
|
tokenRequest: data,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return new CoreApi(DEFAULT_CONFIG).coreTokensCreate({
|
|
|
|
tokenRequest: data,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
renderForm(): TemplateResult {
|
|
|
|
return html`<form class="pf-c-form pf-m-horizontal">
|
|
|
|
<ak-form-element-horizontal label=${t`Identifier`} name="identifier" ?required=${true}>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
value="${first(this.instance?.identifier, "")}"
|
|
|
|
class="pf-c-form-control"
|
|
|
|
required
|
|
|
|
/>
|
|
|
|
<p class="pf-c-form__helper-text">
|
|
|
|
${t`Unique identifier the token is referenced by.`}
|
|
|
|
</p>
|
|
|
|
</ak-form-element-horizontal>
|
2021-09-19 13:25:48 +00:00
|
|
|
<ak-form-element-horizontal label=${t`User`} ?required=${true} name="user">
|
|
|
|
<select class="pf-c-form-control">
|
|
|
|
${until(
|
|
|
|
new CoreApi(DEFAULT_CONFIG)
|
|
|
|
.coreUsersList({
|
|
|
|
ordering: "username",
|
|
|
|
})
|
|
|
|
.then((users) => {
|
|
|
|
return users.results.map((user) => {
|
|
|
|
return html`<option
|
|
|
|
value=${user.pk}
|
|
|
|
?selected=${this.instance?.user === user.pk}
|
|
|
|
>
|
2021-11-18 19:44:15 +00:00
|
|
|
${UserOption(user)}
|
2021-09-19 13:25:48 +00:00
|
|
|
</option>`;
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
html`<option>${t`Loading...`}</option>`,
|
|
|
|
)}
|
|
|
|
</select>
|
|
|
|
</ak-form-element-horizontal>
|
2021-08-25 19:22:24 +00:00
|
|
|
<ak-form-element-horizontal label=${t`Intent`} ?required=${true} name="intent">
|
|
|
|
<select class="pf-c-form-control">
|
|
|
|
<option
|
|
|
|
value=${IntentEnum.Api}
|
|
|
|
?selected=${this.instance?.intent === IntentEnum.Api}
|
|
|
|
>
|
|
|
|
${t`API Token (can be used to access the API programmatically)`}
|
|
|
|
</option>
|
|
|
|
<option
|
|
|
|
value=${IntentEnum.AppPassword}
|
|
|
|
?selected=${this.instance?.intent === IntentEnum.AppPassword}
|
|
|
|
>
|
|
|
|
${t`App password (can be used to login using a flow executor)`}
|
|
|
|
</option>
|
|
|
|
</select>
|
|
|
|
</ak-form-element-horizontal>
|
|
|
|
<ak-form-element-horizontal label=${t`Description`} name="description">
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
value="${first(this.instance?.description, "")}"
|
|
|
|
class="pf-c-form-control"
|
|
|
|
/>
|
|
|
|
</ak-form-element-horizontal>
|
|
|
|
<ak-form-element-horizontal name="expiring">
|
|
|
|
<div class="pf-c-check">
|
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
class="pf-c-check__input"
|
|
|
|
?checked=${first(this.instance?.expiring, true)}
|
|
|
|
/>
|
|
|
|
<label class="pf-c-check__label"> ${t`Expiring?`} </label>
|
|
|
|
</div>
|
|
|
|
<p class="pf-c-form__helper-text">
|
|
|
|
${t`If this is selected, the token will expire. Upon expiration, the token will be rotated.`}
|
|
|
|
</p>
|
|
|
|
</ak-form-element-horizontal>
|
|
|
|
<ak-form-element-horizontal label=${t`Expires on`} name="expires">
|
|
|
|
<input
|
|
|
|
type="datetime-local"
|
2021-09-18 22:28:40 +00:00
|
|
|
data-type="datetime-local"
|
2021-09-18 13:31:48 +00:00
|
|
|
value="${dateTimeLocal(first(this.instance?.expires, new Date()))}"
|
2021-08-25 19:22:24 +00:00
|
|
|
class="pf-c-form-control"
|
|
|
|
/>
|
|
|
|
</ak-form-element-horizontal>
|
|
|
|
</form>`;
|
|
|
|
}
|
|
|
|
}
|