Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing strange param behaviour with sso #1009

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/application/ApplicationThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,12 @@
if (urlParams.get('credentials')) {
setWelcomeScreenOpen(false);
const connection = decodeURIComponent(urlParams.get('credentials'));
const protocol = connection.split('://')[0];

Check warning on line 267 in src/application/ApplicationThunks.ts

View workflow job for this annotation

GitHub Actions / build-test (18.x)

Use array destructuring
const username = connection.split('://')[1].split(':')[0];

Check warning on line 268 in src/application/ApplicationThunks.ts

View workflow job for this annotation

GitHub Actions / build-test (18.x)

Use array destructuring
const password = connection.split('://')[1].split(':')[1].split('@')[0];

Check warning on line 269 in src/application/ApplicationThunks.ts

View workflow job for this annotation

GitHub Actions / build-test (18.x)

Use array destructuring
const database = connection.split('@')[1].split(':')[0];

Check warning on line 270 in src/application/ApplicationThunks.ts

View workflow job for this annotation

GitHub Actions / build-test (18.x)

Use array destructuring
const url = connection.split('@')[1].split(':')[1];

Check warning on line 271 in src/application/ApplicationThunks.ts

View workflow job for this annotation

GitHub Actions / build-test (18.x)

Use array destructuring
const port = connection.split('@')[1].split(':')[2];

Check warning on line 272 in src/application/ApplicationThunks.ts

View workflow job for this annotation

GitHub Actions / build-test (18.x)

Use array destructuring

dispatch(setConnectionModalOpen(false));
dispatch(
Expand All @@ -291,7 +291,6 @@
if (skipConfirmation === true) {
dispatch(onConfirmLoadSharedDashboardThunk());
}

window.history.pushState({}, document.title, window.location.pathname);
} else {
dispatch(setConnectionModalOpen(false));
Expand Down Expand Up @@ -358,7 +357,6 @@
}
if (shareDetails.standalone == true) {
dispatch(setStandaloneMode(true));
localStorage.setItem('standaloneShared', 'true'); // EDGE CASE: redirect SSO removes the shareDetails when redirecting
}
dispatch(resetShareDetails());
} catch (e) {
Expand Down Expand Up @@ -410,29 +408,33 @@

try {
// Parse the URL parameters to see if there's any deep linking of parameters.
const state = getState();
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
if (state.application.waitForSSO) {
const paramsBeforeSSO = JSON.parse(sessionStorage.getItem('SSO_PARAMS_BEFORE_REDIRECT') || '{}');
Object.entries(paramsBeforeSSO).forEach(([key, value]) => {
urlParams.set(key, value);
});
}
const paramsToSetAfterConnecting = {};
Array.from(urlParams.entries()).forEach(([key, value]) => {
if (key.startsWith('neodash_')) {
paramsToSetAfterConnecting[key] = value;
}
});

sessionStorage.getItem('SSO_PARAMS_BEFORE_REDIRECT');
const page = urlParams.get('page');
if (page !== '' && page !== null) {
if (!isNaN(page)) {
dispatch(setPageNumberThunk(parseInt(page)));
}
}
const state = getState();
dispatch(setSSOEnabled(config.ssoEnabled, state.application.cachedSSODiscoveryUrl));
dispatch(setSSOProviders(config.ssoProviders));

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

// if a dashboard database was previously set, remember to use it.
const dashboardDatabase = state.application.standaloneDashboardDatabase;
Expand All @@ -455,7 +457,6 @@
config.standaloneDatabaseList
)
);
localStorage.removeItem('standaloneShared');

dispatch(setLoggingMode(config.loggingMode));
dispatch(setLoggingDatabase(config.loggingDatabase));
Expand Down Expand Up @@ -530,14 +531,18 @@
}

if (standalone) {
if (config.standaloneDashboardURL !== undefined && config.standaloneDashboardURL.length > 0) {
if (urlParams.get('id')) {
dispatch(setDashboardToLoadAfterConnecting(urlParams.get('id')));
} else if (config.standaloneDashboardURL !== undefined && config.standaloneDashboardURL.length > 0) {
dispatch(setDashboardToLoadAfterConnecting(config.standaloneDashboardURL));
} else {
dispatch(setDashboardToLoadAfterConnecting(`name:${config.standaloneDashboardName}`));
}
dispatch(setParametersToLoadAfterConnecting(paramsToSetAfterConnecting));
}
sessionStorage.removeItem('SSO_PARAMS_BEFORE_REDIRECT');
});

dispatch(setWaitForSSO(false));
if (!success) {
alert('Unable to connect using SSO. See the browser console for more details.');
Expand All @@ -550,6 +555,12 @@
} else {
return;
}
} else if (state.application.ssoEnabled && !state.application.waitForSSO && urlParams) {
let paramsToStore = {};
urlParams.forEach((value, key) => {
paramsToStore[key] = value;
});
sessionStorage.setItem('SSO_PARAMS_BEFORE_REDIRECT', JSON.stringify(paramsToStore));
}

if (standalone) {
Expand All @@ -558,7 +569,7 @@
dispatch(initializeApplicationAsEditorThunk(config, paramsToSetAfterConnecting));
}
} catch (e) {
console.log(e);

Check warning on line 572 in src/application/ApplicationThunks.ts

View workflow job for this annotation

GitHub Actions / build-test (18.x)

Unexpected console statement
dispatch(setWelcomeScreenOpen(false));
dispatch(
createNotificationThunk(
Expand Down
13 changes: 10 additions & 3 deletions src/component/sso/SSOLoginButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Button, IconButton } from '@neo4j-ndl/react';

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

const filterByProvidersList = (discoveredProviders, validProviders) => {
return validProviders == null || validProviders.length == 0
Expand All @@ -15,19 +15,26 @@ export const SSOLoginButton = ({ discoveryAPIUrl, hostname, port, onSSOAttempt,
};
const attemptManualSSOProviderRetrieval = () => {
// Do an extra check to see if the hostname provides some SSO provider configuration.
getDiscoveryDataInfo(`https://${hostname}:${port}`)
const protocol = isLocalhost(hostname) ? 'http' : 'https';
const discoveryUrl = `${protocol}://${hostname}:${port}`;
getDiscoveryDataInfo(discoveryUrl)
.then((mergedSSOProviders) => {
setSSOProviders(filterByProvidersList(mergedSSOProviders, providers));
if (mergedSSOProviders.length == 0) {
setDiscoveryUrlValidated(undefined);
} else {
setDiscoveryUrlValidated(`https://${hostname}:${port}`);
setDiscoveryUrlValidated(discoveryUrl);
}
})
// eslint-disable-next-line no-console
.catch((err) => console.error('Error in getDiscoveryDataInfo of Login component', err));
};

function isLocalhost(hostname) {
const localhostNames = ['localhost', '127.0.0.1', '::1'];
return localhostNames.includes(hostname);
}

useEffect(() => {
// First, try to get the SSO discovery URL from the config.json configuration file and see if it contains anything.
getDiscoveryDataInfo(discoveryAPIUrl)
Expand Down
Loading