2017-11-17 6 views
0

Dies ist mein Code:Shopify hmac Überprüfung 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; 
} 

Die hmac von Shopi und hmac von meinem Code erstellt ist nicht passend.

Was mache ich falsch?

Antwort

1

Sie müssen die Anforderungsparameter nur beim Erstellen der Liste der Schlüssel/Wert-Paare angeben - benötigen Sie nicht "protocol = https: //".

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

Sie müssen urldecode() das Ergebnis http_build_query(). Es gibt eine URL-codierte Abfragezeichenfolge zurück.

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

Statt:

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

Etwas wie folgt aus:

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

Dieses nur knapp sein Ziel mir helfen :( –