Skip to content

Commit

Permalink
Merge pull request #32 from kamermans/stream_for-deprecation
Browse files Browse the repository at this point in the history
Avoid deprecations, update test platform
  • Loading branch information
kamermans authored Jul 14, 2021
2 parents 840fde5 + 9a1d256 commit ececd21
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: php

dist: trusty
dist: bionic

php:
- 5.4
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Guzzle OAuth 2.0 Subscriber

> [![Build Status](https://travis-ci.org/kamermans/guzzle-oauth2-subscriber.svg?branch=master)](https://travis-ci.org/kamermans/guzzle-oauth2-subscriber)
> Tested with Guzzle 4, 5, 6, 7 and PHP 5.4, 5.5, 5.6, 7.0, 7.1, 7.2, 7.3 and 7.4.
This is an OAuth 2.0 client for Guzzle which aims to be 100% compatible with Guzzle 4, 5, 6, 7 and all future versions within a single package.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kamermans/guzzle-oauth2-subscriber",
"description": "OAuth 2.0 client for Guzzle 4, 5 and 6+",
"description": "OAuth 2.0 client for Guzzle 4, 5, 6 and 7+",
"keywords": ["oauth", "guzzle"],
"license": "MIT",
"authors": [
Expand Down
2 changes: 1 addition & 1 deletion guzzle_environments/run_tests_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function run_tests()
echo "# Running tests against Guzzle $GUZZLE_VER"
echo "###############################################"

docker run -ti --rm \
docker run --rm \
-v $DIR/../:/test \
--workdir=/test/guzzle_environments/$GUZZLE_VER \
--entrypoint=/bin/sh \
Expand Down
4 changes: 2 additions & 2 deletions src/GrantType/AuthorizationCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function getRawData(SignerInterface $clientCredentialsSigner, $refreshTok
}

/**
* @return PostBody
* @return PostBody|\Psr\Http\Message\StreamInterface
*/
protected function getPostBody()
{
Expand All @@ -89,7 +89,7 @@ protected function getPostBody()
$data['redirect_uri'] = $this->config['redirect_uri'];
}

return \GuzzleHttp\Psr7\stream_for(http_build_query($data, '', '&'));
return Helper::streamFor(http_build_query($data, '', '&'));
}

$postBody = new PostBody();
Expand Down
4 changes: 2 additions & 2 deletions src/GrantType/ClientCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function getRawData(SignerInterface $clientCredentialsSigner, $refreshTok
}

/**
* @return PostBody
* @return PostBody|\Psr\Http\Message\StreamInterface
*/
protected function getPostBody()
{
Expand All @@ -90,7 +90,7 @@ protected function getPostBody()
$data['audience'] = $this->config['audience'];
}

return \GuzzleHttp\Psr7\stream_for(http_build_query($data, '', '&'));
return Helper::streamFor(http_build_query($data, '', '&'));
}

$postBody = new PostBody();
Expand Down
6 changes: 3 additions & 3 deletions src/GrantType/PasswordCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public function getRawData(SignerInterface $clientCredentialsSigner, $refreshTok
}

/**
* @return PostBody
*/
* @return PostBody|\Psr\Http\Message\StreamInterface
*/
protected function getPostBody()
{
if (Helper::guzzleIs('>=', '6')) {
Expand All @@ -86,7 +86,7 @@ protected function getPostBody()
$data['scope'] = $this->config['scope'];
}

return \GuzzleHttp\Psr7\stream_for(http_build_query($data, '', '&'));
return Helper::streamFor(http_build_query($data, '', '&'));
}

$postBody = new PostBody();
Expand Down
6 changes: 3 additions & 3 deletions src/GrantType/RefreshToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public function getRawData(SignerInterface $clientCredentialsSigner, $refreshTok
}

/**
* @return PostBody
*/
* @return PostBody|\Psr\Http\Message\StreamInterface
*/
protected function getPostBody($refreshToken)
{
if (Helper::guzzleIs('>=', '6')) {
Expand All @@ -86,7 +86,7 @@ protected function getPostBody($refreshToken)
$data['scope'] = $this->config['scope'];
}

return \GuzzleHttp\Psr7\stream_for(http_build_query($data, '', '&'));
return Helper::streamFor(http_build_query($data, '', '&'));
}

$postBody = new PostBody();
Expand Down
2 changes: 1 addition & 1 deletion src/GrantType/Specific/GithubApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ protected function getPostBody()

$postBody = json_encode($postBody);

return Helper::guzzleIs('<', 6)? Stream::factory($postBody): \GuzzleHttp\Psr7\stream_for($postBody);
return Helper::guzzleIs('<', 6)? Stream::factory($postBody): Helper::streamFor($postBody);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Signer/ClientCredentials/PostFormData.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function sign($request, $clientId, $clientSecret)
$data[$this->clientIdField] = $clientId;
$data[$this->clientSecretField] = $clientSecret;

$body_stream = \GuzzleHttp\Psr7\stream_for(http_build_query($data, '', '&'));
$body_stream = Helper::streamFor(http_build_query($data, '', '&'));
return $request->withBody($body_stream);
}

Expand Down
23 changes: 23 additions & 0 deletions src/Utils/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@

class Helper
{
/**
* Create a new stream based on the input type.
*
* Options is an associative array that can contain the following keys:
* - metadata: Array of custom metadata.
* - size: Size of the stream.
*
* @param resource|string|null|int|float|bool|StreamInterface|callable|\Iterator $resource Entity body data
* @param array $options Additional options
*
* @return StreamInterface
* @throws \InvalidArgumentException if the $resource arg is not valid.
*/
public static function streamFor($resource = '', array $options = [])
{
// stream_for was used until GuzzleHttp\Psr7 v1.7.0
if (function_exists('GuzzleHttp\Psr7\stream_for')) {
return \GuzzleHttp\Psr7\stream_for($resource, $options);
}

return \GuzzleHttp\Psr7\Utils::streamFor($resource, $options);
}

public static function guzzleIs($operator, $version, $guzzle_version=null)
{
if ($guzzle_version === null) {
Expand Down
2 changes: 1 addition & 1 deletion tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected function parseQueryString($query_string)
protected function setPostBody($request, array $data=[])
{
if (Helper::guzzleIs('>=', 6)) {
return $request->withBody(\GuzzleHttp\Psr7\stream_for(http_build_query($data, '', '&')));
return $request->withBody(Helper::streamFor(http_build_query($data, '', '&')));
}

$request->setBody(new PostBody($data));
Expand Down
27 changes: 0 additions & 27 deletions tests/OAuth2MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,31 +568,4 @@ public function testOnErrorDoesNotLoop()
$this->assertSame(401, $container[0]['response']->getStatusCode());
$this->assertSame(401, $container[1]['response']->getStatusCode());
}

public function __DISABLED__testOnErrorDoesNotLoop()
{
// Setup Grant Type
$grant = $this->getMockBuilder('\kamermans\OAuth2\GrantType\ClientCredentials')
->setMethods(['getRawData'])
->disableOriginalConstructor()
->getMock();

$grant->expects($this->exactly(0))
->method('getRawData');

// Setup OAuth2Middleware
$sub = new OAuth2Middleware($grant);

$client = new Client();
$request = new Request('GET', '/', [], null, ['auth' => 'oauth']);
// This header keeps the subscriber from trying to reauth a reauth request (infinte loop)
$request->setHeader('X-Guzzle-Retry', 1);
$response = new Response(401);
$transaction = $this->getTransaction($client, $request);
$except = new RequestException('foo', $request, $response);
$event = new ErrorEvent($transaction, $except);

// Force an onError event, which triggers the signer and grant data processor
$sub->onError($event);
}
}

0 comments on commit ececd21

Please sign in to comment.