Skip to content

Commit

Permalink
fix: rollback webhook handlers runnig on edge
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierniki committed Sep 11, 2023
1 parent 4dc4cd0 commit 6ac5e00
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 41 deletions.
4 changes: 0 additions & 4 deletions src/app/api/algolia-webhook/publish/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { errorToNextResponse } from "../httpError"
import { NextRequestWithValidBody, validateBody } from "../validateBody"
import { validateSignature } from "../validateSignature"

export const runtime = "edge"

async function handleAlgoliaPublishWebhook(req: NextRequestWithValidBody<z.infer<typeof bodySchema>>) {
const article = req.validBody.data

Expand All @@ -26,8 +24,6 @@ async function handleAlgoliaPublishWebhook(req: NextRequestWithValidBody<z.infer
})
)

console.log(indexingResults)

return NextResponse.json({ result: indexingResults }, { status: 201 })
}

Expand Down
2 changes: 0 additions & 2 deletions src/app/api/algolia-webhook/unpublish/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { errorToNextResponse } from "../httpError"
import { NextRequestWithValidBody, validateBody } from "../validateBody"
import { validateSignature } from "../validateSignature"

export const runtime = "edge"

async function handleAlgoliaUnpublishWebhook(req: NextRequestWithValidBody<z.infer<typeof bodySchema>>) {
const article = req.validBody.data

Expand Down
36 changes: 1 addition & 35 deletions src/app/api/algolia-webhook/validateSignature.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NextRequest } from "next/server"
import { HttpError } from "./httpError"
import { verifyWebhookSignature } from "@hygraph/utils"
import { env } from "../../../env.mjs"

export const validateSignature = async (req: NextRequest) => {
Expand All @@ -22,38 +23,3 @@ export type NextRequestWithBody = NextRequest & { parsedBody: unknown }

export const hasParsedBody = (req: NextRequestWithBody | NextRequest): req is NextRequestWithBody =>
Boolean((req as NextRequestWithBody).body)

async function verifyWebhookSignature({
body,
signature,
secret,
rawPayload,
}: {
body?: unknown
signature: string
secret: string
rawPayload?: string
}) {
const [rawSign, rawEnv, rawTimestamp] = signature.split(", ")

const sign = rawSign.replace("sign=", "")
const EnvironmentName = rawEnv.replace("env=", "")
const Timestamp = parseInt(rawTimestamp.replace("t=", ""))

const payload = JSON.stringify({
Body: rawPayload || JSON.stringify(body),
EnvironmentName,
TimeStamp: Timestamp,
})

const encoder = new TextEncoder()
const key = await crypto.subtle.importKey("raw", encoder.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, [
"sign",
])

const signatureArrayBuffer = await crypto.subtle.sign("HMAC", key, encoder.encode(payload))

const hash = btoa(String.fromCharCode(...Array.from(new Uint8Array(signatureArrayBuffer))))

return sign === hash
}

0 comments on commit 6ac5e00

Please sign in to comment.