@openshift-console/dynamic-plugin-sdk
Version:
Provides core APIs, types and utilities used by dynamic plugins at runtime.
37 lines (36 loc) • 1.32 kB
JavaScript
import { getImpersonate } from '../../app/core/reducers';
import storeHandler from '../../app/storeHandler';
export const getCSRFToken = () => {
const cookiePrefix = 'csrf-token=';
return (document &&
document.cookie &&
document.cookie
.split(';')
.map((c) => c.trim())
.filter((c) => c.startsWith(cookiePrefix))
.map((c) => c.slice(cookiePrefix.length))
.pop());
};
/**
* A function that creates impersonation headers for API requests using current redux state.
* @returns an object containing the appropriate impersonation requst headers, based on redux state
*/
export const getConsoleRequestHeaders = () => {
const store = storeHandler.getStore();
if (!store)
return undefined;
const state = store.getState();
const headers = {
'X-CSRFToken': getCSRFToken(),
};
// Set impersonation headers
const { kind, name } = getImpersonate(state) || {};
if ((kind === 'User' || kind === 'Group') && name) {
// Even if we are impersonating a group, we still need to set Impersonate-User to something or k8s will complain
headers['Impersonate-User'] = name;
if (kind === 'Group') {
headers['Impersonate-Group'] = name;
}
}
return headers;
};