SHA256 webhook signature from WooCommerce never verifies

后端 未结 6 998
再見小時候
再見小時候 2021-01-02 23:47

I am receiving webhooks from a woocommerce site into a nodejs/express application. I am trying to verify the webhook\'s signature to prove authenticity, yet the hash I compu

6条回答
  •  孤城傲影
    2021-01-03 00:18

    Hope to save someone time, below works for me.

    // Make sure to add a WISTIA_SECRET_KEY in your Environment Variables
    // See https://docs.pipedream.com/environment-variables/
    const secret = process.env.SELF_AUTOMATE_KEY;
    const signature = event.headers["x-wc-webhook-signature"];
    const body = steps.trigger.raw_event["body_b64"];
    const clean_Body = body.replace("body_b64: ", "");
    //const body = steps.trigger.raw_event;
    console.log(event.headers["x-wc-webhook-signature"]);
    
    console.log("Print Body", clean_Body);
    
    if (process.env.SELF_AUTOMATE_KEY === undefined) {
      $end("No WISTIA_SECRET_KEY environment variable defined. Exiting.")
    }
    
    if (!("x-wc-webhook-signature" in event.headers)) {
      $end("No x-wc-webhook-signature header present in the request. Exiting.")
    }
    
    // Once we've confirmed we have a signature, we want to 
    // validate it by generating an HMAC SHA-256 hexdigest
    const crypto = require('crypto');
    
    const hash = crypto.createHmac('sha256',
      secret).update(JSON.stringify(clean_Body), 'base64').digest('base64');
    
    
    
    console.log(hash);
    // $end() ends the execution of a pipeline, presenting a nice message in the "Messages"
    // column in the inspector above. See https://docs.pipedream.com/notebook/code/#end
    if (hash !== signature) {
      $end("The correct secret key was not passed in the event. Exiting!")
    }
    

提交回复
热议问题