Skip to content

Commit

Permalink
Leave the work of URL encoding to http_build_query()
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroenvermeulen committed Jan 8, 2024
1 parent 6085f51 commit 54b07ee
Showing 1 changed file with 2 additions and 24 deletions.
26 changes: 2 additions & 24 deletions src/OAuth/OAuth1/Signature/Signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getSignature(UriInterface $uri, array $params, $method = 'POST')
{
parse_str($uri->getQuery(), $queryStringData);

$signatureData = $this->urlEncodeArray(array_merge($queryStringData, $params));
$signatureData = array_merge($queryStringData, $params);

// determine base uri
$baseUri = $uri->getScheme() . '://' . $uri->getRawAuthority();
Expand All @@ -66,12 +66,11 @@ public function getSignature(UriInterface $uri, array $params, $method = 'POST')

$baseString = strtoupper($method) . '&';
$baseString .= rawurlencode($baseUri) . '&';
$baseString .= rawurlencode(http_build_query($signatureData, '', '&'));
$baseString .= http_build_query($signatureData, '', '&', PHP_QUERY_RFC3986);

return base64_encode($this->hash($baseString));
}


/**
* @return string
*/
Expand Down Expand Up @@ -117,25 +116,4 @@ protected function hash($data)
);
}
}

/**
* URL encodes both keys and values in an array, recusively.
* Sorts the array by key after encoding.
*
* @param array $data
* @return array
*/
protected function urlEncodeArray(array $data)
{
$result = [];
foreach ($data as $key => $value) {
if (is_array($value)) {
$result[rawurlencode($key)] = $this->rawUrlEncodeArray($value);
} else {
$result[rawurlencode($key)] = rawurlencode($value);
}
}
ksort($result);
return $result;
}
}

0 comments on commit 54b07ee

Please sign in to comment.