From 5bb2c54648860bd8864ae1398e4f51e9c19c6ffc Mon Sep 17 00:00:00 2001 From: Agusti Fernandez Pardo Date: Wed, 18 May 2022 20:29:35 +0200 Subject: [PATCH] fix: add fallback for keys generated with cal_ instead of cal_live_ prefix --- lib/helpers/verifyApiKey.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/helpers/verifyApiKey.ts b/lib/helpers/verifyApiKey.ts index cf5c4a8472..6026106231 100644 --- a/lib/helpers/verifyApiKey.ts +++ b/lib/helpers/verifyApiKey.ts @@ -23,7 +23,10 @@ export const dateNotInPast = function (date: Date) { export const verifyApiKey: NextMiddleware = async (req, res, next) => { if (!req.query.apiKey) return res.status(401).json({ message: "No apiKey provided" }); // We remove the prefix from the user provided api_key. If no env set default to "cal_" - const strippedApiKey = `${req.query.apiKey}`.replace(process.env.API_KEY_PREFIX || "cal_", ""); + let strippedApiKey = `${req.query.apiKey}`.replace(process.env.API_KEY_PREFIX, ""); + strippedApiKey = strippedApiKey.includes("cal_") + ? strippedApiKey.replace("cal_", "") + : strippedApiKey; // Hash the key again before matching against the database records. const hashedKey = hashAPIKey(strippedApiKey); // Check if the hashed api key exists in database.