From 33f6d610f0403e657ef1d50091f7655aaa945d97 Mon Sep 17 00:00:00 2001 From: Ajeyakrishna Date: Mon, 25 Dec 2023 00:20:25 +0530 Subject: [PATCH] chore: moves error messages to constants --- src/constants/responses.ts | 5 +++++ src/utils/verifyAuthToken.ts | 10 ++++++---- tests/unit/utils/verifyToken.test.ts | 10 +++++++--- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/constants/responses.ts b/src/constants/responses.ts index a7a56285..44007852 100644 --- a/src/constants/responses.ts +++ b/src/constants/responses.ts @@ -72,3 +72,8 @@ export const OVERDUE_CUSTOM_MESSAGE = export const ONBOARDING_DEFAULT_MESSAGE = `You currently have an onboarding status. Please provide an update explaining any challenges you're facing in completing your tasks. If you're finished, consider assigning new tasks to Admin.`; export const ONBOARDING_CUSTOM_MESSAGE = `Please update your status explaining why you are unable to complete your onboarding tasks within {{days}} days.`; + +export const INVALID_TOKEN_FORMAT = + "Invalid Authentication header format. Expected 'Bearer '"; + +export const AUTHENTICATION_ERROR = "Invalid Authentication token"; diff --git a/src/utils/verifyAuthToken.ts b/src/utils/verifyAuthToken.ts index 4de51044..52c64f01 100644 --- a/src/utils/verifyAuthToken.ts +++ b/src/utils/verifyAuthToken.ts @@ -1,3 +1,7 @@ +import { + AUTHENTICATION_ERROR, + INVALID_TOKEN_FORMAT, +} from "../constants/responses"; import { env } from "../typeDefinitions/default.types"; import jwt from "@tsndr/cloudflare-worker-jwt"; @@ -10,15 +14,13 @@ import jwt from "@tsndr/cloudflare-worker-jwt"; export async function verifyAuthToken(authHeader: string, env: env) { const parts = authHeader.split(" "); if (parts.length !== 2 || parts[0] !== "Bearer") { - throw new Error( - "Invalid Authentication header format. Expected 'Bearer '" - ); + throw new Error(INVALID_TOKEN_FORMAT); } const authToken = parts[1]; const isValid = await jwt.verify(authToken, env.RDS_SERVERLESS_PUBLIC_KEY, { algorithm: "RS256", }); if (!isValid) { - throw new Error("Invalid Authentication token"); + throw new Error(AUTHENTICATION_ERROR); } } diff --git a/tests/unit/utils/verifyToken.test.ts b/tests/unit/utils/verifyToken.test.ts index 00bd37bf..2c3c63f5 100644 --- a/tests/unit/utils/verifyToken.test.ts +++ b/tests/unit/utils/verifyToken.test.ts @@ -1,5 +1,9 @@ import jwt from "@tsndr/cloudflare-worker-jwt"; import { verifyAuthToken } from "../../../src/utils/verifyAuthToken"; +import { + AUTHENTICATION_ERROR, + INVALID_TOKEN_FORMAT, +} from "../../../src/constants/responses"; describe("verifyAuthToken", () => { const authToken = "validToken"; @@ -24,20 +28,20 @@ describe("verifyAuthToken", () => { const authHeader = "Bearer invalidToken"; jwt.verify = jest.fn().mockResolvedValue(false); await expect(verifyAuthToken(authHeader, mockEnv)).rejects.toThrow( - "Invalid Authentication token" + AUTHENTICATION_ERROR ); }); it("should throw an error when Bearer is not passed", async () => { const authHeader = "Beaer invalidToken"; await expect(verifyAuthToken(authHeader, mockEnv)).rejects.toThrow( - "Invalid Authentication header format. Expected 'Bearer '" + INVALID_TOKEN_FORMAT ); }); it("should throw an error for a malformed auth header", async () => { const malformedHeader = "invalidformat"; await expect(verifyAuthToken(malformedHeader, mockEnv)).rejects.toThrow( - "Invalid Authentication header format. Expected 'Bearer '" + INVALID_TOKEN_FORMAT ); }); });