Skip to content

Commit

Permalink
Add a util function to generate the relative redirectUrl (#7600) (#7669)
Browse files Browse the repository at this point in the history
* feat: add get redirect url function

---------



(cherry picked from commit e947ade)

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 12, 2024
1 parent 64edc4f commit e291f87
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
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';

0 comments on commit e291f87

Please sign in to comment.