Spaces:
Sleeping
Sleeping
Download src/libs/shared/presentation/http/middleware/security/WebhookHmacMiddleware.ts from fouadkhalid123/asd: direct link, hf CLI and curl.
- Browser
- Download file 1 kB
-
https://huggingface.co/spaces/fouadkhalid123/asd/resolve/main/src/libs/shared/presentation/http/middleware/security/WebhookHmacMiddleware.ts
- Command line
-
hf download hf://spaces/fouadkhalid123/asd/src/libs/shared/presentation/http/middleware/security/WebhookHmacMiddleware.ts
-
curl -L -o WebhookHmacMiddleware.ts https://huggingface.co/spaces/fouadkhalid123/asd/resolve/main/src/libs/shared/presentation/http/middleware/security/WebhookHmacMiddleware.ts
1 kB
| import { Request, Response, NextFunction } from "express"; | |
| import { createHmac, timingSafeEqual } from "crypto"; | |
| import { UnauthorizedError } from "../../../../domain/errors/UnauthorizedError"; | |
| export const WebhookHmacMiddleware = (req: Request, res: Response, next: NextFunction) => { | |
| try { | |
| const hmacHeader = req.headers["x-shopify-hmac-sha256"]; | |
| if (!hmacHeader) { | |
| throw new UnauthorizedError("HMAC header missing"); | |
| } | |
| const secret = process.env.SHOPIFY_WEBHOOK_SECRET || ""; | |
| const rawBody = (req as any).rawBody; | |
| if (!rawBody) { | |
| throw new UnauthorizedError("Raw body missing for validation"); | |
| } | |
| const hash = createHmac("sha256", secret).update(rawBody).digest("base64"); | |
| if (!timingSafeEqual(Buffer.from(hash), Buffer.from(String(hmacHeader)))) { | |
| throw new UnauthorizedError("HMAC validation failed"); | |
| } | |
| next(); | |
| } catch (error) { | |
| next(error); | |
| } | |
| }; | |