Skip to content

Commit e760893

Browse files
Merge pull request #667 from zengyan-amazon/logout-query
Allow adding query parameters to OIDC logout url
2 parents f2e344e + 0d1b708 commit e760893

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
import { composeLogoutUrl } from './helper';
17+
18+
describe('test OIDC helper utility', () => {
19+
test('test compose logout url', () => {
20+
const idpEndSessionUrl = 'https://idp.com/path';
21+
const customLogoutUrl = 'https://customurl.com/path';
22+
const additionalQuery = { key: 'value' };
23+
24+
expect('https://customurl.com/path?key=value').toEqual(
25+
composeLogoutUrl(customLogoutUrl, idpEndSessionUrl, additionalQuery)
26+
);
27+
});
28+
29+
test('test compose logout url when no custom logout url', () => {
30+
const idpEndSessionUrl = 'https://idp.com/path';
31+
const customLogoutUrl = '';
32+
const additionalQuery = { key: 'value' };
33+
34+
expect('https://idp.com/path?key=value').toEqual(
35+
composeLogoutUrl(customLogoutUrl, idpEndSessionUrl, additionalQuery)
36+
);
37+
});
38+
39+
test('test compse logout url when custom url has query parameter', () => {
40+
const idpEndSessionUrl = 'https://idp.com/path';
41+
const customLogoutUrl = 'https://customurl.com/path?a=b';
42+
const additionalQuery = { key: 'value' };
43+
44+
expect('https://customurl.com/path?a=b&key=value').toEqual(
45+
composeLogoutUrl(customLogoutUrl, idpEndSessionUrl, additionalQuery)
46+
);
47+
});
48+
49+
test('test compse logout url when idp end session url has query parameter', () => {
50+
const idpEndSessionUrl = 'https://idp.com/path?a=b';
51+
const customLogoutUrl = '';
52+
const additionalQuery = { key: 'value' };
53+
54+
expect('https://idp.com/path?a=b&key=value').toEqual(
55+
composeLogoutUrl(customLogoutUrl, idpEndSessionUrl, additionalQuery)
56+
);
57+
});
58+
});

server/auth/types/openid/helper.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ export async function callTokenEndpoint(tokenEndpoint: string, query: any): Prom
7070
};
7171
}
7272

73+
export function composeLogoutUrl(
74+
customLogoutUrl: string | undefined,
75+
idpEndsessionEndpoint: string | undefined,
76+
additionalQueryParams: any
77+
) {
78+
const logoutEndpont = customLogoutUrl || idpEndsessionEndpoint;
79+
const logoutUrl = new URL(logoutEndpont!);
80+
Object.keys(additionalQueryParams).forEach((key) => {
81+
logoutUrl.searchParams.append(key, additionalQueryParams[key] as string);
82+
});
83+
return logoutUrl.toString();
84+
}
85+
7386
export interface TokenResponse {
7487
idToken?: string;
7588
accessToken?: string;

server/auth/types/openid/routes.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { SecuritySessionCookie } from '../../../session/security_cookie';
2626
import { SecurityPluginConfigType } from '../../..';
2727
import { OpenIdAuthConfig } from './openid_auth';
2828
import { SecurityClient } from '../../../backend/opendistro_security_client';
29-
import { getBaseRedirectUrl, callTokenEndpoint } from './helper';
29+
import { getBaseRedirectUrl, callTokenEndpoint, composeLogoutUrl } from './helper';
3030
import { validateNextUrl } from '../../../utils/next_url';
3131

3232
export class OpenIdAuthRoutes {
@@ -190,9 +190,11 @@ export class OpenIdAuthRoutes {
190190
id_token_hint: token,
191191
};
192192

193-
const logoutBaseUri =
194-
this.config.openid?.logout_url || this.openIdAuthConfig.endSessionEndpoint;
195-
const endSessionUrl = `${logoutBaseUri}?${stringify(logoutQueryParams)}`;
193+
const endSessionUrl = composeLogoutUrl(
194+
this.config.openid?.logout_url,
195+
this.openIdAuthConfig.endSessionEndpoint,
196+
logoutQueryParams
197+
);
196198

197199
return response.redirected({
198200
headers: {

0 commit comments

Comments
 (0)