From 1bcfd6636f773025fca3e08fe2f6c7c8d330cabb Mon Sep 17 00:00:00 2001 From: Harman-singh-waraich Date: Tue, 28 May 2024 21:20:14 +0530 Subject: [PATCH 1/2] feat(web): add-authentication-to-file-uploads --- web/netlify/functions/uploadToIPFS.ts | 21 +++---- .../Evidence/SubmitEvidenceModal.tsx | 61 ++++++++++--------- web/src/pages/Resolver/index.tsx | 33 +++++----- web/src/utils/uploadFormDataToIPFS.ts | 5 ++ 4 files changed, 64 insertions(+), 56 deletions(-) diff --git a/web/netlify/functions/uploadToIPFS.ts b/web/netlify/functions/uploadToIPFS.ts index bd101e363..e59ccfcb7 100644 --- a/web/netlify/functions/uploadToIPFS.ts +++ b/web/netlify/functions/uploadToIPFS.ts @@ -1,9 +1,11 @@ import { File, FilebaseClient } from "@filebase/client"; -import { Handler } from "@netlify/functions"; +import middy from "@middy/core"; import amqp, { Connection } from "amqplib"; import busboy from "busboy"; -const { FILEBASE_TOKEN, RABBITMQ_URL, FILEBASE_API_WRAPPER } = process.env; +import { authMiddleware } from "../middleware/authMiddleware"; + +const { FILEBASE_TOKEN, RABBITMQ_URL } = process.env; const filebase = new FilebaseClient({ token: FILEBASE_TOKEN ?? "" }); type FormElement = @@ -65,24 +67,17 @@ const pinToFilebase = async (data: FormData, operation: string): Promise { +export const uploadToIPFS = async (event) => { const { queryStringParameters } = event; - if (!queryStringParameters || !queryStringParameters.key || !queryStringParameters.operation) { + if (!queryStringParameters?.operation) { return { statusCode: 400, body: JSON.stringify({ message: "Invalid query parameters" }), }; } - const { key, operation } = queryStringParameters; - - if (key !== FILEBASE_API_WRAPPER) { - return { - statusCode: 403, - body: JSON.stringify({ message: "Invalid API key" }), - }; - } + const { operation } = queryStringParameters; try { const parsed = await parseMultipart(event); @@ -102,3 +97,5 @@ export const handler: Handler = async (event) => { }; } }; + +export const handler = middy(uploadToIPFS).use(authMiddleware()); diff --git a/web/src/pages/Cases/CaseDetails/Evidence/SubmitEvidenceModal.tsx b/web/src/pages/Cases/CaseDetails/Evidence/SubmitEvidenceModal.tsx index ceafeb276..3848a2ad8 100644 --- a/web/src/pages/Cases/CaseDetails/Evidence/SubmitEvidenceModal.tsx +++ b/web/src/pages/Cases/CaseDetails/Evidence/SubmitEvidenceModal.tsx @@ -11,6 +11,7 @@ import { prepareWriteEvidenceModule } from "hooks/contracts/generated"; import { uploadFormDataToIPFS } from "utils/uploadFormDataToIPFS"; import { wrapWithToast, OPTIONS as toastOptions } from "utils/wrapWithToast"; +import { EnsureAuth } from "components/EnsureAuth"; import { EnsureChain } from "components/EnsureChain"; const StyledModal = styled(Modal)` @@ -67,35 +68,37 @@ const SubmitEvidenceModal: React.FC<{