Skip to content

Commit

Permalink
[Refactor-1160] Different Values Pointing to Basic Auth, Need to Unify (
Browse files Browse the repository at this point in the history
opensearch-project#1619) (opensearch-project#1649)

---------
Signed-off-by: Prabhas Kurapati <[email protected]>
(cherry picked from commit 2131598df843c3d171250067a8a3c26d2fc4e414)

Co-authored-by: Prabhas Kurapati <[email protected]>
Co-authored-by: Darshit Chanpura <[email protected]>
  • Loading branch information
3 people authored Nov 17, 2023
1 parent 25c41bb commit 5012a8c
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 51 deletions.
20 changes: 7 additions & 13 deletions public/apps/account/test/log-out-button.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,13 @@ import { shallow } from 'enzyme';
import React from 'react';
import { LogoutButton } from '../log-out-button';
import { logout } from '../utils';
import { AuthType } from '../../../../common';

jest.mock('../utils', () => ({
logout: jest.fn(),
}));

describe('Account menu - Log out button', () => {
enum authType {
OpenId = 'openid',
SAML = 'saml',
Proxy = 'proxy',
Basic = 'basicauth',
}

const mockHttpStart = {
basePath: {
serverBasePath: '',
Expand All @@ -39,42 +33,42 @@ describe('Account menu - Log out button', () => {
describe('renders', () => {
it('renders when auth type is MultiAuth: openid', () => {
const component = shallow(
<LogoutButton authType={authType.OpenId} http={mockHttpStart} divider={mockDivider} />
<LogoutButton authType={AuthType.OPEN_ID} http={mockHttpStart} divider={mockDivider} />
);
expect(component).toMatchSnapshot();
});

it('renders when auth type is MultiAuth: saml', () => {
const component = shallow(
<LogoutButton authType={authType.SAML} http={mockHttpStart} divider={mockDivider} />
<LogoutButton authType={AuthType.SAML} http={mockHttpStart} divider={mockDivider} />
);
expect(component).toMatchSnapshot();
});

it('renders when auth type is MultiAuth: basicauth', () => {
const component = shallow(
<LogoutButton authType={authType.Basic} http={mockHttpStart} divider={mockDivider} />
<LogoutButton authType={AuthType.BASIC} http={mockHttpStart} divider={mockDivider} />
);
expect(component).toMatchSnapshot();
});

it('renders when auth type is OpenId', () => {
const component = shallow(
<LogoutButton authType={authType.OpenId} http={mockHttpStart} divider={mockDivider} />
<LogoutButton authType={AuthType.OPEN_ID} http={mockHttpStart} divider={mockDivider} />
);
expect(component).toMatchSnapshot();
});

it('renders when auth type is SAML', () => {
const component = shallow(
<LogoutButton authType={authType.SAML} http={mockHttpStart} divider={mockDivider} />
<LogoutButton authType={AuthType.SAML} http={mockHttpStart} divider={mockDivider} />
);
expect(component).toMatchSnapshot();
});

it('renders when auth type is Proxy', () => {
const component = shallow(
<LogoutButton authType={authType.Proxy} http={mockHttpStart} divider={mockDivider} />
<LogoutButton authType={AuthType.PROXY} http={mockHttpStart} divider={mockDivider} />
);
expect(component).toMatchSnapshot();
});
Expand Down
3 changes: 2 additions & 1 deletion public/apps/customerror/custom-error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import ReactDOM from 'react-dom';
import { Router, Route } from 'react-router-dom';
import { ERROR_MISSING_ROLE_PATH } from '../../../common';
import { ClientConfigType } from '../../types';
import { AuthType } from '../../../common';
import './_index.scss';
import { logout } from '../account/utils';

Expand All @@ -28,7 +29,7 @@ interface CustomErrorDeps {
subtitle: string;
http: CoreStart['http'];
chrome: CoreStart['chrome'];
config: ClientConfigType['ui']['basicauth']['login'];
config: ClientConfigType['ui'][AuthType.BASIC]['login'];
}

export function CustomErrorPage(props: CustomErrorDeps) {
Expand Down
11 changes: 6 additions & 5 deletions public/apps/login/test/login-page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { LoginPage, extractNextUrlFromWindowLocation } from '../login-page';
import { validateCurrentPassword } from '../../../utils/login-utils';
import { API_AUTH_LOGOUT } from '../../../../common';
import { chromeServiceMock } from '../../../../../../src/core/public/mocks';
import { AuthType } from '../../../../common';

jest.mock('../../../utils/login-utils', () => ({
validateCurrentPassword: jest.fn(),
Expand Down Expand Up @@ -101,7 +102,7 @@ describe('Login page', () => {
const config: ClientConfigType = {
ui: configUI,
auth: {
type: ['basicauth'],
type: [AuthType.BASIC],
logout_url: API_AUTH_LOGOUT,
},
};
Expand All @@ -115,7 +116,7 @@ describe('Login page', () => {
const config: ClientConfigType = {
ui: configUI,
auth: {
type: 'basicauth',
type: AuthType.BASIC,
logout_url: API_AUTH_LOGOUT,
},
};
Expand All @@ -129,7 +130,7 @@ describe('Login page', () => {
const config: ClientConfigType = {
ui: configUI,
auth: {
type: ['basicauth', 'openid', 'saml'],
type: [AuthType.BASIC, 'openid', AuthType.SAML],
logout_url: API_AUTH_LOGOUT,
},
};
Expand Down Expand Up @@ -173,7 +174,7 @@ describe('Login page', () => {
const config: ClientConfigType = {
ui: configUiDefault,
auth: {
type: 'basicauth',
type: AuthType.BASIC,
},
};
beforeEach(() => {
Expand Down Expand Up @@ -207,7 +208,7 @@ describe('Login page', () => {
const config: ClientConfigType = {
ui: configUiDefault,
auth: {
type: 'basicauth',
type: AuthType.BASIC,
},
};
beforeEach(() => {
Expand Down
44 changes: 24 additions & 20 deletions server/auth/auth_handler_factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ import {
import { SecurityPluginConfigType } from '..';
import { SecuritySessionCookie } from '../session/security_cookie';
import { getAuthenticationHandler } from './auth_handler_factory';
import { AuthType } from '../../common';

const mockBasicAuthType = AuthType.BASIC;
const mockSAMLAuthType = AuthType.SAML;

jest.mock('./types', () => {
return {
BasicAuthentication: jest.fn().mockImplementation(() => {
return {
authHandler: () => {},
type: 'basicauth',
type: mockBasicAuthType,
init: () => {},
};
}),
Expand Down Expand Up @@ -57,14 +61,14 @@ jest.mock('./types', () => {
SamlAuthentication: jest.fn().mockImplementation(() => {
return {
authHandler: () => {},
type: 'saml',
type: mockSAMLAuthType,
init: () => {},
};
}),
MultipleAuthentication: jest.fn().mockImplementation(() => {
return {
authHandler: () => {},
type: ['openid', 'saml', 'basiauth'],
type: ['openid', mockSAMLAuthType, mockBasicAuthType],
init: () => {},
};
}),
Expand All @@ -83,54 +87,54 @@ describe('test authentication factory', () => {

test('get basic auth: string array', async () => {
const auth = await getAuthenticationHandler(
['basicauth'],
[AuthType.BASIC],
router,
config,
core,
esClient,
sessionStorageFactory,
logger
);
expect(auth.type).toEqual('basicauth');
expect(auth.type).toEqual(AuthType.BASIC);
});

test('get basic auth: string', async () => {
const auth = await getAuthenticationHandler(
'basicauth',
AuthType.BASIC,
router,
config,
core,
esClient,
sessionStorageFactory,
logger
);
expect(auth.type).toEqual('basicauth');
expect(auth.type).toEqual(AuthType.BASIC);
});

test('get basic auth with empty auth type: string array', async () => {
test('get basic auth with empty auth type: string', async () => {
const auth = await getAuthenticationHandler(
[''],
'',
router,
config,
core,
esClient,
sessionStorageFactory,
logger
);
expect(auth.type).toEqual('basicauth');
expect(auth.type).toEqual(AuthType.BASIC);
});

test('get basic auth with empty auth type: string', async () => {
test('get basic auth with empty auth type: string array', async () => {
const auth = await getAuthenticationHandler(
'',
[''],
router,
config,
core,
esClient,
sessionStorageFactory,
logger
);
expect(auth.type).toEqual('basicauth');
expect(auth.type).toEqual(AuthType.BASIC);
});

test('get jwt auth: string array', async () => {
Expand Down Expand Up @@ -213,28 +217,28 @@ describe('test authentication factory', () => {

test('get saml auth: string array', async () => {
const auth = await getAuthenticationHandler(
['saml'],
[AuthType.SAML],
router,
config,
core,
esClient,
sessionStorageFactory,
logger
);
expect(auth.type).toEqual('saml');
expect(auth.type).toEqual(AuthType.SAML);
});

test('get saml auth: string', async () => {
const auth = await getAuthenticationHandler(
'saml',
AuthType.SAML,
router,
config,
core,
esClient,
sessionStorageFactory,
logger
);
expect(auth.type).toEqual('saml');
expect(auth.type).toEqual(AuthType.SAML);
});

test('multiple_auth_enabled is on, get multi auth', async () => {
Expand All @@ -244,15 +248,15 @@ describe('test authentication factory', () => {
},
};
const auth = await getAuthenticationHandler(
['openid', 'saml', 'basiauth'],
['openid', AuthType.SAML, AuthType.BASIC],
router,
config,
core,
esClient,
sessionStorageFactory,
logger
);
expect(auth.type).toEqual(['openid', 'saml', 'basiauth']);
expect(auth.type).toEqual(['openid', AuthType.SAML, AuthType.BASIC]);
});

test('multiple_auth_enabled is off, get multi auth', async () => {
Expand All @@ -263,7 +267,7 @@ describe('test authentication factory', () => {
};
try {
await getAuthenticationHandler(
['openid', 'saml', 'basiauth'],
['openid', AuthType.SAML, AuthType.BASIC],
router,
config,
core,
Expand Down
5 changes: 3 additions & 2 deletions server/auth/types/basic/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from '../../../../common';
import { resolveTenant } from '../../../multitenancy/tenant_resolver';
import { encodeUriQuery } from '../../../../../../src/plugins/opensearch_dashboards_utils/common/url/encode_uri_query';
import { AuthType } from '../../../../common';

export class BasicAuthRoutes {
constructor(
Expand Down Expand Up @@ -112,7 +113,7 @@ export class BasicAuthRoutes {
credentials: {
authHeaderValue: `Basic ${encodedCredentials}`,
},
authType: 'basicauth',
authType: AuthType.BASIC,
isAnonymousAuth: false,
expiryTime: Date.now() + this.config.session.ttl,
};
Expand Down Expand Up @@ -202,7 +203,7 @@ export class BasicAuthRoutes {
this.sessionStorageFactory.asScoped(request).clear();
const sessionStorage: SecuritySessionCookie = {
username: user.username,
authType: 'basicauth',
authType: AuthType.BASIC,
isAnonymousAuth: true,
expiryTime: Date.now() + this.config.session.ttl,
};
Expand Down
2 changes: 1 addition & 1 deletion server/auth/types/saml/saml_auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
export class SamlAuthentication extends AuthenticationType {
public static readonly AUTH_HEADER_NAME = 'authorization';

public readonly type: string = 'saml';
public readonly type: string = AuthType.SAML;

constructor(
config: SecurityPluginConfigType,
Expand Down
7 changes: 4 additions & 3 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
import { schema, TypeOf } from '@osd/config-schema';
import { PluginInitializerContext, PluginConfigDescriptor } from '../../../src/core/server';
import { SecurityPlugin } from './plugin';
import { AuthType } from '../common';

const validateAuthType = (value: string[]) => {
const supportedAuthTypes = [
'',
'basicauth',
AuthType.BASIC,
'jwt',
'openid',
'saml',
AuthType.SAML,
'proxy',
'kerberos',
'proxycache',
Expand Down Expand Up @@ -88,7 +89,7 @@ export const configSchema = schema.object({

if (value.length > 1) {
const includeBasicAuth = value.find((element) => {
return element.toLowerCase() === 'basicauth';
return element.toLowerCase() === AuthType.BASIC;
});

if (!includeBasicAuth) {
Expand Down
4 changes: 2 additions & 2 deletions server/routes/auth_type_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import { IRouter } from 'opensearch-dashboards/server';
import { SecurityPluginConfigType } from '..';

import { AuthType } from '../../common';
export function defineAuthTypeRoutes(router: IRouter, config: SecurityPluginConfigType) {
/**
* Auth type API that returns current auth type configured on OpenSearchDashboards Server.
Expand All @@ -30,7 +30,7 @@ export function defineAuthTypeRoutes(router: IRouter, config: SecurityPluginConf
router.get(
{ path: '/api/authtype', validate: false, options: { authRequired: false } },
async (context, request, response) => {
const authType = config.auth.type || 'basicauth';
const authType = config.auth.type || AuthType.BASIC;
return response.ok({
body: {
authtype: authType,
Expand Down
Loading

0 comments on commit 5012a8c

Please sign in to comment.