From 85312ea61394fd7098d49720b9b7077274e2f7fe Mon Sep 17 00:00:00 2001 From: Max Wofford Date: Tue, 12 Oct 2021 15:11:21 -0400 Subject: [PATCH] CDN v1 uses Auth passthrough --- api/v1/new.ts | 15 +++++++++++---- api/v1/newSingle.ts | 12 +++++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/api/v1/new.ts b/api/v1/new.ts index 2d61dc8..0abe50e 100644 --- a/api/v1/new.ts +++ b/api/v1/new.ts @@ -87,15 +87,22 @@ export default async (req: ServerRequest) => { ); } + const authorization = req.headers.get("Authorization"); + const uploadedURLs = await Promise.all(fileURLs.map(async (url, index) => { const { pathname } = urlParse(url); const filename = index + pathname.substr(pathname.lastIndexOf("/") + 1); - const res = await (await fetch("https://cdn.hackclub.com/api/newSingle", { + const headers = { + "Content-Type": "application/json", + "Authorization": "" + } + if (authorization) { + headers['Authorization'] = authorization; + } + const res = await (await fetch("http://localhost:3000/api/newSingle", { method: "POST", - headers: { - "Content-Type": "application/json", - }, + headers, body: url, })).json(); diff --git a/api/v1/newSingle.ts b/api/v1/newSingle.ts index 97ff23b..efbc56e 100644 --- a/api/v1/newSingle.ts +++ b/api/v1/newSingle.ts @@ -10,8 +10,14 @@ const endpoint = (path: string) => { return url; }; -const uploadFile = async (url: string) => { - const req = await fetch(url); +const uploadFile = async (url: string, authorization: string|null) => { + const options = { + method: 'GET', headers: { 'Authorization': "" } + } + if (authorization) { + options.headers = { 'Authorization': authorization } + } + const req = await fetch(url, options); const data = new Uint8Array(await req.arrayBuffer()); const sha = new Hash("sha1").digest(data).hex(); const size = data.byteLength; @@ -53,7 +59,7 @@ export default async (req: ServerRequest) => { }, ); } - const uploadedFileURL = await uploadFile(singleFileURL); + const uploadedFileURL = await uploadFile(singleFileURL, req.headers.get("Authorization")); req.respond({ body: JSON.stringify(uploadedFileURL),