Skip to content

Commit

Permalink
chore: moves error messages to constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajeyakrishna-k committed Dec 24, 2023
1 parent 930a161 commit 33f6d61
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/constants/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <token>'";

export const AUTHENTICATION_ERROR = "Invalid Authentication token";
10 changes: 6 additions & 4 deletions src/utils/verifyAuthToken.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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 <token>'"
);
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);
}
}
10 changes: 7 additions & 3 deletions tests/unit/utils/verifyToken.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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 <token>'"
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 <token>'"
INVALID_TOKEN_FORMAT
);
});
});

0 comments on commit 33f6d61

Please sign in to comment.