Skip to content

Commit f145fde

Browse files
committed
Merge pull request #520 from thephpleague/fix/query-string-separator
Ensure generated query strings are consistent
2 parents 317812a + addfc24 commit f145fde

File tree

4 files changed

+73
-4
lines changed

4 files changed

+73
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# OAuth 2.0 Client Changelog
22

3+
## 1.4.1
4+
5+
_Released: 2016-04-29_
6+
7+
* Add `QueryBuilderTrait` to standardize query string generation.
8+
39
## 1.4.0
410

511
_Released: 2016-04-19_

src/Provider/AbstractProvider.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
use League\OAuth2\Client\Grant\GrantFactory;
2222
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
2323
use League\OAuth2\Client\Token\AccessToken;
24-
use League\OAuth2\Client\Tool\RequestFactory;
2524
use League\OAuth2\Client\Tool\ArrayAccessorTrait;
25+
use League\OAuth2\Client\Tool\QueryBuilderTrait;
26+
use League\OAuth2\Client\Tool\RequestFactory;
2627
use Psr\Http\Message\RequestInterface;
2728
use Psr\Http\Message\ResponseInterface;
2829
use RandomLib\Factory as RandomFactory;
@@ -36,6 +37,7 @@
3637
abstract class AbstractProvider
3738
{
3839
use ArrayAccessorTrait;
40+
use QueryBuilderTrait;
3941

4042
/**
4143
* @var string Key used in a token response to identify the resource owner.
@@ -368,7 +370,7 @@ protected function getAuthorizationParameters(array $options)
368370
*/
369371
protected function getAuthorizationQuery(array $params)
370372
{
371-
return http_build_query($params);
373+
return $this->buildQueryString($params);
372374
}
373375

374376
/**
@@ -454,7 +456,7 @@ protected function getAccessTokenResourceOwnerId()
454456
*/
455457
protected function getAccessTokenQuery(array $params)
456458
{
457-
return http_build_query($params);
459+
return $this->buildQueryString($params);
458460
}
459461

460462
/**
@@ -500,7 +502,7 @@ protected function getAccessTokenUrl(array $params)
500502
*/
501503
protected function getAccessTokenBody(array $params)
502504
{
503-
return http_build_query($params);
505+
return $this->buildQueryString($params);
504506
}
505507

506508
/**

src/Tool/QueryBuilderTrait.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* This file is part of the league/oauth2-client library
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
9+
* @license http://opensource.org/licenses/MIT MIT
10+
* @link http://thephpleague.com/oauth2-client/ Documentation
11+
* @link https://packagist.org/packages/league/oauth2-client Packagist
12+
* @link https://github.com/thephpleague/oauth2-client GitHub
13+
*/
14+
15+
namespace League\OAuth2\Client\Tool;
16+
17+
/**
18+
* Provides a standard way to generate query strings.
19+
*/
20+
trait QueryBuilderTrait
21+
{
22+
/**
23+
* Build a query string from an array.
24+
*
25+
* @param array $params
26+
*
27+
* @return string
28+
*/
29+
protected function buildQueryString(array $params)
30+
{
31+
return http_build_query($params, null, '&');
32+
}
33+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace League\OAuth2\Client\Test\Tool;
4+
5+
use League\OAuth2\Client\Tool\QueryBuilderTrait;
6+
use PHPUnit_Framework_TestCase;
7+
8+
class QueryBuilderTraitTest extends PHPUnit_Framework_TestCase
9+
{
10+
use QueryBuilderTrait;
11+
12+
public function setUp()
13+
{
14+
ini_set('arg_separator.output', '&amp;');
15+
}
16+
17+
public function testBuildQueryString()
18+
{
19+
$params = [
20+
'a' => 'foo',
21+
'b' => 'bar',
22+
];
23+
24+
$query = $this->buildQueryString($params);
25+
26+
$this->assertSame('a=foo&b=bar', $query);
27+
}
28+
}

0 commit comments

Comments
 (0)