2021-05-29 15:35:56 +00:00
|
|
|
import { Config, Configuration, CoreApi, CurrentTenant, Middleware, ResponseContext, RootApi, Tenant } from "authentik-api";
|
2021-03-08 10:14:00 +00:00
|
|
|
import { getCookie } from "../utils";
|
2021-03-27 22:18:51 +00:00
|
|
|
import { API_DRAWER_MIDDLEWARE } from "../elements/notifications/APIDrawer";
|
|
|
|
import { MessageMiddleware } from "../elements/messages/Middleware";
|
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 => {
|
|
|
|
console.debug(`authentik/api[${tenant.matchedDomain}]: ${context.response.status} ${context.init.method} ${context.url}`);
|
|
|
|
});
|
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) {
|
|
|
|
globalTenantPromise = new CoreApi(DEFAULT_CONFIG).coreTenantsCurrentRetrieve();
|
|
|
|
}
|
|
|
|
return globalTenantPromise;
|
|
|
|
}
|
|
|
|
|
2021-03-08 10:14:00 +00:00
|
|
|
export const DEFAULT_CONFIG = new Configuration({
|
2021-05-17 18:48:58 +00:00
|
|
|
basePath: "",
|
2021-03-08 10:14:00 +00:00
|
|
|
headers: {
|
|
|
|
"X-CSRFToken": getCookie("authentik_csrf"),
|
2021-03-24 20:16:03 +00:00
|
|
|
},
|
|
|
|
middleware: [
|
2021-03-27 22:18:51 +00:00
|
|
|
API_DRAWER_MIDDLEWARE,
|
|
|
|
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
|
|
|
});
|