Skip to content

Commit f3ae774

Browse files
Merge pull request #1009 from neo4j-labs/fix/sso-standalone-params
fixing strange param behaviour with sso
2 parents 7fea3eb + 563a1d6 commit f3ae774

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

src/application/ApplicationThunks.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ export const handleSharedDashboardsThunk = () => (dispatch: any) => {
291291
if (skipConfirmation === true) {
292292
dispatch(onConfirmLoadSharedDashboardThunk());
293293
}
294-
295294
window.history.pushState({}, document.title, window.location.pathname);
296295
} else {
297296
dispatch(setConnectionModalOpen(false));
@@ -358,7 +357,6 @@ export const onConfirmLoadSharedDashboardThunk = () => (dispatch: any, getState:
358357
}
359358
if (shareDetails.standalone == true) {
360359
dispatch(setStandaloneMode(true));
361-
localStorage.setItem('standaloneShared', 'true'); // EDGE CASE: redirect SSO removes the shareDetails when redirecting
362360
}
363361
dispatch(resetShareDetails());
364362
} catch (e) {
@@ -410,29 +408,33 @@ export const loadApplicationConfigThunk = () => async (dispatch: any, getState:
410408

411409
try {
412410
// Parse the URL parameters to see if there's any deep linking of parameters.
411+
const state = getState();
413412
const queryString = window.location.search;
414413
const urlParams = new URLSearchParams(queryString);
414+
if (state.application.waitForSSO) {
415+
const paramsBeforeSSO = JSON.parse(sessionStorage.getItem('SSO_PARAMS_BEFORE_REDIRECT') || '{}');
416+
Object.entries(paramsBeforeSSO).forEach(([key, value]) => {
417+
urlParams.set(key, value);
418+
});
419+
}
415420
const paramsToSetAfterConnecting = {};
416421
Array.from(urlParams.entries()).forEach(([key, value]) => {
417422
if (key.startsWith('neodash_')) {
418423
paramsToSetAfterConnecting[key] = value;
419424
}
420425
});
421-
426+
sessionStorage.getItem('SSO_PARAMS_BEFORE_REDIRECT');
422427
const page = urlParams.get('page');
423428
if (page !== '' && page !== null) {
424429
if (!isNaN(page)) {
425430
dispatch(setPageNumberThunk(parseInt(page)));
426431
}
427432
}
428-
const state = getState();
429433
dispatch(setSSOEnabled(config.ssoEnabled, state.application.cachedSSODiscoveryUrl));
430434
dispatch(setSSOProviders(config.ssoProviders));
431435

432436
// Check if we are in standalone mode
433-
// const standaloneShared = localStorage.getItem('standaloneShared') == 'true'; // EDGE case: from url param it could happen that we lose the value due to SSO redirect
434-
const { standalone } = config;
435-
// || standaloneShared;
437+
const standalone = config.standalone || urlParams.get('standalone') == 'Yes';
436438

437439
// if a dashboard database was previously set, remember to use it.
438440
const dashboardDatabase = state.application.standaloneDashboardDatabase;
@@ -455,7 +457,6 @@ export const loadApplicationConfigThunk = () => async (dispatch: any, getState:
455457
config.standaloneDatabaseList
456458
)
457459
);
458-
localStorage.removeItem('standaloneShared');
459460

460461
dispatch(setLoggingMode(config.loggingMode));
461462
dispatch(setLoggingDatabase(config.loggingDatabase));
@@ -530,14 +531,18 @@ export const loadApplicationConfigThunk = () => async (dispatch: any, getState:
530531
}
531532

532533
if (standalone) {
533-
if (config.standaloneDashboardURL !== undefined && config.standaloneDashboardURL.length > 0) {
534+
if (urlParams.get('id')) {
535+
dispatch(setDashboardToLoadAfterConnecting(urlParams.get('id')));
536+
} else if (config.standaloneDashboardURL !== undefined && config.standaloneDashboardURL.length > 0) {
534537
dispatch(setDashboardToLoadAfterConnecting(config.standaloneDashboardURL));
535538
} else {
536539
dispatch(setDashboardToLoadAfterConnecting(`name:${config.standaloneDashboardName}`));
537540
}
538541
dispatch(setParametersToLoadAfterConnecting(paramsToSetAfterConnecting));
539542
}
543+
sessionStorage.removeItem('SSO_PARAMS_BEFORE_REDIRECT');
540544
});
545+
541546
dispatch(setWaitForSSO(false));
542547
if (!success) {
543548
alert('Unable to connect using SSO. See the browser console for more details.');
@@ -550,6 +555,12 @@ export const loadApplicationConfigThunk = () => async (dispatch: any, getState:
550555
} else {
551556
return;
552557
}
558+
} else if (state.application.ssoEnabled && !state.application.waitForSSO && urlParams) {
559+
let paramsToStore = {};
560+
urlParams.forEach((value, key) => {
561+
paramsToStore[key] = value;
562+
});
563+
sessionStorage.setItem('SSO_PARAMS_BEFORE_REDIRECT', JSON.stringify(paramsToStore));
553564
}
554565

555566
if (standalone) {

src/component/sso/SSOLoginButton.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Button, IconButton } from '@neo4j-ndl/react';
66

77
export const SSOLoginButton = ({ discoveryAPIUrl, hostname, port, onSSOAttempt, onClick, providers }) => {
88
const [savedSSOProviders, setSSOProviders] = useState([]);
9-
const [discoveryUrlValidated, setDiscoveryUrlValidated] = useState(undefined);
9+
const [discoveryUrlValidated, setDiscoveryUrlValidated] = useState<string | undefined>(undefined);
1010

1111
const filterByProvidersList = (discoveredProviders, validProviders) => {
1212
return validProviders == null || validProviders.length == 0
@@ -15,19 +15,26 @@ export const SSOLoginButton = ({ discoveryAPIUrl, hostname, port, onSSOAttempt,
1515
};
1616
const attemptManualSSOProviderRetrieval = () => {
1717
// Do an extra check to see if the hostname provides some SSO provider configuration.
18-
getDiscoveryDataInfo(`https://${hostname}:${port}`)
18+
const protocol = isLocalhost(hostname) ? 'http' : 'https';
19+
const discoveryUrl = `${protocol}://${hostname}:${port}`;
20+
getDiscoveryDataInfo(discoveryUrl)
1921
.then((mergedSSOProviders) => {
2022
setSSOProviders(filterByProvidersList(mergedSSOProviders, providers));
2123
if (mergedSSOProviders.length == 0) {
2224
setDiscoveryUrlValidated(undefined);
2325
} else {
24-
setDiscoveryUrlValidated(`https://${hostname}:${port}`);
26+
setDiscoveryUrlValidated(discoveryUrl);
2527
}
2628
})
2729
// eslint-disable-next-line no-console
2830
.catch((err) => console.error('Error in getDiscoveryDataInfo of Login component', err));
2931
};
3032

33+
function isLocalhost(hostname) {
34+
const localhostNames = ['localhost', '127.0.0.1', '::1'];
35+
return localhostNames.includes(hostname);
36+
}
37+
3138
useEffect(() => {
3239
// First, try to get the SSO discovery URL from the config.json configuration file and see if it contains anything.
3340
getDiscoveryDataInfo(discoveryAPIUrl)

0 commit comments

Comments
 (0)