Files
authentik/web/src/common/api/middleware.ts
Jens L. 0cf6bff93c tests/e2e: add test for authentication flow in compatibility mode (#14392)
* tests/e2e: add test for authentication flow in compatibility mode

Signed-off-by: Jens Langhammer <jens@goauthentik.io>

* web: Add prefix class to CSS for easier debugging of constructed stylesheets.

- Use CSS variables for highlighter.

* web: Fix issue where MDX components apply styles out of order.

* web: Fix hover color.

* web: Fix CSS module types. Clean up globals.

* web: Fix issues surrounding availability of shadow root in compatibility mode.

* web: Fix typo.

* web: Partial fixes for storybook dark theme.

* web: Fix overflow.

* web: Fix issues surrounding competing interfaces attempting to apply styles.

* fix padding in ak-alert in. markdown

Signed-off-by: Jens Langhammer <jens@goauthentik.io>

* web: Minimize use of sub-module exports.

---------

Signed-off-by: Jens Langhammer <jens@goauthentik.io>
Co-authored-by: Teffen Ellis <teffen@sister.software>
2025-05-15 16:51:11 +02:00

66 lines
1.9 KiB
TypeScript

import { EVENT_REQUEST_POST } from "@goauthentik/common/constants.js";
import { getCookie } from "@goauthentik/common/utils.js";
import {
CurrentBrand,
FetchParams,
Middleware,
RequestContext,
ResponseContext,
} from "@goauthentik/api";
export const CSRFHeaderName = "X-authentik-CSRF";
export interface RequestInfo {
time: number;
method: string;
path: string;
status: number;
}
export class LoggingMiddleware implements Middleware {
brand: CurrentBrand;
constructor(brand: CurrentBrand) {
this.brand = brand;
}
post(context: ResponseContext): Promise<Response | void> {
let msg = `authentik/api[${this.brand.matchedDomain}]: `;
// https://developer.mozilla.org/en-US/docs/Web/API/console#styling_console_output
msg += `%c${context.response.status}%c ${context.init.method} ${context.url}`;
let style = "";
if (context.response.status >= 400) {
style = "color: red; font-weight: bold;";
}
console.debug(msg, style, "");
return Promise.resolve(context.response);
}
}
export class CSRFMiddleware implements Middleware {
pre?(context: RequestContext): Promise<FetchParams | void> {
// @ts-ignore
context.init.headers[CSRFHeaderName] = getCookie("authentik_csrf");
return Promise.resolve(context);
}
}
export class EventMiddleware implements Middleware {
post?(context: ResponseContext): Promise<Response | void> {
const request: RequestInfo = {
time: new Date().getTime(),
method: (context.init.method || "GET").toUpperCase(),
path: context.url,
status: context.response.status,
};
window.dispatchEvent(
new CustomEvent(EVENT_REQUEST_POST, {
bubbles: true,
composed: true,
detail: request,
}),
);
return Promise.resolve(context.response);
}
}