Gigya PHP Sdk-评论通知


Gigya PHP Sdk - Comment Notifications

我正在尝试使用Gigya的注释通知功能,我遵循了以下指南:http://developers.gigya.com/010_Developer_Guide/18_Plugins/022_Comments_Version_2/Comment_Notifications

我开发了以下代码:

<?php
    require_once('GSSDK.php');
    $event = $_POST['event'];
    $eventData = $_POST['eventData'];
    $nonce = $_POST['nonce'];
    $timestamp = $_POST['timestamp'];
    $signature = $_POST['signature'];
    $signatureBase = sprintf("%s_%s_%s_%s", $event, $eventData, $nonce, $timestamp);
    $expectedSignature = SigUtils::calcSignature(
        $signatureBase,
        MY_SECRET_KEY);
    if($signature !== $expectedSignature) {
        header('HTTP/1.0 403 Forbidden');
        die();
    }
    //Some other stuff
    exit();
?>

但它从来没有涉及到"//其他一些东西"的部分。期望的签名总是与Gigya服务器提供的签名不同。我做错了什么?

请尝试以下代码:

<?php
  static function calcSignature($baseString,$key)
  {
    $baseString = utf8_encode($baseString);
    $rawHmac = hash_hmac("sha1", utf8_encode($baseString), base64_decode($key), true);
    $sig = base64_encode($rawHmac); 
    return $sig;
  }
  function checkSignature() 
  {
    $event = $_POST["event"];
    $eventData = $_POST["eventData"];
    $nonce = $_POST["nonce"];
    $timestamp = $_POST["timestamp"];
    $signature = $_POST["signature"];
    $signatureBase = $event . "_" . $eventData . "_" . $nonce . "_" . $timestamp;
    $secret = "[your gigya secret key]";
    $expectedSignature = calcSignature($signatureBase, $secret);        
    // Now compare the expectedSignature value to the signature value returned in the callback
    if ($signature !== $expectedSignature) 
    {
      header('HTTP/1.0 403 Forbidden');
      die();
    }
  }
  checkSignature();
  //Some other stuff
  exit();
?>

这段代码删除了对GigyaSDK的依赖,只是为了检查签名。提供的方法与GigyaSDK使用的方法相同,但这里的优点是,由于不需要加载整个GigyaSSDK,因此占用的内存要小得多。

此外,我不确定这是否是故意的,但你的比较有代码:

if(!$signature !== $expectedSignature) {

代替:

if ($signature !== $expectedSignature) {

我不太确定$signature上的无关逻辑not运算符应该实现什么目的,但这似乎会导致意外行为。