Skip to content

Commit

Permalink
[Fixes #47] Replace http_build_query with custom function
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tareqtms committed Jun 11, 2019
1 parent 7a06c4b commit d71a202
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/AuthHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ public static function getCurrentUrl()
return "$protocol://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
}

/**
* Build a query string from a data array
* This is a replacement for http_build_query because that returns an url-encoded string.
*
* @param array $data Data array
*
* @return array
*/
public static function buildQueryString($data)
{
$paramStrings = [];
foreach ($data as $key => $value) {
$paramStrings[] = "$key=$value";
}
return join('&', $paramStrings);
}

/**
* Verify if the request is made from shopify using hmac hash value
*
Expand Down Expand Up @@ -61,7 +78,7 @@ public static function verifyShopifyRequest()
unset($data['signature']);
}
//Create data string for the remaining url parameters
$dataString = http_build_query($data);
$dataString = self::buildQueryString($data);

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

Expand Down

0 comments on commit d71a202

Please sign in to comment.