Skip to content

Commit

Permalink
Merge pull request #602 from thephpleague/2.x
Browse files Browse the repository at this point in the history
Release version 2.x
  • Loading branch information
ramsey authored Jan 13, 2017
2 parents fbe4f9c + 51c4f45 commit e5b1d8e
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 329 deletions.
35 changes: 30 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
language: php

php:
- 5.5
- 5.6
- 7.0
- hhvm
matrix:
include:
- php: 5.6
- php: 7.0
- php: 7.1
- php: nightly
- php: hhvm-3.6
sudo: required
dist: trusty
group: edge
- php: hhvm-3.9
sudo: required
dist: trusty
group: edge
- php: hhvm-3.12
sudo: required
dist: trusty
group: edge
- php: hhvm-3.15
sudo: required
dist: trusty
group: edge
- php: hhvm-nightly
sudo: required
dist: trusty
group: edge
fast_finish: true
allow_failures:
- php: nightly
- php: hhvm-nightly

before_script:
- travis_retry composer install --no-interaction --prefer-source
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# OAuth 2.0 Client Changelog

## 2.0.0

_Released: 2017-01-12_

* Rename `getResponse()` to `getParsedResponse()`
* Add `getResponse()` method that returns the unparsed PSR-7 `Response` instance
* Removed `RandomFactory`, switched to native random functions

## 1.4.1

_Released: 2016-04-29_
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The following versions of PHP are supported.
* PHP 5.5
* PHP 5.6
* PHP 7.0
* PHP 7.1
* HHVM

## Providers
Expand Down
20 changes: 13 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
"sort-packages": true
},
"require": {
"php": ">=5.5.0",
"ircmaxell/random-lib": "^1.2",
"guzzlehttp/guzzle": "^6.0"
"php": ">=5.6.0",
"guzzlehttp/guzzle": "^6.0",
"paragonie/random_compat": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^4.0",
"mockery/mockery": "~0.9",
"squizlabs/php_codesniffer": "^2.0",
"jakub-onderka/php-parallel-lint": "~0.9"
"eloquent/liberator": "^2.0",
"eloquent/phony": "^0.14.1",
"jakub-onderka/php-parallel-lint": "~0.9",
"phpunit/phpunit": "^5.0",
"squizlabs/php_codesniffer": "^2.0"
},
"keywords": [
"oauth",
Expand Down Expand Up @@ -49,5 +50,10 @@
"psr-4": {
"League\\OAuth2\\Client\\Test\\": "test/src/"
}
},
"extra": {
"branch-alias": {
"dev-2.x": "2.0.x-dev"
}
}
}
70 changes: 19 additions & 51 deletions src/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
use League\OAuth2\Client\Tool\RequestFactory;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use RandomLib\Factory as RandomFactory;
use RandomLib\Generator as RandomGenerator;
use UnexpectedValueException;

/**
Expand Down Expand Up @@ -90,11 +88,6 @@ abstract class AbstractProvider
*/
protected $httpClient;

/**
* @var RandomFactory
*/
protected $randomFactory;

/**
* Constructs an OAuth 2.0 service provider.
*
Expand All @@ -103,7 +96,7 @@ abstract class AbstractProvider
* Individual providers may introduce more options, as needed.
* @param array $collaborators An array of collaborators that may be used to
* override this provider's default behavior. Collaborators include
* `grantFactory`, `requestFactory`, `httpClient`, and `randomFactory`.
* `grantFactory`, `requestFactory`, and `httpClient`.
* Individual providers may introduce more collaborators, as needed.
*/
public function __construct(array $options = [], array $collaborators = [])
Expand Down Expand Up @@ -132,11 +125,6 @@ public function __construct(array $options = [], array $collaborators = [])
);
}
$this->setHttpClient($collaborators['httpClient']);

if (empty($collaborators['randomFactory'])) {
$collaborators['randomFactory'] = new RandomFactory();
}
$this->setRandomFactory($collaborators['randomFactory']);
}

/**
Expand Down Expand Up @@ -228,29 +216,6 @@ public function getHttpClient()
return $this->httpClient;
}

/**
* Sets the instance of the CSPRNG random generator factory.
*
* @param RandomFactory $factory
* @return self
*/
public function setRandomFactory(RandomFactory $factory)
{
$this->randomFactory = $factory;

return $this;
}

/**
* Returns the current CSPRNG random generator factory instance.
*
* @return RandomFactory
*/
public function getRandomFactory()
{
return $this->randomFactory;
}

/**
* Returns the current value of the state parameter.
*
Expand Down Expand Up @@ -299,11 +264,11 @@ abstract public function getResourceOwnerDetailsUrl(AccessToken $token);
*/
protected function getRandomState($length = 32)
{
$generator = $this
->getRandomFactory()
->getMediumStrengthGenerator();
$state = bin2hex(random_bytes($length));

return $generator->generateString($length, RandomGenerator::CHAR_ALNUM);
// Generated bytes will always be longer than requested after
// conversion to a hex and needs to be trimmed.
return substr($state, 0, $length);
}

/**
Expand Down Expand Up @@ -562,7 +527,7 @@ public function getAccessToken($grant, array $options = [])

$params = $grant->prepareRequestParameters($params, $options);
$request = $this->getAccessTokenRequest($params);
$response = $this->getResponse($request);
$response = $this->getParsedResponse($request);
$prepared = $this->prepareAccessTokenResponse($response);
$token = $this->createAccessToken($prepared, $grant);

Expand Down Expand Up @@ -620,17 +585,15 @@ protected function createRequest($method, $url, $token, array $options)
/**
* Sends a request instance and returns a response instance.
*
* WARNING: This method does not attempt to catch exceptions caused by HTTP
* errors! It is recommended to wrap this method in a try/catch block.
*
* @param RequestInterface $request
* @return ResponseInterface
*/
protected function sendRequest(RequestInterface $request)
public function getResponse(RequestInterface $request)
{
try {
$response = $this->getHttpClient()->send($request);
} catch (BadResponseException $e) {
$response = $e->getResponse();
}
return $response;
return $this->getHttpClient()->send($request);
}

/**
Expand All @@ -639,9 +602,14 @@ protected function sendRequest(RequestInterface $request)
* @param RequestInterface $request
* @return mixed
*/
public function getResponse(RequestInterface $request)
public function getParsedResponse(RequestInterface $request)
{
$response = $this->sendRequest($request);
try {
$response = $this->getResponse($request);
} catch (BadResponseException $e) {
$response = $e->getResponse();
}

$parsed = $this->parseResponse($response);

$this->checkResponse($response, $parsed);
Expand Down Expand Up @@ -792,7 +760,7 @@ protected function fetchResourceOwnerDetails(AccessToken $token)

$request = $this->getAuthenticatedRequest(self::METHOD_GET, $url, $token);

return $this->getResponse($request);
return $this->getParsedResponse($request);
}

/**
Expand Down
10 changes: 2 additions & 8 deletions test/src/Grant/GrantFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use League\OAuth2\Client\Grant\AbstractGrant;
use League\OAuth2\Client\Grant\Exception\InvalidGrantException;
use League\OAuth2\Client\Test\Grant\Fake as MockGrant;
use Mockery as m;
use PHPUnit_Framework_TestCase as TestCase;

class GrantFactoryTest extends \PHPUnit_Framework_TestCase
class GrantFactoryTest extends TestCase
{
/**
* @var AbstractGrant
Expand All @@ -20,12 +20,6 @@ protected function setUp()
$this->factory = new GrantFactory();
}

public function tearDown()
{
m::close();
parent::tearDown();
}

/**
* @dataProvider providerGetGrantDefaults
*/
Expand Down
55 changes: 30 additions & 25 deletions test/src/Grant/GrantTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

namespace League\OAuth2\Client\Test\Grant;

use Eloquent\Phony\Phpunit\Phony;
use GuzzleHttp\ClientInterface;
use PHPUnit_Framework_TestCase as TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use League\OAuth2\Client\Token\AccessToken;
use League\OAuth2\Client\Test\Provider\Fake as MockProvider;
use Mockery as m;

abstract class GrantTestCase extends \PHPUnit_Framework_TestCase
abstract class GrantTestCase extends TestCase
{
/** @var \League\OAuth2\Client\Provider\AbstractProvider */
/**
* @var \League\OAuth2\Client\Provider\AbstractProvider
*/
protected $provider;

protected function setUp()
Expand All @@ -23,12 +26,6 @@ protected function setUp()
));
}

public function tearDown()
{
m::close();
parent::tearDown();
}

/**
* Test that the grant's __toString method.
*/
Expand All @@ -53,28 +50,36 @@ abstract protected function getParamExpectation();
*/
public function testGetAccessToken($grant, array $params = [])
{
$stream = m::mock(StreamInterface::class);
$stream->shouldReceive('__toString')->times(1)->andReturn(
// Mock
$stream = Phony::mock(StreamInterface::class);
$stream->__toString->returns(
'{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'
);

$response = m::mock(ResponseInterface::class);
$response->shouldReceive('getBody')->times(1)->andReturn($stream);
$response->shouldReceive('getHeader')->with('content-type')->times(1)->andReturn('application/json');
$response = Phony::mock(ResponseInterface::class);
$response->getBody->returns($stream->get());
$response->getHeader->with('content-type')->returns('application/json');

$paramCheck = $this->getParamExpectation();

$client = m::mock(ClientInterface::class);
$client->shouldReceive('send')->with(
$request = m::on(function ($request) use ($paramCheck) {
parse_str((string) $request->getBody(), $body);
return $paramCheck($body);
})
)->times(1)->andReturn($response);

$this->provider->setHttpClient($client);
$client = Phony::mock(ClientInterface::class);
$client->send->returns($response->get());

// Execute
$this->provider->setHttpClient($client->get());
$token = $this->provider->getAccessToken($grant, $params);

// Verify
$this->assertInstanceOf(AccessToken::class, $token);

Phony::inOrder(
$client->send->times(1)->calledWith(
$this->callback(function ($request) {
parse_str((string) $request->getBody(), $body);
return call_user_func($this->getParamExpectation(), $body);
})
),
$response->getBody->times(1)->called(),
$stream->__toString->times(1)->called(),
$response->getHeader->times(1)->called()
);
}
}
Loading

0 comments on commit e5b1d8e

Please sign in to comment.