From 57fbdfaabed6e6825205c190702482366aa9637a Mon Sep 17 00:00:00 2001 From: Rushil Perera Date: Fri, 6 Sep 2024 18:06:13 -0500 Subject: [PATCH] refactor: pass entire request object to verifyQstashHeader --- src/controllers/upcoming-titles/index.ts | 9 +-------- src/controllers/watch-status/index.ts | 9 +-------- src/libs/qstash/verifyQstashHeader.ts | 10 +++++----- 3 files changed, 7 insertions(+), 21 deletions(-) diff --git a/src/controllers/upcoming-titles/index.ts b/src/controllers/upcoming-titles/index.ts index a212714..f97934a 100644 --- a/src/controllers/upcoming-titles/index.ts +++ b/src/controllers/upcoming-titles/index.ts @@ -16,14 +16,7 @@ import { getUpcomingTitlesFromAnilist } from "./anilist"; const app = new Hono(); app.post("/", async (c) => { - if ( - !(await verifyQstashHeader( - env(c, "workerd"), - c.req.path, - c.req.header("Upstash-Signature"), - await c.req.text(), - )) - ) { + if (!(await verifyQstashHeader(env(c, "workerd"), c.req))) { return c.json(ErrorResponse, { status: 401 }); } diff --git a/src/controllers/watch-status/index.ts b/src/controllers/watch-status/index.ts index 2be9053..5fadcc3 100644 --- a/src/controllers/watch-status/index.ts +++ b/src/controllers/watch-status/index.ts @@ -73,14 +73,7 @@ app.openapi(route, async (c) => { const aniListToken = c.req.header("X-AniList-Token"); if (isRetrying) { - if ( - !(await verifyQstashHeader( - env(c, "workerd"), - c.req.path, - c.req.header("Upstash-Signature"), - await c.req.text(), - )) - ) { + if (!(await verifyQstashHeader(env(c, "workerd"), c.req))) { return c.json(ErrorResponse, { status: 401 }); } } else { diff --git a/src/libs/qstash/verifyQstashHeader.ts b/src/libs/qstash/verifyQstashHeader.ts index f529ee5..4645dd9 100644 --- a/src/libs/qstash/verifyQstashHeader.ts +++ b/src/libs/qstash/verifyQstashHeader.ts @@ -1,13 +1,13 @@ import { Receiver, SignatureError } from "@upstash/qstash"; +import type { HonoRequest } from "hono"; import type { Env } from "~/types/env"; export async function verifyQstashHeader( env: Env, - route: string, - signature: string | undefined, - body: string, + req: HonoRequest, ): Promise { + const signature = req.header("Upstash-Signature"); if (!signature) { return Promise.resolve(false); } @@ -19,9 +19,9 @@ export async function verifyQstashHeader( }); return await receiver.verify({ - body, + body: await req.text(), signature, - url: `https://aniplay-v2.rururu.workers.dev${route}`, + url: req.url, }); } catch (error) { if (error instanceof SignatureError) {