Skip to content

Commit d71a202

Browse files
committed
[Fixes #47] Replace http_build_query with custom function
http_build_query in the method does URL-encoding to its components. While it may seem a logical thing to do, technically the string to be constructed is not a URL. Shopify doesn't do such encoding and as a result, the generated hash value does not match. For example in the case when Shopify supplies protocol=http:// query parameter and current implementation with http_build_query encodes it to protocol=https%3A%2F%2F resulting in false negative result.
1 parent 7a06c4b commit d71a202

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

lib/AuthHelper.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,23 @@ public static function getCurrentUrl()
3232
return "$protocol://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
3333
}
3434

35+
/**
36+
* Build a query string from a data array
37+
* This is a replacement for http_build_query because that returns an url-encoded string.
38+
*
39+
* @param array $data Data array
40+
*
41+
* @return array
42+
*/
43+
public static function buildQueryString($data)
44+
{
45+
$paramStrings = [];
46+
foreach ($data as $key => $value) {
47+
$paramStrings[] = "$key=$value";
48+
}
49+
return join('&', $paramStrings);
50+
}
51+
3552
/**
3653
* Verify if the request is made from shopify using hmac hash value
3754
*
@@ -61,7 +78,7 @@ public static function verifyShopifyRequest()
6178
unset($data['signature']);
6279
}
6380
//Create data string for the remaining url parameters
64-
$dataString = http_build_query($data);
81+
$dataString = self::buildQueryString($data);
6582

6683
$realHmac = hash_hmac('sha256', $dataString, $sharedSecret);
6784

0 commit comments

Comments
 (0)