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

Add a util function to generate the relative redirectUrl #7600

Merged
merged 3 commits into from
Aug 8, 2024
Merged
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
2 changes: 2 additions & 0 deletions changelogs/fragments/7600.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feat:
- Add a util function to generate the relative redirectUrl. ([#7600](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7600))
30 changes: 30 additions & 0 deletions src/core/server/http/http_tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,15 @@ import {
HapiValidationError,
getServerOptions,
getRequestId,
getRedirectUrl,
} from './http_tools';
import { HttpServer } from './http_server';
import { HttpConfig, config } from './http_config';
import { Router } from './router';
import { loggingSystemMock } from '../logging/logging_system.mock';
import { ByteSizeValue } from '@osd/config-schema';
import { httpServerMock } from './http_server.mocks';
import { updateWorkspaceState } from '../utils';

const emptyOutput = {
statusCode: 400,
Expand Down Expand Up @@ -269,3 +272,30 @@ describe('getRequestId', () => {
});
});
});

describe('getRedirectUrl', () => {
it('should prepend with basePath when no requestWorkspaceId is present in request', () => {
const request = httpServerMock.createOpenSearchDashboardsRequest();
expect(
getRedirectUrl({
request,
nextUrl: '/app/next_url',
basePath: '/base',
})
).toEqual('/base/app/next_url');
});

it('should prepend with basePath and workspace prefix when requestWorkspaceId is present in request', () => {
const request = httpServerMock.createOpenSearchDashboardsRequest();
updateWorkspaceState(request, {
requestWorkspaceId: 'workspace_id',
});
expect(
getRedirectUrl({
request,
nextUrl: '/app/next_url',
basePath: '/base',
})
).toEqual('/base/w/workspace_id/app/next_url');
});
});
24 changes: 24 additions & 0 deletions src/core/server/http/http_tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ import { ValidationError } from 'joi';
import uuid from 'uuid';
import { HttpConfig } from './http_config';
import { validateObject } from './prototype_pollution';
import { getWorkspaceState } from '../utils';
import { OpenSearchDashboardsRequest } from './router';
import { WORKSPACE_PATH_PREFIX } from '../../../core/utils';

/**
* Converts OpenSearch Dashboards `HttpConfig` into `ServerOptions` that are accepted by the Hapi server.
Expand Down Expand Up @@ -194,3 +197,24 @@ export function getRequestId(request: Request, options: HttpConfig['requestId'])
? request.headers['x-opaque-id'] ?? uuid.v4()
: uuid.v4();
}

/**
* Return back the expected redirect url with basePath and conditional workspace id based on the request.
* This method should be used when the request is not authenticated and need a redirect url after login.
* And it should take care all the stuff on prepending required params into the path.
* @param request Hapi request object
* @returns the expected next url
*/
export function getRedirectUrl(props: {
request: OpenSearchDashboardsRequest;
nextUrl: string; // The url without basePath and workspace id prefix
basePath: string;
}) {
const { request, nextUrl, basePath } = props;
const workspaceState = getWorkspaceState(request);
if (workspaceState.requestWorkspaceId) {
return `${basePath}${WORKSPACE_PATH_PREFIX}/${workspaceState.requestWorkspaceId}${nextUrl}`;
}

return `${basePath}${nextUrl}`;
}
1 change: 1 addition & 0 deletions src/core/server/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,4 @@ export {
} from './cookie_session_storage';
export * from './types';
export { BasePath, IBasePath } from './base_path_service';
export { getRedirectUrl } from './http_tools';
Loading