From addfc24bbaa5567982afdf1c40f01283c88ac286 Mon Sep 17 00:00:00 2001 From: Woody Gilk Date: Fri, 29 Apr 2016 10:04:57 -0500 Subject: [PATCH] Ensure generated query strings are consistent PHP configuration can be modified to set a different argument separator from the standard `&`. Generated query strings should always use the standard separator. Fixes #519 --- CHANGELOG.md | 6 +++++ src/Provider/AbstractProvider.php | 10 +++++--- src/Tool/QueryBuilderTrait.php | 33 +++++++++++++++++++++++++ test/src/Tool/QueryBuilderTraitTest.php | 28 +++++++++++++++++++++ 4 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 src/Tool/QueryBuilderTrait.php create mode 100644 test/src/Tool/QueryBuilderTraitTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 90ff591b..c64774ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # OAuth 2.0 Client Changelog +## 1.4.1 + +_Released: 2016-04-29_ + +* Add `QueryBuilderTrait` to standardize query string generation. + ## 1.4.0 _Released: 2016-04-19_ diff --git a/src/Provider/AbstractProvider.php b/src/Provider/AbstractProvider.php index 950d2c70..4c80dfe9 100644 --- a/src/Provider/AbstractProvider.php +++ b/src/Provider/AbstractProvider.php @@ -21,8 +21,9 @@ use League\OAuth2\Client\Grant\GrantFactory; use League\OAuth2\Client\Provider\Exception\IdentityProviderException; use League\OAuth2\Client\Token\AccessToken; -use League\OAuth2\Client\Tool\RequestFactory; use League\OAuth2\Client\Tool\ArrayAccessorTrait; +use League\OAuth2\Client\Tool\QueryBuilderTrait; +use League\OAuth2\Client\Tool\RequestFactory; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use RandomLib\Factory as RandomFactory; @@ -36,6 +37,7 @@ abstract class AbstractProvider { use ArrayAccessorTrait; + use QueryBuilderTrait; /** * @var string Key used in a token response to identify the resource owner. @@ -368,7 +370,7 @@ protected function getAuthorizationParameters(array $options) */ protected function getAuthorizationQuery(array $params) { - return http_build_query($params); + return $this->buildQueryString($params); } /** @@ -454,7 +456,7 @@ protected function getAccessTokenResourceOwnerId() */ protected function getAccessTokenQuery(array $params) { - return http_build_query($params); + return $this->buildQueryString($params); } /** @@ -500,7 +502,7 @@ protected function getAccessTokenUrl(array $params) */ protected function getAccessTokenBody(array $params) { - return http_build_query($params); + return $this->buildQueryString($params); } /** diff --git a/src/Tool/QueryBuilderTrait.php b/src/Tool/QueryBuilderTrait.php new file mode 100644 index 00000000..468f3b85 --- /dev/null +++ b/src/Tool/QueryBuilderTrait.php @@ -0,0 +1,33 @@ + + * @license http://opensource.org/licenses/MIT MIT + * @link http://thephpleague.com/oauth2-client/ Documentation + * @link https://packagist.org/packages/league/oauth2-client Packagist + * @link https://github.com/thephpleague/oauth2-client GitHub + */ + +namespace League\OAuth2\Client\Tool; + +/** + * Provides a standard way to generate query strings. + */ +trait QueryBuilderTrait +{ + /** + * Build a query string from an array. + * + * @param array $params + * + * @return string + */ + protected function buildQueryString(array $params) + { + return http_build_query($params, null, '&'); + } +} diff --git a/test/src/Tool/QueryBuilderTraitTest.php b/test/src/Tool/QueryBuilderTraitTest.php new file mode 100644 index 00000000..bfc05d53 --- /dev/null +++ b/test/src/Tool/QueryBuilderTraitTest.php @@ -0,0 +1,28 @@ + 'foo', + 'b' => 'bar', + ]; + + $query = $this->buildQueryString($params); + + $this->assertSame('a=foo&b=bar', $query); + } +}