import "@goauthentik/admin/providers/RelatedApplicationButton"; import "@goauthentik/admin/providers/oauth2/OAuth2ProviderForm"; import { DEFAULT_CONFIG } from "@goauthentik/common/api/config"; import { EVENT_REFRESH } from "@goauthentik/common/constants"; import renderDescriptionList from "@goauthentik/components/DescriptionList"; import "@goauthentik/components/events/ObjectChangelog"; import MDProviderOAuth2 from "@goauthentik/docs/add-secure-apps/providers/oauth2/index.md"; import { AKElement } from "@goauthentik/elements/Base"; import "@goauthentik/elements/CodeMirror"; import "@goauthentik/elements/EmptyState"; import "@goauthentik/elements/Markdown"; import "@goauthentik/elements/Tabs"; import "@goauthentik/elements/buttons/ModalButton"; import "@goauthentik/elements/buttons/SpinnerButton"; import { msg } from "@lit/localize"; import { CSSResult, TemplateResult, html } from "lit"; import { customElement, property, state } from "lit/decorators.js"; import PFBanner from "@patternfly/patternfly/components/Banner/banner.css"; import PFButton from "@patternfly/patternfly/components/Button/button.css"; import PFCard from "@patternfly/patternfly/components/Card/card.css"; import PFContent from "@patternfly/patternfly/components/Content/content.css"; import PFDescriptionList from "@patternfly/patternfly/components/DescriptionList/description-list.css"; import PFDivider from "@patternfly/patternfly/components/Divider/divider.css"; 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 PFGrid from "@patternfly/patternfly/layouts/Grid/grid.css"; import PFBase from "@patternfly/patternfly/patternfly-base.css"; import { ClientTypeEnum, CoreApi, CoreUsersListRequest, OAuth2Provider, OAuth2ProviderSetupURLs, PropertyMappingPreview, ProvidersApi, RbacPermissionsAssignedByUsersListModelEnum, User, } from "@goauthentik/api"; export function TypeToLabel(type?: ClientTypeEnum): string { if (!type) return ""; switch (type) { case ClientTypeEnum.Confidential: return msg("Confidential"); case ClientTypeEnum.Public: return msg("Public"); case ClientTypeEnum.UnknownDefaultOpenApi: return msg("Unknown type"); } } @customElement("ak-provider-oauth2-view") export class OAuth2ProviderViewPage extends AKElement { @property({ type: Number }) set providerID(value: number) { new ProvidersApi(DEFAULT_CONFIG) .providersOauth2Retrieve({ id: value, }) .then((prov) => { this.provider = prov; }); } @property({ attribute: false }) provider?: OAuth2Provider; @state() providerUrls?: OAuth2ProviderSetupURLs; @state() preview?: PropertyMappingPreview; @state() previewUser?: User; static get styles(): CSSResult[] { return [ PFBase, PFButton, PFPage, PFGrid, PFContent, PFCard, PFDescriptionList, PFForm, PFFormControl, PFBanner, PFDivider, ]; } constructor() { super(); this.addEventListener(EVENT_REFRESH, () => { if (!this.provider?.pk) return; this.providerID = this.provider?.pk; }); } fetchPreview(): void { new ProvidersApi(DEFAULT_CONFIG) .providersOauth2PreviewUserRetrieve({ id: this.provider?.pk || 0, forUser: this.previewUser?.pk, }) .then((preview) => (this.preview = preview)); } render(): TemplateResult { if (!this.provider) { return html``; } return html`
{ new ProvidersApi(DEFAULT_CONFIG) .providersOauth2SetupUrlsRetrieve({ id: this.provider?.pk || 0, }) .then((prov) => { this.providerUrls = prov; }); }} > ${this.renderTabOverview()}
{ this.fetchPreview(); }} > ${this.renderTabPreview()}
`; } renderTabOverview(): TemplateResult { if (!this.provider) { return html``; } return html` ${this.provider?.assignedApplicationName ? html`` : html`
${msg("Warning: Provider is not used by an Application.")}
`}
${msg("Name")}
${this.provider.name}
${msg("Assigned to application")}
${msg("Client type")}
${TypeToLabel(this.provider.clientType)}
${msg("Client ID")}
${this.provider.clientId}
${msg("Redirect URIs")}
    ${this.provider.redirectUris.map((ru) => { return html`
  • ${ru.matchingMode}: ${ru.url}
  • `; })}

{ if (!this.provider) { return input; } return input.replaceAll( "<application slug>", this.provider.assignedApplicationSlug, ); }, ]} .md=${MDProviderOAuth2} meta="providers/oauth2/index.md" >
`; } renderTabPreview(): TemplateResult { if (!this.provider) { return html``; } return html`
${msg("JWT payload")}
${renderDescriptionList( [ [ msg("Preview for user"), html` => { const args: CoreUsersListRequest = { ordering: "username", }; if (query !== undefined) { args.search = query; } const users = await new CoreApi( DEFAULT_CONFIG, ).coreUsersList(args); return users.results; }} .renderElement=${(user: User): string => { return user.username; }} .renderDescription=${(user: User): TemplateResult => { return html`${user.name}`; }} .value=${(user: User | undefined): number | undefined => { return user?.pk; }} .selected=${(user: User): boolean => { return user.pk === this.previewUser?.pk; }} ?blankable=${true} @ak-change=${(ev: CustomEvent) => { this.previewUser = ev.detail.value; this.fetchPreview(); }} > `, ], ], { horizontal: true }, )}
${this.preview ? html`
${JSON.stringify(this.preview?.preview, null, 4)}
` : html` `}
`; } } declare global { interface HTMLElementTagNameMap { "ak-provider-oauth2-view": OAuth2ProviderViewPage; } }