From 875126434c9ca4d1eeceb95d7e4858d0a14edddb Mon Sep 17 00:00:00 2001 From: Steve Kamerman Date: Wed, 14 Jul 2021 10:39:15 -0400 Subject: [PATCH 1/5] Avoid deprecations, update test platform --- .travis.yml | 2 +- guzzle_environments/run_tests_docker.sh | 2 +- src/GrantType/AuthorizationCode.php | 4 +-- src/GrantType/ClientCredentials.php | 4 +-- src/GrantType/PasswordCredentials.php | 6 ++--- src/GrantType/RefreshToken.php | 6 ++--- src/GrantType/Specific/GithubApplication.php | 2 +- src/Signer/ClientCredentials/PostFormData.php | 2 +- src/Utils/Helper.php | 23 ++++++++++++++++ tests/BaseTestCase.php | 2 +- tests/OAuth2MiddlewareTest.php | 27 ------------------- 11 files changed, 38 insertions(+), 42 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7cd9b79..0f662b9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: php -dist: trusty +dist: bionic php: - 5.4 diff --git a/guzzle_environments/run_tests_docker.sh b/guzzle_environments/run_tests_docker.sh index afd33e0..4cbc9f0 100755 --- a/guzzle_environments/run_tests_docker.sh +++ b/guzzle_environments/run_tests_docker.sh @@ -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 \ diff --git a/src/GrantType/AuthorizationCode.php b/src/GrantType/AuthorizationCode.php index 7340cf4..e13089a 100644 --- a/src/GrantType/AuthorizationCode.php +++ b/src/GrantType/AuthorizationCode.php @@ -71,7 +71,7 @@ public function getRawData(SignerInterface $clientCredentialsSigner, $refreshTok } /** - * @return PostBody + * @return PostBody|\Psr\Http\Message\StreamInterface */ protected function getPostBody() { @@ -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(); diff --git a/src/GrantType/ClientCredentials.php b/src/GrantType/ClientCredentials.php index 27d61bf..eee2c44 100644 --- a/src/GrantType/ClientCredentials.php +++ b/src/GrantType/ClientCredentials.php @@ -73,7 +73,7 @@ public function getRawData(SignerInterface $clientCredentialsSigner, $refreshTok } /** - * @return PostBody + * @return PostBody|\Psr\Http\Message\StreamInterface */ protected function getPostBody() { @@ -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(); diff --git a/src/GrantType/PasswordCredentials.php b/src/GrantType/PasswordCredentials.php index 27b392d..f2ff336 100644 --- a/src/GrantType/PasswordCredentials.php +++ b/src/GrantType/PasswordCredentials.php @@ -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')) { @@ -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(); diff --git a/src/GrantType/RefreshToken.php b/src/GrantType/RefreshToken.php index e983798..fc2472d 100644 --- a/src/GrantType/RefreshToken.php +++ b/src/GrantType/RefreshToken.php @@ -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')) { @@ -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(); diff --git a/src/GrantType/Specific/GithubApplication.php b/src/GrantType/Specific/GithubApplication.php index af9f6d7..78714c4 100644 --- a/src/GrantType/Specific/GithubApplication.php +++ b/src/GrantType/Specific/GithubApplication.php @@ -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); } /** diff --git a/src/Signer/ClientCredentials/PostFormData.php b/src/Signer/ClientCredentials/PostFormData.php index 41c00d8..ef15db5 100644 --- a/src/Signer/ClientCredentials/PostFormData.php +++ b/src/Signer/ClientCredentials/PostFormData.php @@ -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); } diff --git a/src/Utils/Helper.php b/src/Utils/Helper.php index 0590149..fed5656 100644 --- a/src/Utils/Helper.php +++ b/src/Utils/Helper.php @@ -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) { diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index bed7990..e2c9216 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -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)); diff --git a/tests/OAuth2MiddlewareTest.php b/tests/OAuth2MiddlewareTest.php index 673bf72..65640f0 100644 --- a/tests/OAuth2MiddlewareTest.php +++ b/tests/OAuth2MiddlewareTest.php @@ -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); - } } From 0cf338e034f3bde174afdfbc53fbd86c91de5b54 Mon Sep 17 00:00:00 2001 From: Steve Kamerman Date: Wed, 14 Jul 2021 10:39:15 -0400 Subject: [PATCH 2/5] Avoid deprecations, update test dist --- .travis.yml | 2 +- guzzle_environments/run_tests_docker.sh | 2 +- src/GrantType/AuthorizationCode.php | 4 +-- src/GrantType/ClientCredentials.php | 4 +-- src/GrantType/PasswordCredentials.php | 6 ++--- src/GrantType/RefreshToken.php | 6 ++--- src/GrantType/Specific/GithubApplication.php | 2 +- src/Signer/ClientCredentials/PostFormData.php | 2 +- src/Utils/Helper.php | 23 ++++++++++++++++ tests/BaseTestCase.php | 2 +- tests/OAuth2MiddlewareTest.php | 27 ------------------- 11 files changed, 38 insertions(+), 42 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7cd9b79..0f662b9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: php -dist: trusty +dist: bionic php: - 5.4 diff --git a/guzzle_environments/run_tests_docker.sh b/guzzle_environments/run_tests_docker.sh index afd33e0..4cbc9f0 100755 --- a/guzzle_environments/run_tests_docker.sh +++ b/guzzle_environments/run_tests_docker.sh @@ -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 \ diff --git a/src/GrantType/AuthorizationCode.php b/src/GrantType/AuthorizationCode.php index 7340cf4..e13089a 100644 --- a/src/GrantType/AuthorizationCode.php +++ b/src/GrantType/AuthorizationCode.php @@ -71,7 +71,7 @@ public function getRawData(SignerInterface $clientCredentialsSigner, $refreshTok } /** - * @return PostBody + * @return PostBody|\Psr\Http\Message\StreamInterface */ protected function getPostBody() { @@ -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(); diff --git a/src/GrantType/ClientCredentials.php b/src/GrantType/ClientCredentials.php index 27d61bf..eee2c44 100644 --- a/src/GrantType/ClientCredentials.php +++ b/src/GrantType/ClientCredentials.php @@ -73,7 +73,7 @@ public function getRawData(SignerInterface $clientCredentialsSigner, $refreshTok } /** - * @return PostBody + * @return PostBody|\Psr\Http\Message\StreamInterface */ protected function getPostBody() { @@ -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(); diff --git a/src/GrantType/PasswordCredentials.php b/src/GrantType/PasswordCredentials.php index 27b392d..f2ff336 100644 --- a/src/GrantType/PasswordCredentials.php +++ b/src/GrantType/PasswordCredentials.php @@ -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')) { @@ -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(); diff --git a/src/GrantType/RefreshToken.php b/src/GrantType/RefreshToken.php index e983798..fc2472d 100644 --- a/src/GrantType/RefreshToken.php +++ b/src/GrantType/RefreshToken.php @@ -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')) { @@ -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(); diff --git a/src/GrantType/Specific/GithubApplication.php b/src/GrantType/Specific/GithubApplication.php index af9f6d7..78714c4 100644 --- a/src/GrantType/Specific/GithubApplication.php +++ b/src/GrantType/Specific/GithubApplication.php @@ -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); } /** diff --git a/src/Signer/ClientCredentials/PostFormData.php b/src/Signer/ClientCredentials/PostFormData.php index 41c00d8..ef15db5 100644 --- a/src/Signer/ClientCredentials/PostFormData.php +++ b/src/Signer/ClientCredentials/PostFormData.php @@ -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); } diff --git a/src/Utils/Helper.php b/src/Utils/Helper.php index 0590149..fed5656 100644 --- a/src/Utils/Helper.php +++ b/src/Utils/Helper.php @@ -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) { diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index bed7990..e2c9216 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -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)); diff --git a/tests/OAuth2MiddlewareTest.php b/tests/OAuth2MiddlewareTest.php index 673bf72..65640f0 100644 --- a/tests/OAuth2MiddlewareTest.php +++ b/tests/OAuth2MiddlewareTest.php @@ -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); - } } From e3cec8483475b0843b08f031b540ae7bf104dde6 Mon Sep 17 00:00:00 2001 From: Steve Kamerman Date: Wed, 14 Jul 2021 11:20:50 -0400 Subject: [PATCH 3/5] Moved to travis-ci.com --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2c668d6..594a543 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # 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) +> [![Build Status](https://travis-ci.com/kamermans/guzzle-oauth2-subscriber.svg?branch=master)](https://travis-ci.com/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. From b951db3c234ea331ee6084f315961044c1f48354 Mon Sep 17 00:00:00 2001 From: Steve Kamerman Date: Wed, 14 Jul 2021 11:32:18 -0400 Subject: [PATCH 4/5] Travis-ci isn't free and isn't working, need to move to another system... --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 594a543..8c0f58b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ # Guzzle OAuth 2.0 Subscriber -> [![Build Status](https://travis-ci.com/kamermans/guzzle-oauth2-subscriber.svg?branch=master)](https://travis-ci.com/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. From 9a1d25602bd03a3d945b8813bf9a4a96e712b43a Mon Sep 17 00:00:00 2001 From: Steve Kamerman Date: Wed, 14 Jul 2021 11:36:41 -0400 Subject: [PATCH 5/5] Updated composer description --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c534d4d..28fe02c 100644 --- a/composer.json +++ b/composer.json @@ -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": [