Skip to content

Commit

Permalink
fix: build
Browse files Browse the repository at this point in the history
  • Loading branch information
listlessbird committed Dec 10, 2024
1 parent 7277676 commit 26d345c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
4 changes: 2 additions & 2 deletions web/src/app/api/auth/yt/callback/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
21 changes: 12 additions & 9 deletions web/src/lib/yt/encrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
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"),
Expand All @@ -31,12 +33,13 @@ export async function encrypt(text: string): Promise<string> {
}

export async function decrypt(encryptedText: string): Promise<string> {
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");
Expand Down

0 comments on commit 26d345c

Please sign in to comment.