import { t } from "@lingui/macro"; import { css, CSSResult, customElement, html, LitElement, property, TemplateResult } from "lit-element"; import { until } from "lit-html/directives/until"; import { EventMatcherPolicyActionEnum, FlowsApi } from "authentik-api"; import "../../elements/Spinner"; import "../../elements/Expand"; import { PFSize } from "../../elements/Spinner"; import { EventContext, EventWithContext } from "../../api/Events"; import { DEFAULT_CONFIG } from "../../api/Config"; import PFDescriptionList from "@patternfly/patternfly/components/DescriptionList/description-list.css"; import PFFlex from "@patternfly/patternfly/layouts/Flex/flex.css"; import PFBase from "@patternfly/patternfly/patternfly-base.css"; import PFList from "@patternfly/patternfly/components/List/list.css"; @customElement("ak-event-info") export class EventInfo extends LitElement { @property({attribute: false}) event!: EventWithContext; static get styles(): CSSResult[] { return [PFBase, PFFlex, PFList, PFDescriptionList, css` code { display: block; white-space: pre-wrap; } .pf-l-flex { justify-content: space-between; } .pf-l-flex__item { min-width: 25%; } iframe { width: 100%; height: 50rem; } ` ]; } getModelInfo(context: EventContext): TemplateResult { if (context === null) { return html`-`; } return html`
${t`UID`}
${context.pk as string}
${t`Name`}
${context.name as string}
${t`App`}
${context.app as string}
${t`Model Name`}
${context.model_name as string}
`; } getEmailInfo(context: EventContext): TemplateResult { if (context === null) { return html`-`; } return html`
${t`Message`}
${context.message}
${t`Subject`}
${context.subject}
${t`From`}
${context.from_email}
${t`To`}
${(context.to_email as string[]).map(to => { return html`
  • ${to}
  • `; })}
    `; } defaultResponse(): TemplateResult { return html`

    ${t`Context`}

    ${JSON.stringify(this.event?.context, null, 4)}

    ${t`User`}

    ${JSON.stringify(this.event?.user, null, 4)}
    `; } render(): TemplateResult { if (!this.event) { return html``; } switch (this.event?.action) { case EventMatcherPolicyActionEnum.ModelCreated: case EventMatcherPolicyActionEnum.ModelUpdated: case EventMatcherPolicyActionEnum.ModelDeleted: return html`

    ${t`Affected model:`}

    ${this.getModelInfo(this.event.context?.model as EventContext)} `; case EventMatcherPolicyActionEnum.AuthorizeApplication: return html`

    ${t`Authorized application:`}

    ${this.getModelInfo(this.event.context.authorized_application as EventContext)}

    ${t`Using flow`}

    ${until(new FlowsApi(DEFAULT_CONFIG).flowsInstancesList({ flowUuid: this.event.context.flow as string, }).then(resp => { return html`${resp.results[0].name}`; }), html``)}
    ${this.defaultResponse()}`; case EventMatcherPolicyActionEnum.EmailSent: return html`

    ${t`Email info:`}

    ${this.getEmailInfo(this.event.context)} `; case EventMatcherPolicyActionEnum.SecretView: return html`

    ${t`Secret:`}

    ${this.getModelInfo(this.event.context.secret as EventContext)}`; case EventMatcherPolicyActionEnum.PropertyMappingException: return html`

    ${t`Exception`}

    ${this.event.context.message || this.event.context.error}

    ${t`Expression`}

    ${this.event.context.expression}
    ${this.defaultResponse()}`; case EventMatcherPolicyActionEnum.PolicyException: return html`

    ${t`Binding`}

    ${this.getModelInfo(this.event.context.binding as EventContext)}

    ${t`Request`}

    ${t`Exception`}

    ${this.event.context.message || this.event.context.error}
    ${this.defaultResponse()}`; case EventMatcherPolicyActionEnum.PolicyExecution: return html`

    ${t`Binding`}

    ${this.getModelInfo(this.event.context.binding as EventContext)}

    ${t`Request`}

    ${t`Result`}

    ${this.defaultResponse()}`; case EventMatcherPolicyActionEnum.ConfigurationError: return html`

    ${this.event.context.message}

    ${this.defaultResponse()}`; case EventMatcherPolicyActionEnum.UpdateAvailable: return html`

    ${t`New version available!`}

    ${this.event.context.new_version} `; // Action types which typically don't record any extra context. // If context is not empty, we fall to the default response. case EventMatcherPolicyActionEnum.Login: if ("using_source" in this.event.context) { return html`

    ${t`Using source`}

    ${this.getModelInfo(this.event.context.using_source as EventContext)}
    `; } return this.defaultResponse(); case EventMatcherPolicyActionEnum.LoginFailed: return html`

    ${t`Attempted to log in as ${this.event.context.username}`}

    ${this.defaultResponse()}`; case EventMatcherPolicyActionEnum.Logout: if (this.event.context === {}) { return html`${t`No additional data available.`}`; } return this.defaultResponse(); default: return this.defaultResponse(); } } }