2022-01-16 15:10:55 +00:00
|
|
|
import { Config, Configuration, CoreApi, CurrentTenant, FetchParams, Middleware, RequestContext, ResponseContext, RootApi } from "@goauthentik/api";
|
2021-03-08 10:14:00 +00:00
|
|
|
import { getCookie } from "../utils";
|
2021-08-05 20:04:31 +00:00
|
|
|
import { APIMiddleware } from "../elements/notifications/APIDrawer";
|
2021-03-27 22:18:51 +00:00
|
|
|
import { MessageMiddleware } from "../elements/messages/Middleware";
|
2021-09-02 15:10:43 +00:00
|
|
|
import { VERSION } from "../constants";
|
2020-11-26 21:37:41 +00:00
|
|
|
|
2021-04-03 21:06:57 +00:00
|
|
|
export class LoggingMiddleware implements Middleware {
|
|
|
|
|
|
|
|
post(context: ResponseContext): Promise<Response | void> {
|
2021-05-29 17:47:55 +00:00
|
|
|
tenant().then(tenant => {
|
2021-07-04 10:38:50 +00:00
|
|
|
let msg = `authentik/api[${tenant.matchedDomain}]: `;
|
|
|
|
msg += `${context.response.status} ${context.init.method} ${context.url}`;
|
2021-08-08 22:38:43 +00:00
|
|
|
console.debug(msg);
|
2021-05-29 17:47:55 +00:00
|
|
|
});
|
2021-04-03 21:06:57 +00:00
|
|
|
return Promise.resolve(context.response);
|
|
|
|
}
|
2021-04-13 16:35:26 +00:00
|
|
|
|
2021-04-03 21:06:57 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 21:49:30 +00:00
|
|
|
let globalConfigPromise: Promise<Config>;
|
|
|
|
export function config(): Promise<Config> {
|
|
|
|
if (!globalConfigPromise) {
|
2021-05-16 15:49:37 +00:00
|
|
|
globalConfigPromise = new RootApi(DEFAULT_CONFIG).rootConfigRetrieve();
|
2021-04-22 21:49:30 +00:00
|
|
|
}
|
|
|
|
return globalConfigPromise;
|
|
|
|
}
|
|
|
|
|
2021-05-29 15:35:56 +00:00
|
|
|
let globalTenantPromise: Promise<CurrentTenant>;
|
|
|
|
export function tenant(): Promise<CurrentTenant> {
|
|
|
|
if (!globalTenantPromise) {
|
2021-06-02 12:38:24 +00:00
|
|
|
globalTenantPromise = new CoreApi(DEFAULT_CONFIG).coreTenantsCurrentRetrieve().then(tenant => {
|
2021-06-02 19:19:39 +00:00
|
|
|
/**
|
|
|
|
* <link rel="icon" href="/static/dist/assets/icons/icon.png">
|
|
|
|
* <link rel="shortcut icon" href="/static/dist/assets/icons/icon.png">
|
|
|
|
*/
|
|
|
|
const rels = ["icon", "shortcut icon"];
|
|
|
|
rels.forEach(rel => {
|
2021-06-02 19:47:56 +00:00
|
|
|
let relIcon = document.head.querySelector<HTMLLinkElement>(`link[rel='${rel}']`);
|
2021-06-02 19:19:39 +00:00
|
|
|
if (!relIcon) {
|
|
|
|
relIcon = document.createElement('link');
|
|
|
|
relIcon.rel = rel;
|
|
|
|
document.getElementsByTagName('head')[0].appendChild(relIcon);
|
|
|
|
}
|
2021-06-02 19:29:20 +00:00
|
|
|
relIcon.href = tenant.brandingFavicon;
|
2021-06-02 19:19:39 +00:00
|
|
|
})
|
2021-06-02 12:38:24 +00:00
|
|
|
return tenant;
|
|
|
|
});
|
2021-05-29 15:35:56 +00:00
|
|
|
}
|
|
|
|
return globalTenantPromise;
|
|
|
|
}
|
|
|
|
|
2022-01-16 15:10:55 +00:00
|
|
|
export class CSRFMiddleware implements Middleware {
|
|
|
|
pre?(context: RequestContext): Promise<FetchParams | void> {
|
|
|
|
// @ts-ignore
|
2022-01-16 15:17:44 +00:00
|
|
|
context.init.headers["X-authentik-CSRF"] = getCookie("authentik_csrf");
|
2022-01-16 15:10:55 +00:00
|
|
|
return Promise.resolve(context);
|
2022-01-12 22:14:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-01 09:15:52 +00:00
|
|
|
export function getMetaContent(key: string): string {
|
|
|
|
const metaEl = document.querySelector<HTMLMetaElement>(`meta[name=${key}]`);
|
|
|
|
if (!metaEl) return "";
|
|
|
|
return metaEl.content;
|
|
|
|
}
|
|
|
|
|
2021-03-08 10:14:00 +00:00
|
|
|
export const DEFAULT_CONFIG = new Configuration({
|
2021-10-14 17:45:20 +00:00
|
|
|
basePath: process.env.AK_API_BASE_PATH + "/api/v3",
|
2021-03-08 10:14:00 +00:00
|
|
|
headers: {
|
2022-06-01 09:15:52 +00:00
|
|
|
"sentry-trace": getMetaContent("sentry-trace"),
|
2021-03-24 20:16:03 +00:00
|
|
|
},
|
|
|
|
middleware: [
|
2022-01-16 15:10:55 +00:00
|
|
|
new CSRFMiddleware(),
|
2021-08-05 20:04:31 +00:00
|
|
|
new APIMiddleware(),
|
2021-03-27 22:18:51 +00:00
|
|
|
new MessageMiddleware(),
|
2021-04-03 21:06:57 +00:00
|
|
|
new LoggingMiddleware(),
|
2021-03-24 20:16:03 +00:00
|
|
|
],
|
2021-03-08 10:14:00 +00:00
|
|
|
});
|
2021-09-02 15:10:43 +00:00
|
|
|
|
2021-11-06 12:19:39 +00:00
|
|
|
// This is just a function so eslint doesn't complain about
|
|
|
|
// missing-whitespace-between-attributes or
|
|
|
|
// unexpected-character-in-attribute-name
|
|
|
|
export function AndNext(url: string): string {
|
|
|
|
return `?next=${encodeURIComponent(url)}`;
|
|
|
|
}
|
|
|
|
|
2021-10-14 17:45:20 +00:00
|
|
|
console.debug(`authentik(early): version ${VERSION}, apiBase ${DEFAULT_CONFIG.basePath}`);
|