From acf85ea9513a4c089d095652727d3df6d545142b Mon Sep 17 00:00:00 2001 From: mbaharip <62494292+mbahArip@users.noreply.github.com> Date: Thu, 25 May 2023 20:36:22 +0700 Subject: [PATCH] Add validation for root folder --- src/app/(api)/api/validate/route.ts | 90 +++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/app/(api)/api/validate/route.ts diff --git a/src/app/(api)/api/validate/route.ts b/src/app/(api)/api/validate/route.ts new file mode 100644 index 0000000..08d0660 --- /dev/null +++ b/src/app/(api)/api/validate/route.ts @@ -0,0 +1,90 @@ +import { cookies } from "next/headers"; +import { NextRequest, NextResponse } from "next/server"; + +import createErrorPayload from "utils/apiHelper/createErrorPayload"; +import passwordHash from "utils/encryptionHelper/passwordHash"; +import ExtendedError from "utils/generalHelper/extendedError"; + +import { API_Response } from "types/api"; +import { ValidateFilePathResponse } from "types/api/path"; +import { Constant } from "types/general/constant"; + +import apiConfig from "config/api.config"; +import siteConfig from "config/site.config"; + +export async function GET(request: NextRequest) { + const _start = Date.now(); + + try { + const masterKey = request.headers.get("x-gdrive-key"); + const validMasterKey = passwordHash.verify( + masterKey || "", + apiConfig.masterKey, + ); + + if (siteConfig.privateIndex && !validMasterKey) { + const userPassword = cookies().get( + `next-gdrive-password`, + )?.value; + if (!userPassword) { + throw new ExtendedError( + Constant.apiNotAuthorized, + 401, + "unauthorized", + `You need to provide password to access "root"`, + ); + } + + const parsedUserPassword = JSON.parse(userPassword); + const nearestFolderPassword = + parsedUserPassword["root"]; + if (!nearestFolderPassword) { + throw new ExtendedError( + Constant.apiNotAuthorized, + 401, + "unauthorized", + `You need to provide password to access "root"`, + ); + } + + if ( + !passwordHash.compare( + siteConfig.indexPassword, + nearestFolderPassword, + ) + ) { + throw new ExtendedError( + Constant.apiNotAuthorized, + 401, + "unauthorized", + `The password you provided is incorrect`, + ); + } + } + + const payload: API_Response = + { + success: true, + timestamp: new Date().toISOString(), + responseTime: Date.now() - _start, + data: [], + }; + + return NextResponse.json(payload, { + status: 200, + headers: { + "Cache-Control": apiConfig.cacheControl, + }, + }); + } catch (error: any) { + const payload = createErrorPayload( + error, + "GET /api/validate", + _start, + ); + + return new Response(JSON.stringify(payload), { + status: payload.code, + }); + } +}