From 26d345cd1146835af002048381a058694df6af30 Mon Sep 17 00:00:00 2001 From: listlessbird <124798751+listlessbird@users.noreply.github.com> Date: Tue, 10 Dec 2024 17:37:49 +0530 Subject: [PATCH] fix: build --- web/src/app/api/auth/yt/callback/route.ts | 4 ++-- web/src/lib/yt/encrypt.ts | 21 ++++++++++++--------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/web/src/app/api/auth/yt/callback/route.ts b/web/src/app/api/auth/yt/callback/route.ts index 74d46a2..6649f66 100644 --- a/web/src/app/api/auth/yt/callback/route.ts +++ b/web/src/app/api/auth/yt/callback/route.ts @@ -4,9 +4,9 @@ import { getCurrentSession } from "@/lib/auth"; import { validateYtStateToken } from "@/lib/yt/csrf"; import { encrypt } from "@/lib/yt/encrypt"; import { YoutubeService } from "@/lib/yt/yt.service"; -import { NextRequest, NextResponse } from "next/server"; +import { NextRequest } from "next/server"; -export async function GET(req: NextRequest, res: NextResponse) { +export async function GET(req: NextRequest) { const code = req.nextUrl.searchParams.get("code"); const state = req.nextUrl.searchParams.get("state"); diff --git a/web/src/lib/yt/encrypt.ts b/web/src/lib/yt/encrypt.ts index 73059e3..71764ce 100644 --- a/web/src/lib/yt/encrypt.ts +++ b/web/src/lib/yt/encrypt.ts @@ -3,22 +3,24 @@ import { randomBytes, createCipheriv, createDecipheriv } from "crypto"; const ENCRYPTION_KEY = process.env.ENCRYPTION_KEY; const ALGORITHM = "aes-256-gcm"; -if (!ENCRYPTION_KEY || ENCRYPTION_KEY.length !== 32) { - if (ENCRYPTION_KEY) { - console.error( - `Invalid encryption key, length is ${ENCRYPTION_KEY.length} it must be 32 characters long +function isValidKey() { + if (!ENCRYPTION_KEY || ENCRYPTION_KEY.length !== 32) { + if (ENCRYPTION_KEY) { + console.error( + `Invalid encryption key, length is ${ENCRYPTION_KEY.length} it must be 32 characters long Run the following command to generate a new encryption key: openssl rand -base64 32 | cut -c1-32 ` - ); + ); + } + throw new Error("Invalid encryption key"); } - throw new Error("Invalid encryption key"); } - export async function encrypt(text: string): Promise { + isValidKey(); const iv = randomBytes(12); - const cipher = createCipheriv(ALGORITHM, ENCRYPTION_KEY, iv); + const cipher = createCipheriv(ALGORITHM, ENCRYPTION_KEY as string, iv); const encrypted = Buffer.concat([ // @ts-ignore cipher.update(text, "utf8"), @@ -31,12 +33,13 @@ export async function encrypt(text: string): Promise { } export async function decrypt(encryptedText: string): Promise { + isValidKey(); const buffer = Buffer.from(encryptedText, "base64"); const iv = buffer.subarray(0, 12); const authTag = buffer.subarray(12, 28); const encrypted = buffer.subarray(28); - const decipher = createDecipheriv(ALGORITHM, ENCRYPTION_KEY, iv); + const decipher = createDecipheriv(ALGORITHM, ENCRYPTION_KEY as string, iv); decipher.setAuthTag(authTag); return decipher.update(encrypted) + decipher.final("utf8");