Skip to content

armgit / dashy

Public Imported from GitHub
0

πŸš€ A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!

πŸ›‚ Re-authenticate on expired sub-pages

Alicia Sykes8c1e525cf2dc
diff --git a/src/store.js b/src/store.js
index 6dae3410..a47bc3e3 100644
--- a/src/store.js
+++ b/src/store.js
@@ -58,6 +58,10 @@ const emptyConfig = {
sections: [],
};
+/* One-shot guard against reload loops when a sub-config 401 triggers re-auth.
+ * Set in sessionStorage before reloading, cleared on the next successful sub-config fetch */
+const SUB_CONFIG_RELOAD_KEY = 'dashy.sub-config-reload-attempt';
+
/* Read + JSON-parse a raw localStorage slot, returning undefined on miss/fail. */
const readLocal = (key) => {
const raw = localStorage.getItem(key);
@@ -520,7 +524,18 @@ const store = createStore({
let response;
try {
response = await request.get(subConfigPath, isRemote ? {} : makeBasicAuthHeaders());
+ sessionStorage.removeItem(SUB_CONFIG_RELOAD_KEY);
} catch (fetchErr) {
+ const status = fetchErr.response?.status;
+ const auth = state.rootConfig?.appConfig?.auth || {};
+ const ssoActive = Boolean(auth.enableOidc || auth.enableKeycloak);
+ // Local 401 for OIDC caused by token expiration, trigger reload to re-auth
+ if (status === 401 && ssoActive && !isRemote && !sessionStorage.getItem(SUB_CONFIG_RELOAD_KEY)) {
+ sessionStorage.setItem(SUB_CONFIG_RELOAD_KEY, String(Date.now()));
+ ErrorHandler(`Sub-config '${subConfigPath}' returned 401, reauthentication needed`);
+ window.location.reload();
+ return { ...emptyConfig };
+ }
commit(CRITICAL_ERROR_MSG, `Unable to load config from '${subConfigPath}'`);
ErrorHandler(`Sub-config load failed: ${subConfigPath}`, fetchErr);
return { ...emptyConfig };
diff --git a/src/utils/auth/OidcAuth.js b/src/utils/auth/OidcAuth.js
index c9dbafe2..88fa0d0f 100644
--- a/src/utils/auth/OidcAuth.js
+++ b/src/utils/auth/OidcAuth.js
@@ -11,6 +11,17 @@ import $store from '@/store';
const SIGNIN_GUARD_KEY = 'dashy.oidc.signin-attempt';
const SIGNIN_GUARD_THRESHOLD_MS = 5 * 1000;
+/* Return a same-origin path to navigate back to after IdP, or just `/` */
+const safeReturnTo = (raw) => {
+ if (typeof raw !== 'string') return '/';
+ try {
+ const u = new URL(raw, window.location.origin);
+ return u.origin === window.location.origin ? u.pathname + u.search + u.hash : '/';
+ } catch {
+ return '/';
+ }
+};
+
const getAppConfig = () => {
const Accumulator = new ConfigAccumulator();
return Accumulator.appConfig() || {};
@@ -86,8 +97,9 @@ class OidcAuth {
);
}
this.persistUserInfo(callbackUser);
+ const returnTo = safeReturnTo(callbackUser.state);
toast(i18n.global.t('login.authenticated-redirecting'), { type: 'success' });
- setTimeout(() => window.location.replace('/'), 600);
+ setTimeout(() => window.location.replace(returnTo), 600);
return;
}
@@ -110,7 +122,7 @@ class OidcAuth {
// Fresh token established this run: reload to refetch config with Bearer
if (!hadValidToken && getApiAuthHeader()) {
toast(i18n.global.t('login.authenticated-redirecting'), { type: 'success' });
- setTimeout(() => window.location.replace('/'), 500);
+ setTimeout(() => window.location.reload(), 500);
}
}
@@ -126,7 +138,8 @@ class OidcAuth {
);
}
sessionStorage.setItem(SIGNIN_GUARD_KEY, String(Date.now()));
- await this.userManager.signinRedirect();
+ const returnTo = window.location.pathname + window.location.search + window.location.hash;
+ await this.userManager.signinRedirect({ state: returnTo });
}
/* Mirror the OIDC user into the localStorage keys other parts of Dashy read */