我正在尝试使用Gigya的注释通知功能,我遵循了以下指南: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服务器提供的签名不同。我做错了什么?
发布于 2015-03-31 19:57:46
尝试下面的代码:
<?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使用的方法相同,但是这里的优点是这是一个更小的内存占用,因为不需要加载整个GigyaSDK。
此外,我不确定这是否有意为之,但您的比较有以下代码:
if(!$signature !== $expectedSignature) {而不是:
if ($signature !== $expectedSignature) {我不太清楚$signature上无关的逻辑-非操作符的目的是什么,但这似乎会导致意外的行为。
https://stackoverflow.com/questions/29285903
复制相似问题