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

[Backport 2.x] Fix openid redirect issue to use base_redirect_url when nextUrl is absent #1283

Merged
merged 1 commit into from
Dec 27, 2022
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
37 changes: 36 additions & 1 deletion server/auth/types/openid/helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* permissions and limitations under the License.
*/

import { composeLogoutUrl, getExpirationDate, getRootUrl } from './helper';
import { composeLogoutUrl, getExpirationDate, getRootUrl, getNextUrl } from './helper';

describe('test OIDC helper utility', () => {
test('test compose logout url', () => {
Expand Down Expand Up @@ -146,4 +146,39 @@ describe('test OIDC helper utility', () => {
})
);
});

test('test getNextUrl when request.query.nextUrl is present', () => {
const config = {
openid: {
base_redirect_url: 'http://localhost:5601/ui',
},
};

const core = {};

const request = {
query: {
nextUrl: 'http://localhost:5601/ui/app/home',
},
};

expect('http://localhost:5601/ui/app/home').toEqual(getNextUrl(config, core, request));
});

test('test getNextUrl when request.query.nextUrl is absent', () => {
const config = {
openid: {
base_redirect_url: 'http://localhost:5601/ui',
},
};

const core = {};

const request = {
query: {},
};

// Should go to config.openid?.base_redirect_url
expect('http://localhost:5601/ui').toEqual(getNextUrl(config, core, request));
});
});
8 changes: 8 additions & 0 deletions server/auth/types/openid/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ export function getBaseRedirectUrl(
return rootUrl;
}

export function getNextUrl(
config: SecurityPluginConfigType,
core: CoreSetup,
request: OpenSearchDashboardsRequest
): string {
return request.query.nextUrl || getBaseRedirectUrl(config, core, request) || '/';
}

export async function callTokenEndpoint(
tokenEndpoint: string,
query: any,
Expand Down
11 changes: 8 additions & 3 deletions server/auth/types/openid/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ import { SecuritySessionCookie } from '../../../session/security_cookie';
import { SecurityPluginConfigType } from '../../..';
import { OpenIdAuthConfig } from './openid_auth';
import { SecurityClient } from '../../../backend/opensearch_security_client';
import { getBaseRedirectUrl, callTokenEndpoint, composeLogoutUrl } from './helper';
import {
getBaseRedirectUrl,
callTokenEndpoint,
composeLogoutUrl,
getNextUrl,
getExpirationDate,
} from './helper';
import { validateNextUrl } from '../../../utils/next_url';
import { getExpirationDate } from './helper';
import {
AuthType,
OPENID_AUTH_LOGIN,
Expand Down Expand Up @@ -110,7 +115,7 @@ export class OpenIdAuthRoutes {
const cookie: SecuritySessionCookie = {
oidc: {
state: nonce,
nextUrl: request.query.nextUrl || '/',
nextUrl: getNextUrl(this.config, this.core, request),
},
authType: AuthType.OPEN_ID,
};
Expand Down