Skip to content

Commit 2de498f

Browse files
committed
frontend: components: redux: Add oidc autologin and config
1 parent 836eafc commit 2de498f

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

frontend/src/components/App/Layout.tsx

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import { useQuery } from '@tanstack/react-query';
2626
import { useEffect } from 'react';
2727
import { Trans, useTranslation } from 'react-i18next';
2828
import { useDispatch } from 'react-redux';
29+
import { useLocation } from 'react-router';
30+
import { getAppUrl } from '../../helpers/getAppUrl';
2931
import { getCluster } from '../../lib/cluster';
3032
import { getSelectedClusters } from '../../lib/cluster';
3133
import { useClustersConf } from '../../lib/k8s';
@@ -143,7 +145,11 @@ const fetchConfig = (dispatch: Dispatch<UnknownAction>) => {
143145
clustersToConfig[cluster.name] = cluster;
144146
});
145147

146-
const configToStore = { ...config, clusters: clustersToConfig };
148+
const configToStore = {
149+
...config,
150+
clusters: clustersToConfig,
151+
oidcAutoLogin: config.oidcAutoLogin,
152+
};
147153

148154
if (clusters === null) {
149155
dispatch(setConfig(configToStore));
@@ -188,6 +194,7 @@ export default function Layout({}: LayoutProps) {
188194
const isFullWidth = useTypedSelector(state => state.ui.isFullWidth);
189195
const { t } = useTranslation();
190196
const allClusters = useClustersConf();
197+
const location = useLocation();
191198

192199
/** This fetches the cluster config from the backend and updates the redux store on an interval.
193200
* When stateless clusters are enabled, it also fetches the stateless cluster config from the
@@ -224,6 +231,36 @@ export default function Layout({}: LayoutProps) {
224231

225232
const panels = useUIPanelsGroupedBySide();
226233

234+
const oidcAutoLogin = useTypedSelector(state => state.config.oidcAutoLogin);
235+
236+
useEffect(() => {
237+
if (!oidcAutoLogin || !clusters) {
238+
return;
239+
}
240+
const urlParams = new URLSearchParams(window.location.search);
241+
const isLoggingOut = urlParams.get('logout') === 'true';
242+
if (isLoggingOut || !!error) {
243+
return;
244+
}
245+
const isCallbackPath =
246+
window.location.pathname.includes('oidc-callback') ||
247+
urlParams.has('code') ||
248+
urlParams.has('state');
249+
if (isCallbackPath) {
250+
return;
251+
}
252+
const currentClusterName = getCluster();
253+
const currentCluster = currentClusterName ? clusters[currentClusterName] : null;
254+
const isOIDC = currentCluster?.auth_type === 'oidc';
255+
if (!isOIDC) {
256+
return;
257+
}
258+
if (currentCluster.useToken === undefined) {
259+
const oauthUrl = `${getAppUrl()}oidc?dt=${Date.now()}&cluster=${getCluster()}`;
260+
window.location.href = oauthUrl;
261+
}
262+
}, [oidcAutoLogin, clusters, error, location.pathname]);
263+
227264
if (!disableBackendLoader) {
228265
if (error && !config) {
229266
return <ErrorPage message={<Trans>Failed to connect to the backend</Trans>} error={error} />;

frontend/src/redux/configSlice.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ export interface ConfigState {
5757
useEvict: boolean;
5858
[key: string]: any;
5959
};
60+
/**
61+
* Whether OIDC auto-login is enabled. Null indicates the value hasn't been loaded from the backend yet.
62+
*/
63+
oidcAutoLogin?: boolean | null;
6064
}
6165

6266
export const defaultTableRowsPerPageOptions = [15, 25, 50];
@@ -71,6 +75,7 @@ export const initialState: ConfigState = {
7175
clusters: null,
7276
statelessClusters: null,
7377
allClusters: null,
78+
oidcAutoLogin: null,
7479
settings: {
7580
tableRowsPerPageOptions:
7681
storedSettings.tableRowsPerPageOptions || defaultTableRowsPerPageOptions,
@@ -89,8 +94,15 @@ const configSlice = createSlice({
8994
* @param state - The current state.
9095
* @param action - The payload action containing the config.
9196
*/
92-
setConfig(state, action: PayloadAction<{ clusters: ConfigState['clusters'] }>) {
97+
setConfig(
98+
state,
99+
action: PayloadAction<{ clusters: ConfigState['clusters']; oidcAutoLogin?: boolean }>
100+
) {
93101
state.clusters = action.payload.clusters;
102+
103+
if (state.oidcAutoLogin !== undefined) {
104+
state.oidcAutoLogin = action.payload.oidcAutoLogin;
105+
}
94106
},
95107
/**
96108
* Save the config. To both the store, and localStorage.

0 commit comments

Comments
 (0)