diff --git a/.gitignore b/.gitignore index fa36fe5..7579f74 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ vendor composer.lock -.idea diff --git a/composer.json b/composer.json index 06600fb..5e7386d 100644 --- a/composer.json +++ b/composer.json @@ -18,12 +18,14 @@ } ], "require": { - "php": ">=5.4.0", - "behat/behat": "^3.0", - "guzzlehttp/guzzle": "^5.1" + "php": ">=7.0", + "behat/behat": "^3.3", + "guzzlehttp/guzzle": "^6.3", + "zendframework/zend-diactoros": "^1.4.1" }, "autoload":{ - "psr-4" : {"RstGroup\\Behat\\OAuth2\\Context\\": "src/"} + "psr-4" : { + "RstGroup\\Behat\\OAuth2\\Context\\": "src/" + } } - } diff --git a/features/password_grant_type.feature b/features/password_grant_type.feature index 29e291f..1a8029e 100644 --- a/features/password_grant_type.feature +++ b/features/password_grant_type.feature @@ -17,7 +17,7 @@ Feature: OAuth2 Token Grant Password | username | no | | password | bar | And I send a access token request - Then the response status code is 400 + Then the response status code is 401 And the response has a "error" property and it is equals "invalid_grant" And the response has a "error_description" property diff --git a/src/OAuth2Context.php b/src/OAuth2Context.php index 3538570..70c9d73 100644 --- a/src/OAuth2Context.php +++ b/src/OAuth2Context.php @@ -1,18 +1,26 @@ parameters['token_url']; $response = $this->getPostResponseFromUrl($url, $parameters); - $data = json_decode($response->getBody(true), true); + $data = json_decode((string) $response->getBody(), true); if (!isset($data['refresh_token'])) { - throw new \Exception(sprintf("Error refresh token. Response: %s", $response->getBody(true))); + throw new Exception(sprintf("Error refresh token. Response: %s", (string) $response->getBody())); } $this->refreshToken = $data['refresh_token']; } @@ -131,16 +139,16 @@ public function iMakeAAccessTokenRequest() $url = $this->parameters['token_url']; $this->response = $this->getPostResponseFromUrl($url, $this->requestBody); - $contentType = (string) $this->response->getHeader('Content-type'); + $contentType = $this->response->getHeaderLine('Content-type'); if ($contentType !== 'application/json') { - throw new \Exception(sprintf("Content-type must be application/json %s", $this->echoLastResponse())); + throw new Exception(sprintf("Content-type must be application/json %s", $this->echoLastResponse())); } - $this->data = json_decode($this->response->getBody(true)); + $this->data = json_decode((string) $this->response->getBody()); $this->lastErrorJson = json_last_error(); if ($this->lastErrorJson != JSON_ERROR_NONE) { - throw new \Exception(sprintf("Error parsing response JSON " . "\n\n %s", $this->echoLastResponse())); + throw new Exception(sprintf("Error parsing response JSON " . "\n\n %s", $this->echoLastResponse())); } } @@ -159,7 +167,7 @@ public function iMakeAAccessTokenRequestWithGivenRefreshToken() public function theResponseStatusCodeIs($httpStatus) { if ((string) $this->response->getStatusCode() !== $httpStatus) { - throw new \Exception(sprintf("HTTP code does not match %s (actual: %s)\n\n %s", $httpStatus, $this->response->getStatusCode(), $this->echoLastResponse())); + throw new Exception(sprintf("HTTP code does not match %s (actual: %s)\n\n %s", $httpStatus, $this->response->getStatusCode(), $this->echoLastResponse())); } } @@ -177,8 +185,8 @@ public function theResponseHasAProperty($propertyName) try { return $this->getPropertyValue($propertyName); - } catch (\LogicException $e) { - throw new \Exception(sprintf("Property %s is not set!\n\n %s", $propertyName, $this->echoLastResponse())); + } catch (LogicException $e) { + throw new Exception(sprintf("Property %s is not set!\n\n %s", $propertyName, $this->echoLastResponse())); } } @@ -204,7 +212,7 @@ public function theResponseHasAPropertyAndItsTypeIs($propertyName, $typeString) break; } default: - throw new \Exception(sprintf("Property %s is not of the correct type: %s!\n\n %s", $propertyName, $typeString, $this->echoLastResponse())); + throw new Exception(sprintf("Property %s is not of the correct type: %s!\n\n %s", $propertyName, $typeString, $this->echoLastResponse())); } } @@ -218,7 +226,7 @@ public function theResponseHasAPropertyAndItIsEquals($propertyName, $propertyVal if ($value == $propertyValue) { return; } - throw new \Exception(sprintf("Given %s value is not %s\n\n %s", $propertyName, $propertyValue, $this->echoLastResponse())); + throw new Exception(sprintf("Given %s value is not %s\n\n %s", $propertyName, $propertyValue, $this->echoLastResponse())); } /** @@ -226,7 +234,10 @@ public function theResponseHasAPropertyAndItIsEquals($propertyName, $propertyVal */ public function echoLastResponse() { - $this->printDebug(sprintf("Request:\n %s \n\n Response:\n %s", $this->request, $this->response)); + $request = RequestSerializer::toString($this->request); + $response = ResponseSerializer::toString($this->response); + + $this->printDebug(sprintf("Request:\n %s \n\n Response:\n %s", $request, $response)); } /** @@ -240,21 +251,21 @@ public function theResponseHasTheOAuth2Format() ]; foreach ($expectedHeaders as $name => $value) { - if ($this->response->getHeader($name) != $value) { - throw new \Exception(sprintf("Header %s is should be %s, %s given", $name, $value, $this->response->getHeader($name))); + $responseHeaderValue = $this->response->getHeaderLine($name); + if ($responseHeaderValue != $value) { + throw new Exception(sprintf("Header %s is should be %s, %s given", $name, $value, $responseHeaderValue)); } } } - /** - * Get POST response from URL without ssl verify and exception propagation - * - * @param string $url POST URL - * @param array $body body array - */ - protected function getPostResponseFromUrl($url, $body) + protected function getPostResponseFromUrl(string $url, array $body): ResponseInterface { - $this->request = $this->client->createRequest('POST', $url, ['body' => $body, 'verify' => false, 'exceptions' => false]); + $bodyAsQuery = http_build_query($body); + $headers = [ + 'Content-Type' => 'application/x-www-form-urlencoded', + ]; + $this->request = new Request('POST', $url, $headers, $bodyAsQuery); + return $this->client->send($this->request); } @@ -263,7 +274,7 @@ protected function getPostResponseFromUrl($url, $body) * * @param string $propertyName property name */ - protected function getPropertyValue($propertyName) + protected function getPropertyValue(string $propertyName) { return $this->getValue($propertyName, $this->data); } @@ -274,10 +285,10 @@ protected function getPropertyValue($propertyName) * @param string $propertyName property name * @param mixed $data data as array or object */ - protected function getValue($propertyName, $data) + protected function getValue(string $propertyName, $data) { if (empty($data)) { - throw new \Exception(sprintf("Response was not set %s", var_export($data, true))); + throw new Exception(sprintf("Response was not set %s", var_export($data, true))); } if (is_array($data) && array_key_exists($propertyName, $data)) { $data = $data[$propertyName]; @@ -287,7 +298,7 @@ protected function getValue($propertyName, $data) $data = $data->$propertyName; return $data; } - throw new \LogicException(sprintf("'%s' is not set", $propertyName)); + throw new LogicException(sprintf("'%s' is not set", $propertyName)); } /** @@ -296,7 +307,7 @@ protected function getValue($propertyName, $data) * @param string $name header name * @param string $value value for header name */ - protected function setHeader($name, $value) + protected function setHeader(string $name, string $value) { $this->headers[$name] = $value; } @@ -313,7 +324,7 @@ protected function setHeaders(array $headers) } } - protected function getGuzzleParameters() + protected function getGuzzleParameters(): array { return isset($this->parameters[self::GUZZLE_PARAMETERS]) && is_array($this->parameters[self::GUZZLE_PARAMETERS]) ? $this->parameters[self::GUZZLE_PARAMETERS] : []; } @@ -323,7 +334,7 @@ protected function getGuzzleParameters() * * @param string $string debug string */ - public function printDebug($string) + public function printDebug(string $string) { echo sprintf("\n\033[36m| %s\033[0m\n\n", strtr($string, ["\n" => "\n| "])); }