refactor: pass entire request object to verifyQstashHeader

This commit is contained in:
2024-09-06 18:06:13 -05:00
parent 3ded897b77
commit 57fbdfaabe
3 changed files with 7 additions and 21 deletions

View File

@@ -16,14 +16,7 @@ import { getUpcomingTitlesFromAnilist } from "./anilist";
const app = new Hono();
app.post("/", async (c) => {
if (
!(await verifyQstashHeader(
env<Env, typeof c>(c, "workerd"),
c.req.path,
c.req.header("Upstash-Signature"),
await c.req.text(),
))
) {
if (!(await verifyQstashHeader(env<Env, typeof c>(c, "workerd"), c.req))) {
return c.json(ErrorResponse, { status: 401 });
}

View File

@@ -73,14 +73,7 @@ app.openapi(route, async (c) => {
const aniListToken = c.req.header("X-AniList-Token");
if (isRetrying) {
if (
!(await verifyQstashHeader(
env<Env, typeof c>(c, "workerd"),
c.req.path,
c.req.header("Upstash-Signature"),
await c.req.text(),
))
) {
if (!(await verifyQstashHeader(env<Env, typeof c>(c, "workerd"), c.req))) {
return c.json(ErrorResponse, { status: 401 });
}
} else {

View File

@@ -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<boolean> {
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) {