From 2a1289da186ba85eecbda07f2aaaaf663ed4cada Mon Sep 17 00:00:00 2001 From: Darshit Chanpura Date: Tue, 9 Apr 2024 14:26:09 -0400 Subject: [PATCH] Uses enum instead of magic constant Signed-off-by: Darshit Chanpura --- server/auth/types/authentication_type.ts | 6 +++--- server/auth/types/basic/routes.ts | 6 +++++- server/readonly/readonly_service.ts | 7 ++++++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/server/auth/types/authentication_type.ts b/server/auth/types/authentication_type.ts index f504656d0..4bc013b21 100755 --- a/server/auth/types/authentication_type.ts +++ b/server/auth/types/authentication_type.ts @@ -31,7 +31,7 @@ import { SecuritySessionCookie } from '../../session/security_cookie'; import { SecurityClient } from '../../backend/opensearch_security_client'; import { resolveTenant, isValidTenant } from '../../multitenancy/tenant_resolver'; import { UnauthenticatedError } from '../../errors'; -import { GLOBAL_TENANT_SYMBOL } from '../../../common'; +import { AuthType, GLOBAL_TENANT_SYMBOL } from '../../../common'; export interface IAuthenticationType { type: string; @@ -210,7 +210,7 @@ export abstract class AuthenticationType implements IAuthenticationType { } } if (!authInfo) { - const authRequestType = cookie.isAnonymousAuth ? 'anonymous' : cookie.authType; + const authRequestType = cookie.isAnonymousAuth ? AuthType.ANONYMOUS : cookie.authType; authInfo = await this.securityClient.authinfo(request, authRequestType, authHeaders); } authState.authInfo = authInfo; @@ -244,7 +244,7 @@ export abstract class AuthenticationType implements IAuthenticationType { authHeader: any, authInfo: any ): Promise { - const authType = cookie.isAnonymousAuth ? 'anonymous' : cookie.authType; + const authType = cookie.isAnonymousAuth ? AuthType.ANONYMOUS : cookie.authType; if (!authInfo) { try { authInfo = await this.securityClient.authinfo(request, authType, authHeader); diff --git a/server/auth/types/basic/routes.ts b/server/auth/types/basic/routes.ts index 3f622b4c2..23e3d71c6 100755 --- a/server/auth/types/basic/routes.ts +++ b/server/auth/types/basic/routes.ts @@ -186,7 +186,11 @@ export class BasicAuthRoutes { } context.security_plugin.logger.info('The Redirect Path is ' + redirectUrl); try { - user = await this.securityClient.authenticateWithHeaders(request, 'anonymous', {}); + user = await this.securityClient.authenticateWithHeaders( + request, + AuthType.ANONYMOUS, + {} + ); } catch (error) { context.security_plugin.logger.error( `Failed authentication: ${error}. Redirecting to Login Page` diff --git a/server/readonly/readonly_service.ts b/server/readonly/readonly_service.ts index 622d8ab8d..901594812 100644 --- a/server/readonly/readonly_service.ts +++ b/server/readonly/readonly_service.ts @@ -24,6 +24,7 @@ import { isPrivateTenant, LOGIN_PAGE_URI, CUSTOM_ERROR_PAGE_URI, + AuthType, } from '../../common'; import { SecurityClient } from '../backend/opensearch_security_client'; import { IAuthenticationType, OpenSearchAuthInfo } from '../auth/types/authentication_type'; @@ -104,7 +105,11 @@ export class ReadonlyService extends BaseReadonlyService { return false; } - const authType = cookie ? (cookie.isAnonymousAuth ? 'anonymous' : cookie.authType) : ''; + const authType = cookie + ? cookie.isAnonymousAuth + ? AuthType.ANONYMOUS + : cookie.authType + : ''; const authInfo = await this.securityClient.authinfo(request, authType, headers); if (!authInfo.user_requested_tenant && cookie) {