2017-11-17 11 views
0

これは私のコードです:shopify HMACの検証PHP

function verifyRequest($request, $secret) { 
    // Per the Shopify docs: 
    // Everything except hmac and signature... 

    $hmac = $request['hmac']; 
    unset($request['hmac']); 
    unset($request['signature']); 

    // Sorted lexilogically... 
    ksort($request); 

    // Special characters replaced... 
    foreach ($request as $k => $val) { 
    $k = str_replace('%', '%25', $k); 
    $k = str_replace('&', '%26', $k); 
    $k = str_replace('=', '%3D', $k); 
    $val = str_replace('%', '%25', $val); 
    $val = str_replace('&', '%26', $val); 
    $params[$k] = $val; 
    } 

    echo $http = "protocol=". urldecode("https://").http_build_query($params) ; 
    echo $test = hash_hmac("sha256", $http , $secret); 

    // enter code hereVerified when equal 
    return $hmac === $test; 
} 

私のコードから作成shopiとHMACからHMACが一致していません。

私は間違っていますか?

答えて

1

「プロトコル= https://」は必要ありません。キーと値のペアのリストを作成する場合は、リクエストパラメータを含める必要があります。

https://help.shopify.com/api/getting-started/authentication/oauth#verification

あなたが(urldecodeする必要があります)http_build_queryの結果()。 URLエンコードされたクエリ文字列を返します。

http://php.net/manual/en/function.http-build-query.php

の代わりに:

echo $http = "protocol=". urldecode("https://").http_build_query($params) ; 
echo $test = hash_hmac("sha256", $http , $secret); 

このような何か:

$http = urldecode(http_build_query($params)); 
$test = hash_hmac('sha256', $http, $secret); 
+0

このdidntのは、私を助け:( –