From 12fe6af995a6dfdf3d550a495560a6965c9e4b41 Mon Sep 17 00:00:00 2001 From: Agusti Fernandez Pardo Date: Thu, 4 Aug 2022 19:54:25 +0200 Subject: [PATCH] feat: adds license check --- .env.example | 5 ++++- lib/helpers/verifyApiKey.ts | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/.env.example b/.env.example index eec3f283f7..c5354bfd7d 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,6 @@ API_KEY_PREFIX=cal_ DATABASE_URL="postgresql://postgres:@localhost:5450/calendso" -NEXT_PUBLIC_WEBAPP_URL=http://localhost:3000 \ No newline at end of file +NEXT_PUBLIC_WEBAPP_URL=http://localhost:3000 + +# Get it in console.cal.com +CALCOM_LICENSE_KEY="" diff --git a/lib/helpers/verifyApiKey.ts b/lib/helpers/verifyApiKey.ts index b915df6626..408cc55356 100644 --- a/lib/helpers/verifyApiKey.ts +++ b/lib/helpers/verifyApiKey.ts @@ -1,6 +1,7 @@ import { NextMiddleware } from "next-api-middleware"; import { hashAPIKey } from "@calcom/features/ee/api-keys/lib/apiKeys"; +import checkLicense from "@calcom/features/ee/common/server/checkLicense"; import { isAdminGuard } from "@lib/utils/isAdmin"; @@ -14,6 +15,9 @@ export const dateNotInPast = function (date: Date) { // This verifies the apiKey and sets the user if it is valid. export const verifyApiKey: NextMiddleware = async (req, res, next) => { + const hasValidLicense = await checkLicense(process.env.CALCOM_LICENSE_KEY || ""); + if (!hasValidLicense) + return res.status(401).json({ error: "Invalid or missing CALCOM_LICENSE_KEY environment variable" }); const { prisma, userId, isAdmin } = req; // If the user is an admin and using a license key (from customPrisma), skip the apiKey check. if (userId === 0 && isAdmin) {