Skip to content

Commit

Permalink
Ensure generated query strings are consistent
Browse files Browse the repository at this point in the history
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
  • Loading branch information
shadowhand committed Apr 29, 2016
1 parent 317812a commit addfc24
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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_
Expand Down
10 changes: 6 additions & 4 deletions src/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,6 +37,7 @@
abstract class AbstractProvider
{
use ArrayAccessorTrait;
use QueryBuilderTrait;

/**
* @var string Key used in a token response to identify the resource owner.
Expand Down Expand Up @@ -368,7 +370,7 @@ protected function getAuthorizationParameters(array $options)
*/
protected function getAuthorizationQuery(array $params)
{
return http_build_query($params);
return $this->buildQueryString($params);
}

/**
Expand Down Expand Up @@ -454,7 +456,7 @@ protected function getAccessTokenResourceOwnerId()
*/
protected function getAccessTokenQuery(array $params)
{
return http_build_query($params);
return $this->buildQueryString($params);
}

/**
Expand Down Expand Up @@ -500,7 +502,7 @@ protected function getAccessTokenUrl(array $params)
*/
protected function getAccessTokenBody(array $params)
{
return http_build_query($params);
return $this->buildQueryString($params);
}

/**
Expand Down
33 changes: 33 additions & 0 deletions src/Tool/QueryBuilderTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <[email protected]>
* @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, '&');
}
}
28 changes: 28 additions & 0 deletions test/src/Tool/QueryBuilderTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace League\OAuth2\Client\Test\Tool;

use League\OAuth2\Client\Tool\QueryBuilderTrait;
use PHPUnit_Framework_TestCase;

class QueryBuilderTraitTest extends PHPUnit_Framework_TestCase
{
use QueryBuilderTrait;

public function setUp()
{
ini_set('arg_separator.output', '&amp;');
}

public function testBuildQueryString()
{
$params = [
'a' => 'foo',
'b' => 'bar',
];

$query = $this->buildQueryString($params);

$this->assertSame('a=foo&b=bar', $query);
}
}

0 comments on commit addfc24

Please sign in to comment.