import { OAuthSource, SourcesApi, FlowsApi, FlowDesignationEnum, OAuthSourceUserMatchingModeEnum } from "authentik-api"; import { t } from "@lingui/macro"; import { customElement, property } from "lit-element"; import { html, TemplateResult } from "lit-html"; import { DEFAULT_CONFIG } from "../../../api/Config"; import "../../../elements/forms/FormGroup"; import "../../../elements/forms/HorizontalFormElement"; import { ifDefined } from "lit-html/directives/if-defined"; import { until } from "lit-html/directives/until"; import { first } from "../../../utils"; import { AppURLManager } from "../../../api/legacy"; import { ModelForm } from "../../../elements/forms/ModelForm"; @customElement("ak-source-oauth-form") export class OAuthSourceForm extends ModelForm { loadInstance(pk: string): Promise { return new SourcesApi(DEFAULT_CONFIG).sourcesOauthRead({ slug: pk, }).then(source => { this.showUrlOptions = first(source.type?.urlsCustomizable, false); return source; }); } @property() modelName?: string; @property({type: Boolean}) showUrlOptions = false; @property({type: Boolean}) showRequestTokenURL = false; getSuccessMessage(): string { if (this.instance) { return t`Successfully updated source.`; } else { return t`Successfully created source.`; } } send = (data: OAuthSource): Promise => { if (this.instance) { return new SourcesApi(DEFAULT_CONFIG).sourcesOauthUpdate({ slug: this.instance.slug, data: data }); } else { return new SourcesApi(DEFAULT_CONFIG).sourcesOauthCreate({ data: data }); } }; renderUrlOptions(): TemplateResult { if (!this.showUrlOptions) { return html``; } return html` ${t`URL settings`}

${t`URL the user is redirect to to consent the authorization.`}

${t`URL used by authentik to retrieve tokens.`}

${t`URL used by authentik to get user information.`}

${this.showRequestTokenURL ? html`

${t`URL used to request the initial token. This URL is only required for OAuth 1.`}

` : html``}
`; } getRedirectURI(slug?: string): string { if (!slug) { return ""; } const path = AppURLManager.sourceOAuth(slug, "callback"); return `${window.location.protocol}//${window.location.host}${path}`; } renderForm(): TemplateResult { return html`
{ const current = (ev.target as HTMLInputElement).value; const label = this.shadowRoot?.querySelector("#callback-url"); if (!label) return; label.innerText = this.getRedirectURI(current); }}>

${t`Use this redirect URL:`} ${this.getRedirectURI(this.instance?.slug)}

${t`Protocol settings`}
${this.renderUrlOptions()} ${t`Flow settings`}

${t`Flow to use when authenticating existing users.`}

${t`Flow to use when enrolling new users.`}

`; } }