Skip to content

Commit

Permalink
Merge pull request RedHatInsights#2719 from Hyperkid123/platform-url
Browse files Browse the repository at this point in the history
Ensure platform URL will always end with trailing /
  • Loading branch information
Hyperkid123 authored Dec 4, 2023
2 parents 995742d + 068400b commit 7031d66
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
4 changes: 1 addition & 3 deletions src/auth/OIDCConnector/OIDCProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ const OIDCProvider: React.FC<React.PropsWithChildren> = ({ children }) => {
config: { ssoUrl },
},
} = data;
// add trailing slash if missing
const sanitizedSSOUrl = `${ssoUrl.replace(/\/$/, '')}/`;
setState({ ssoUrl: platformUrl(DEFAULT_SSO_ROUTES, sanitizedSSOUrl), microFrontendConfig: data });
setState({ ssoUrl: platformUrl(DEFAULT_SSO_ROUTES, ssoUrl), microFrontendConfig: data });
} catch (error) {
setState({ ssoUrl: platformUrl(DEFAULT_SSO_ROUTES), microFrontendConfig: data });
}
Expand Down
8 changes: 5 additions & 3 deletions src/auth/platformUrl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ describe('platformUrl', () => {
it('should return dev sso url if env is set to console.dev', () => {
window.location.hostname = 'console.dev.redhat.com';
const ssourl = platformUrl(DEFAULT_SSO_ROUTES);
expect(ssourl).toBe(DEFAULT_SSO_ROUTES.dev.sso);
expect(ssourl).toBe(DEFAULT_SSO_ROUTES.dev.sso + '/');
// don't forget to reset the hostname for other tests
window.location.hostname = '';
});

it('should return custom sso url if provided', () => {
const customSsoUrl = 'https://custom.sso.url';
const ssourl = platformUrl(DEFAULT_SSO_ROUTES, customSsoUrl);
expect(ssourl).toBe(customSsoUrl);
expect(ssourl).toBe(customSsoUrl + '/');
});

// test for all envs using the DEFAULT_SSO_ROUTES
Expand All @@ -34,7 +34,9 @@ describe('platformUrl', () => {
it(`should return ${env} sso url if env is set to ${url}`, () => {
window.location.hostname = url;
const ssourl = platformUrl(DEFAULT_SSO_ROUTES);
expect(ssourl).toBe(DEFAULT_SSO_ROUTES[env as keyof typeof DEFAULT_SSO_ROUTES].sso);
expect(ssourl).toMatch(new RegExp(DEFAULT_SSO_ROUTES[env as keyof typeof DEFAULT_SSO_ROUTES].sso));
// Must always end with trailing slash
expect(ssourl).toMatch(/\/$/);
// don't forget to reset the hostname for other tests
window.location.hostname = '';
});
Expand Down
12 changes: 9 additions & 3 deletions src/auth/platformUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@ import { DEFAULT_SSO_ROUTES } from '../utils/common';
import logger from './logger';
const log = logger('auth/platform.ts');

// add trailing slash if missing
function sanitizeUrl(url: string) {
return `${url.replace(/\/$/, '')}/`;
}

// Parse through keycloak options routes
export default function platformUlr(env: typeof DEFAULT_SSO_ROUTES, configSsoUrl?: string) {
// we have to use hard coded value for console.dev.redhat.com
// ugly hack

if (location.hostname === 'console.dev.redhat.com') {
return DEFAULT_SSO_ROUTES.dev.sso;
return sanitizeUrl(DEFAULT_SSO_ROUTES.dev.sso);
}
if (configSsoUrl) {
return configSsoUrl;
return sanitizeUrl(configSsoUrl);
}

const ssoEnv = Object.entries(env).find(([, { url }]) => url.includes(location.hostname));

if (ssoEnv) {
log(`SSO Url: ${ssoEnv?.[1].sso}`);
log(`Current env: ${ssoEnv?.[0]}`);
return ssoEnv?.[1].sso;
return sanitizeUrl(ssoEnv?.[1].sso);
} else {
log('SSO url: not found, defaulting to qa');
log('Current env: not found, defaulting to qa');
Expand Down

0 comments on commit 7031d66

Please sign in to comment.