Skip to content

Commit

Permalink
Adds integration tests for anonymous auth login with basic authorizat…
Browse files Browse the repository at this point in the history
…ion header

Signed-off-by: Darshit Chanpura <dchanp@amazon.com>
  • Loading branch information
DarshitChanpura committed Mar 12, 2024
1 parent 63abb41 commit 5492d2a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
4 changes: 2 additions & 2 deletions server/auth/types/basic/basic_auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { SecurityPluginConfigType } from '../../..';
import { SecuritySessionCookie } from '../../../session/security_cookie';
import { BasicAuthRoutes } from './routes';
import { AuthenticationType } from '../authentication_type';
import { LOGIN_PAGE_URI, ANONYMOUS_AUTH_LOGIN } from '../../../../common';
import { LOGIN_PAGE_URI, ANONYMOUS_AUTH_HEADER } from '../../../../common';
import { composeNextUrlQueryParam } from '../../../utils/next_url';
import { AUTH_HEADER_NAME, AuthType, OPENDISTRO_SECURITY_ANONYMOUS } from '../../../../common';

Expand Down Expand Up @@ -130,7 +130,7 @@ export class BasicAuthentication extends AuthenticationType {
request: OpenSearchDashboardsRequest
): any {
if (this.config.auth.anonymous_auth_enabled && cookie.isAnonymousAuth) {
return {};
return { authorization: ANONYMOUS_AUTH_HEADER };
}
const headers: any = {};
Object.assign(headers, { authorization: cookie.credentials?.authHeaderValue });
Expand Down
1 change: 1 addition & 0 deletions server/readonly/readonly_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
isPrivateTenant,
LOGIN_PAGE_URI,
CUSTOM_ERROR_PAGE_URI,
ANONYMOUS_AUTH_HEADER,
} from '../../common';
import { SecurityClient } from '../backend/opensearch_security_client';
import { IAuthenticationType, OpenSearchAuthInfo } from '../auth/types/authentication_type';
Expand Down
23 changes: 22 additions & 1 deletion test/jest_integration/basic_auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ describe('start OpenSearch Dashboards server', () => {
expect(response.status).toEqual(200);
});

it('cannot access home page as anonymous user if no credentials are supplied', async () => {
const response = await osdTestServer.request
.get(root, '/app/home#/')
.unset(AUTHORIZATION_HEADER_NAME);
expect(response.status).toEqual(302);
});

it('can access home page as anonymous user', async () => {
const response = await osdTestServer.request
.get(root, '/app/home#/')
.set(AUTHORIZATION_HEADER_NAME, ANONYMOUS_AUTH_HEADER);
expect(response.status).toEqual(200);
});

it('call authinfo API as admin', async () => {
const testUserCredentials = Buffer.from(ADMIN_CREDENTIALS);
const response = await osdTestServer.request
Expand All @@ -143,10 +157,17 @@ describe('start OpenSearch Dashboards server', () => {
it('call authinfo API without credentials', async () => {
const response = await osdTestServer.request
.get(root, '/api/v1/auth/authinfo')
.unset('Authorization');
.unset(AUTHORIZATION_HEADER_NAME);
expect(response.status).toEqual(401);
});

it('call authinfo API as anonymous user', async () => {
const response = await osdTestServer.request
.get(root, '/api/v1/auth/authinfo')
.set(AUTHORIZATION_HEADER_NAME, ANONYMOUS_AUTH_HEADER);
expect(response.status).toEqual(200);
});

it('call authinfo API with cookie', async () => {
const authCookie = await getAuthCookie(root, ADMIN_USER, ADMIN_PASSWORD);

Expand Down

0 comments on commit 5492d2a

Please sign in to comment.