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-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";
|
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;
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
});
|