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(); const app = new Hono();
app.post("/", async (c) => { app.post("/", async (c) => {
if ( if (!(await verifyQstashHeader(env<Env, typeof c>(c, "workerd"), c.req))) {
!(await verifyQstashHeader(
env<Env, typeof c>(c, "workerd"),
c.req.path,
c.req.header("Upstash-Signature"),
await c.req.text(),
))
) {
return c.json(ErrorResponse, { status: 401 }); 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"); const aniListToken = c.req.header("X-AniList-Token");
if (isRetrying) { if (isRetrying) {
if ( if (!(await verifyQstashHeader(env<Env, typeof c>(c, "workerd"), c.req))) {
!(await verifyQstashHeader(
env<Env, typeof c>(c, "workerd"),
c.req.path,
c.req.header("Upstash-Signature"),
await c.req.text(),
))
) {
return c.json(ErrorResponse, { status: 401 }); return c.json(ErrorResponse, { status: 401 });
} }
} else { } else {

View File

@@ -1,13 +1,13 @@
import { Receiver, SignatureError } from "@upstash/qstash"; import { Receiver, SignatureError } from "@upstash/qstash";
import type { HonoRequest } from "hono";
import type { Env } from "~/types/env"; import type { Env } from "~/types/env";
export async function verifyQstashHeader( export async function verifyQstashHeader(
env: Env, env: Env,
route: string, req: HonoRequest,
signature: string | undefined,
body: string,
): Promise<boolean> { ): Promise<boolean> {
const signature = req.header("Upstash-Signature");
if (!signature) { if (!signature) {
return Promise.resolve(false); return Promise.resolve(false);
} }
@@ -19,9 +19,9 @@ export async function verifyQstashHeader(
}); });
return await receiver.verify({ return await receiver.verify({
body, body: await req.text(),
signature, signature,
url: `https://aniplay-v2.rururu.workers.dev${route}`, url: req.url,
}); });
} catch (error) { } catch (error) {
if (error instanceof SignatureError) { if (error instanceof SignatureError) {