Spaces:
Sleeping

File size: 1,002 Bytes
7f81110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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);
    }
};