Skip to content
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
74 changes: 74 additions & 0 deletions server/auth/types/jwt/jwt_auth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright OpenSearch Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

import { getAuthenticationHandler } from '../../auth_handler_factory';

describe('test jwt auth library', () => {
const router: IRouter = { post: (body) => {} };
let core: CoreSetup;
let esClient: ILegacyClusterClient;
let sessionStorageFactory: SessionStorageFactory<SecuritySessionCookie>;
let logger: Logger;

function getTestJWTAuthenticationHandlerWithConfig(config: SecurityPluginConfigType) {
return getAuthenticationHandler(
'jwt',
router,
config,
core,
esClient,
sessionStorageFactory,
logger
);
}

test('test getTokenFromUrlParam', () => {
const config = {
jwt: {
header: 'Authorization',
url_param: 'authorization',
},
};
const auth = getTestJWTAuthenticationHandlerWithConfig(config);

const url = new URL('http://localhost:5601/app/api/v1/auth/authinfo?authorization=testtoken');
const request = {
url,
};

const expectedToken = 'testtoken';
const token = auth.getTokenFromUrlParam(request);
expect(token).toEqual(expectedToken);
});

test('test getTokenFromUrlParam incorrect url_param', () => {
const config = {
jwt: {
header: 'Authorization',
url_param: 'urlParamName',
},
};
const auth = getTestJWTAuthenticationHandlerWithConfig(config);

const url = new URL('http://localhost:5601/app/api/v1/auth/authinfo?authorization=testtoken');
const request = {
url,
};

const expectedToken = undefined;
const token = auth.getTokenFromUrlParam(request);
expect(token).toEqual(expectedToken);
});
});
5 changes: 2 additions & 3 deletions server/auth/types/jwt/jwt_auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class JwtAuthentication extends AuthenticationType {
private getTokenFromUrlParam(request: OpenSearchDashboardsRequest): string | undefined {
const urlParamName = this.config.jwt?.url_param;
if (urlParamName) {
const token = request.url.searchParams.get('urlParamName');
const token = request.url.searchParams.get(urlParamName);
return (token as string) || undefined;
}
return undefined;
Expand All @@ -79,9 +79,8 @@ export class JwtAuthentication extends AuthenticationType {
if (request.headers[this.authHeaderName]) {
return true;
}

const urlParamName = this.config.jwt?.url_param;
if (urlParamName && request.url.searchParams.get('urlParamName')) {
if (urlParamName && request.url.searchParams.get(urlParamName)) {
return true;
}

Expand Down