From 583077ae2c57c60b7ef79500f32f4cb9c2c1f876 Mon Sep 17 00:00:00 2001 From: SharkyKZ Date: Wed, 21 Dec 2022 11:59:28 +0200 Subject: [PATCH 1/4] Fix header checks --- Tests/ClientTest.php | 50 ++++++++++++++++++----------------------- composer.json | 3 ++- src/Client.php | 53 +++++++++++++++++++++++++++++++------------- 3 files changed, 62 insertions(+), 44 deletions(-) diff --git a/Tests/ClientTest.php b/Tests/ClientTest.php index 6c244355..504a1941 100644 --- a/Tests/ClientTest.php +++ b/Tests/ClientTest.php @@ -8,9 +8,11 @@ use Joomla\Application\WebApplicationInterface; use Joomla\Http\Http; +use Joomla\Http\Response; use Joomla\Input\Input; use Joomla\OAuth2\Client; use Joomla\Registry\Registry; +use Laminas\Diactoros\StreamFactory; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -347,13 +349,11 @@ public function testRefreshTokenJson() */ public function encodedGrantOauthCallback($url, $data, array $headers = null, $timeout = null) { - $response = new \stdClass; - - $response->code = 200; - $response->headers = ['Content-Type' => 'x-www-form-urlencoded']; - $response->body = 'access_token=accessvalue&refresh_token=refreshvalue&expires_in=3600'; - - return $response; + return new Response( + (new StreamFactory)->createStream('access_token=accessvalue&refresh_token=refreshvalue&expires_in=3600'), + 200, + ['Content-Type' => 'x-www-form-urlencoded'] + ); } /** @@ -368,13 +368,11 @@ public function encodedGrantOauthCallback($url, $data, array $headers = null, $t */ public function jsonGrantOauthCallback($url, $data, array $headers = null, $timeout = null) { - $response = new \stdClass; - - $response->code = 200; - $response->headers = ['Content-Type' => 'application/json']; - $response->body = '{"access_token":"accessvalue","refresh_token":"refreshvalue","expires_in":3600}'; - - return $response; + return new Response( + (new StreamFactory)->createStream('{"access_token":"accessvalue","refresh_token":"refreshvalue","expires_in":3600}'), + 200, + ['CONTENT-TYPE' => 'application/json'] + ); } /** @@ -389,13 +387,11 @@ public function jsonGrantOauthCallback($url, $data, array $headers = null, $time */ public function queryOauthCallback($url, $data, array $headers = null, $timeout = null) { - $response = new \stdClass; - - $response->code = 200; - $response->headers = ['Content-Type' => 'text/html']; - $response->body = 'Lorem ipsum dolor sit amet.'; - - return $response; + return new Response( + (new StreamFactory)->createStream('Lorem ipsum dolor sit amet.'), + 200, + ['Content-Type' => 'text/html'] + ); } /** @@ -409,12 +405,10 @@ public function queryOauthCallback($url, $data, array $headers = null, $timeout */ public function getOauthCallback($url, array $headers = null, $timeout = null) { - $response = new \stdClass; - - $response->code = 200; - $response->headers = ['Content-Type' => 'text/html']; - $response->body = 'Lorem ipsum dolor sit amet.'; - - return $response; + return new Response( + (new StreamFactory)->createStream('Lorem ipsum dolor sit amet.'), + 200, + ['Content-Type' => ['text/html']] + ); } } diff --git a/composer.json b/composer.json index dd8cefb0..0d524540 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ }, "require-dev": { "joomla/coding-standards": "^3.0@dev", - "phpunit/phpunit": "^8.5|^9.0" + "phpunit/phpunit": "^8.5|^9.0", + "laminas/laminas-diactoros": "^2.0" }, "autoload": { "psr-4": { diff --git a/src/Client.php b/src/Client.php index 44a8146e..7636e600 100644 --- a/src/Client.php +++ b/src/Client.php @@ -12,6 +12,7 @@ use Joomla\Http\Exception\UnexpectedResponseException; use Joomla\Http\Http; use Joomla\Http\HttpFactory; +use Joomla\Http\Response; use Joomla\Input\Input; use Joomla\Uri\Uri; @@ -101,25 +102,25 @@ public function authenticate() $response = $this->http->post($this->getOption('tokenurl'), $data); - if (!($response->code >= 200 && $response->code < 400)) + if (!($response->getStatusCode() >= 200 && $response->getStatusCode() < 400)) { throw new UnexpectedResponseException( $response, sprintf( 'Error code %s received requesting access token: %s.', - $response->code, - $response->body + $response->getStatusCode(), + (string) $response->getBody() ) ); } - if (strpos($response->headers['Content-Type'], 'application/json') !== false) + if (self::isJsonResponse($response)) { - $token = array_merge(json_decode($response->body, true), ['created' => time()]); + $token = array_merge(json_decode((string) $response->getBody(), true), ['created' => time()]); } else { - parse_str($response->body, $token); + parse_str((string) $response->getBody(), $token); $token = array_merge($token, ['created' => time()]); } @@ -274,14 +275,14 @@ public function query($url, $data = null, $headers = [], $method = 'get', $timeo throw new \InvalidArgumentException('Unknown HTTP request method: ' . $method . '.'); } - if ($response->code < 200 || $response->code >= 400) + if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 400) { throw new UnexpectedResponseException( $response, sprintf( 'Error code %s received requesting data: %s.', - $response->code, - $response->body + $response->getStatusCode(), + (string) $response->getBody() ) ); } @@ -394,25 +395,25 @@ public function refreshToken($token = null) $response = $this->http->post($this->getOption('tokenurl'), $data); - if (!($response->code >= 200 || $response->code < 400)) + if (!($response->getStatusCode() >= 200 || $response->getStatusCode() < 400)) { throw new UnexpectedResponseException( $response, sprintf( 'Error code %s received refreshing token: %s.', - $response->code, - $response->body + $response->getStatusCode(), + (string) $response->getBody() ) ); } - if (strpos($response->headers['Content-Type'], 'application/json') !== false) + if (self::isJsonResponse($response)) { - $token = array_merge(json_decode($response->body, true), ['created' => time()]); + $token = array_merge(json_decode((string) $response->getBody(), true), ['created' => time()]); } else { - parse_str($response->body, $token); + parse_str((string) $response->getBody(), $token); $token = array_merge($token, ['created' => time()]); } @@ -420,4 +421,26 @@ public function refreshToken($token = null) return $token; } + + /** + * Tests if given response contains JSON header + * + * @param Response $response The response object + * + * @return bool + * + * @since __DEPLOY_VERSION__ + */ + private static function isJsonResponse(Response $response): bool + { + foreach ($response->getHeader('Content-Type') as $value) + { + if (strpos($value, 'application/json') !== false) + { + return true; + } + } + + return false; + } } From 92515753a93060cdecfccd101833c7ee94006275 Mon Sep 17 00:00:00 2001 From: SharkyKZ Date: Thu, 22 Dec 2022 10:05:45 +0200 Subject: [PATCH 2/4] Undeclared property --- Tests/ClientTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/ClientTest.php b/Tests/ClientTest.php index 504a1941..024f928b 100644 --- a/Tests/ClientTest.php +++ b/Tests/ClientTest.php @@ -35,7 +35,7 @@ class ClientTest extends TestCase * * @var Http|MockObject */ - protected $client; + protected $http; /** * The input object to use in retrieving GET/POST data. From 4d038988cba11c14eaba6be4273624686b886015 Mon Sep 17 00:00:00 2001 From: SharkyKZ Date: Thu, 22 Dec 2022 10:28:24 +0200 Subject: [PATCH 3/4] Codestyle --- src/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index 7636e600..fb3e477e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -427,7 +427,7 @@ public function refreshToken($token = null) * * @param Response $response The response object * - * @return bool + * @return boolean * * @since __DEPLOY_VERSION__ */ From 65ca6c807aa2afb5b9b1f07562a57b3de3568404 Mon Sep 17 00:00:00 2001 From: SharkyKZ Date: Thu, 22 Dec 2022 10:55:59 +0200 Subject: [PATCH 4/4] Use native method --- src/Client.php | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/src/Client.php b/src/Client.php index fb3e477e..424cd8ce 100644 --- a/src/Client.php +++ b/src/Client.php @@ -12,7 +12,6 @@ use Joomla\Http\Exception\UnexpectedResponseException; use Joomla\Http\Http; use Joomla\Http\HttpFactory; -use Joomla\Http\Response; use Joomla\Input\Input; use Joomla\Uri\Uri; @@ -114,7 +113,7 @@ public function authenticate() ); } - if (self::isJsonResponse($response)) + if (strpos($response->getHeaderLine('Content-Type'), 'application/json') !== false) { $token = array_merge(json_decode((string) $response->getBody(), true), ['created' => time()]); } @@ -407,7 +406,7 @@ public function refreshToken($token = null) ); } - if (self::isJsonResponse($response)) + if (strpos($response->getHeaderLine('Content-Type'), 'application/json') !== false) { $token = array_merge(json_decode((string) $response->getBody(), true), ['created' => time()]); } @@ -421,26 +420,4 @@ public function refreshToken($token = null) return $token; } - - /** - * Tests if given response contains JSON header - * - * @param Response $response The response object - * - * @return boolean - * - * @since __DEPLOY_VERSION__ - */ - private static function isJsonResponse(Response $response): bool - { - foreach ($response->getHeader('Content-Type') as $value) - { - if (strpos($value, 'application/json') !== false) - { - return true; - } - } - - return false; - } }