Hello, I’m having trouble verifying the HMAC header within Firebase functions. Can someone spot what we’re doing wrong?
import crypto from "crypto";
import { logger } from "firebase-functions/v2";
import { Request } from "firebase-functions/v2/https";
const CLIENT_SECRET = process.env.SHOPIFY_CLIENT_SECRET!;
export function verifyShopifyHmac(req: Request) {
if (!req.body) return false;
const hmacHeader = req.header("X-Shopify-Hmac-Sha256");
if (!hmacHeader) {
return false;
}
const computedHmac = crypto
.createHmac("sha256", CLIENT_SECRET)
.update(req.body, "utf-8")
.digest("base64");
logger.debug({ hmacHeader });
logger.debug({ computedHmac });
logger.debug({ equal: hmacHeader === computedHmac });
if (
Buffer.from(computedHmac) !== Buffer.from(hmacHeader as string, "base64")
) {
return false;
}
return true;
}