diff --git a/public/apps/login/login-page.tsx b/public/apps/login/login-page.tsx index 70d894781..a22a36dc7 100644 --- a/public/apps/login/login-page.tsx +++ b/public/apps/login/login-page.tsx @@ -35,6 +35,7 @@ import { OPENID_AUTH_LOGIN_WITH_FRAGMENT, SAML_AUTH_LOGIN_WITH_FRAGMENT, } from '../../../common'; +import { getSavedTenant } from '../../utils/storage-utils'; interface LoginPageDeps { http: CoreStart['http']; @@ -49,8 +50,7 @@ interface LoginButtonConfig { buttonstyle: string; } -function redirect(serverBasePath: string) { - // navigate to nextUrl +export function getNextPath(serverBasePath: string) { const urlParams = new URLSearchParams(window.location.search); let nextUrl = urlParams.get('nextUrl'); if (!nextUrl || nextUrl.toLowerCase().includes('//')) { @@ -58,7 +58,26 @@ function redirect(serverBasePath: string) { // redirect to '/'. nextUrl = serverBasePath + '/'; } - window.location.href = nextUrl + window.location.hash; + const savedTenant = getSavedTenant(); + const url = new URL( + window.location.protocol + '//' + window.location.host + nextUrl + window.location.hash + ); + if ( + !!savedTenant && + !( + url.searchParams.has('security_tenant') || + url.searchParams.has('securitytenant') || + url.searchParams.has('securityTenant_') + ) + ) { + url.searchParams.append('security_tenant', savedTenant); + } + return url.pathname + url.search + url.hash; +} + +function redirect(serverBasePath: string) { + // navigate to nextUrl + window.location.href = getNextPath(serverBasePath); } export function extractNextUrlFromWindowLocation(): string { diff --git a/public/apps/login/test/login-page.test.tsx b/public/apps/login/test/login-page.test.tsx index f21a39e5a..8d1c76358 100644 --- a/public/apps/login/test/login-page.test.tsx +++ b/public/apps/login/test/login-page.test.tsx @@ -16,11 +16,12 @@ import { shallow } from 'enzyme'; import React from 'react'; import { ClientConfigType } from '../../../types'; -import { LoginPage, extractNextUrlFromWindowLocation } from '../login-page'; +import { LoginPage, extractNextUrlFromWindowLocation, getNextPath } from '../login-page'; import { validateCurrentPassword } from '../../../utils/login-utils'; import { API_AUTH_LOGOUT } from '../../../../common'; import { chromeServiceMock } from '../../../../../../src/core/public/mocks'; import { AuthType } from '../../../../common'; +import { setSavedTenant } from '../../../utils/storage-utils'; jest.mock('../../../utils/login-utils', () => ({ validateCurrentPassword: jest.fn(), @@ -85,6 +86,45 @@ describe('test extractNextUrlFromWindowLocation', () => { }); }); +describe('test redirect', () => { + test('extract redirect excludes security_tenant when no tenant in local storage', () => { + // Trick to mock window.location + const originalLocation = window.location; + delete window.location; + window.location = new URL('http://localhost:5601/app/login?nextUrl=%2Fapp%2Fdashboards') as any; + setSavedTenant(null); + const nextPath = getNextPath(''); + expect(nextPath).toEqual('/app/dashboards'); + window.location = originalLocation; + }); + + test('extract redirect includes security_tenant when tenant in local storage', () => { + const originalLocation = window.location; + delete window.location; + window.location = new URL('http://localhost:5601/app/login?nextUrl=%2Fapp%2Fdashboards'); + setSavedTenant('custom'); + const nextPath = getNextPath(''); + expect(nextPath).toEqual('/app/dashboards?security_tenant=custom'); + setSavedTenant(null); + window.location = originalLocation; + }); + + test('extract redirect includes security_tenant when tenant in local storage, existing url params and hash', () => { + const originalLocation = window.location; + delete window.location; + window.location = new URL( + "http://localhost:5601/app/login?nextUrl=%2Fapp%2Fdashboards?param1=value1#/view/7adfa750-4c81-11e8-b3d7-01146121b73d?_g=(filters:!(),refreshInterval:(pause:!f,value:900000),time:(from:now-24h,to:now))&_a=(description:'Analyze%20mock%20flight%20data%20for%20OpenSearch-Air,%20Logstash%20Airways,%20OpenSearch%20Dashboards%20Airlines%20and%20BeatsWest',filters:!(),fullScreenMode:!f,options:(hidePanelTitles:!f,useMargins:!t),query:(language:kuery,query:''),timeRestore:!t,title:'%5BFlights%5D%20Global%20Flight%20Dashboard',viewMode:view)" + ); + setSavedTenant('custom'); + const nextPath = getNextPath(''); + expect(nextPath).toEqual( + "/app/dashboards?param1=value1&security_tenant=custom#/view/7adfa750-4c81-11e8-b3d7-01146121b73d?_g=(filters:!(),refreshInterval:(pause:!f,value:900000),time:(from:now-24h,to:now))&_a=(description:'Analyze%20mock%20flight%20data%20for%20OpenSearch-Air,%20Logstash%20Airways,%20OpenSearch%20Dashboards%20Airlines%20and%20BeatsWest',filters:!(),fullScreenMode:!f,options:(hidePanelTitles:!f,useMargins:!t),query:(language:kuery,query:''),timeRestore:!t,title:'%5BFlights%5D%20Global%20Flight%20Dashboard',viewMode:view)" + ); + setSavedTenant(null); + window.location = originalLocation; + }); +}); + describe('Login page', () => { let chrome: ReturnType; const mockHttpStart = {