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) {
|
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 => {
|
|
|
|
let relIcon = document.head.querySelector<HTMLLinkElement>("link[rel=icon]");
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
});
|