From a8cd9fbb86a666829fe67ed87491244d7cdaf7ff Mon Sep 17 00:00:00 2001 From: tlshaheen Date: Wed, 9 Jul 2014 08:25:26 -0400 Subject: [PATCH 1/3] Don't throw exception if access token is in JSON If params are passed as JSON (instead of array), check and see if the access token is part of the JSON. If it is, do not throw the "You need to give parameters as array if you want to give the token within the URI" exception. --- src/OAuth2/Client.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/OAuth2/Client.php b/src/OAuth2/Client.php index 698739f..a74fdaf 100644 --- a/src/OAuth2/Client.php +++ b/src/OAuth2/Client.php @@ -320,10 +320,19 @@ public function fetch($protected_resource_url, $parameters = array(), $http_meth if (is_array($parameters)) { $parameters[$this->access_token_param_name] = $this->access_token; } else { - throw new InvalidArgumentException( - 'You need to give parameters as array if you want to give the token within the URI.', - InvalidArgumentException::REQUIRE_PARAMS_AS_ARRAY - ); + $throwexception = true; + + $testparams = json_decode($parameters, true); + if (array_key_exists($this->access_token_param_name, $testparams)) { + $throwexception = false; + } + + if ($throwexception) { + throw new InvalidArgumentException( + 'You need to give parameters as array if you want to give the token within the URI.', + InvalidArgumentException::REQUIRE_PARAMS_AS_ARRAY + ); + } } break; case self::ACCESS_TOKEN_BEARER: From 7232f413930b8c8ae7005f71f862eed33e8e40a2 Mon Sep 17 00:00:00 2001 From: tlshaheen Date: Wed, 9 Jul 2014 08:30:12 -0400 Subject: [PATCH 2/3] Formatting fix --- src/OAuth2/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OAuth2/Client.php b/src/OAuth2/Client.php index a74fdaf..8b320cd 100644 --- a/src/OAuth2/Client.php +++ b/src/OAuth2/Client.php @@ -332,7 +332,7 @@ public function fetch($protected_resource_url, $parameters = array(), $http_meth 'You need to give parameters as array if you want to give the token within the URI.', InvalidArgumentException::REQUIRE_PARAMS_AS_ARRAY ); - } + } } break; case self::ACCESS_TOKEN_BEARER: From 799bc90f38f24c0f13f634636ec8aed140cb9bb1 Mon Sep 17 00:00:00 2001 From: tlshaheen Date: Tue, 15 Jul 2014 09:05:14 -0400 Subject: [PATCH 3/3] Return response headers with fetch() This is different than issue #30 in that the http_parse_headers function here is a better implementation of parsing the headers - it accounts for a number of things that could be different about the headers. Issue #30 also doesn't appear to have been merged, unless I am looking at it wrong. --- src/OAuth2/Client.php | 66 ++++++++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 16 deletions(-) diff --git a/src/OAuth2/Client.php b/src/OAuth2/Client.php index 8b320cd..df347d1 100644 --- a/src/OAuth2/Client.php +++ b/src/OAuth2/Client.php @@ -320,19 +320,10 @@ public function fetch($protected_resource_url, $parameters = array(), $http_meth if (is_array($parameters)) { $parameters[$this->access_token_param_name] = $this->access_token; } else { - $throwexception = true; - - $testparams = json_decode($parameters, true); - if (array_key_exists($this->access_token_param_name, $testparams)) { - $throwexception = false; - } - - if ($throwexception) { - throw new InvalidArgumentException( - 'You need to give parameters as array if you want to give the token within the URI.', - InvalidArgumentException::REQUIRE_PARAMS_AS_ARRAY - ); - } + throw new InvalidArgumentException( + 'You need to give parameters as array if you want to give the token within the URI.', + InvalidArgumentException::REQUIRE_PARAMS_AS_ARRAY + ); } break; case self::ACCESS_TOKEN_BEARER: @@ -404,6 +395,7 @@ private function executeRequest($url, $parameters = array(), $http_method = self $curl_options = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => true, + CURLOPT_HEADER => true, CURLOPT_CUSTOMREQUEST => $http_method ); @@ -465,19 +457,27 @@ private function executeRequest($url, $parameters = array(), $http_method = self curl_setopt_array($ch, $this->curl_options); } $result = curl_exec($ch); + + //Split result into body and headers + list($headers, $body) = explode("\r\n\r\n", $result, 2); + + //Parse headers to return + $headers = http_parse_headers($headers); + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); if ($curl_error = curl_error($ch)) { throw new Exception($curl_error, Exception::CURL_ERROR); } else { - $json_decode = json_decode($result, true); + $json_decode = json_decode($body, true); } curl_close($ch); return array( - 'result' => (null === $json_decode) ? $result : $json_decode, + 'result' => (null === $json_decode) ? $body : $json_decode, 'code' => $http_code, - 'content_type' => $content_type + 'content_type' => $content_type, + 'headers' => $headers ); } @@ -522,3 +522,37 @@ class InvalidArgumentException extends \InvalidArgumentException const REQUIRE_PARAMS_AS_ARRAY = 0x03; const MISSING_PARAMETER = 0x04; } + + +//http://php.net/manual/en/function.http-parse-headers.php#112986 +if (!function_exists('http_parse_headers')) { + function http_parse_headers($raw_headers) { + $headers = array(); + $key = ''; + + foreach(explode("\n", $raw_headers) as $i => $h) { + $h = explode(':', $h, 2); + + if (isset($h[1])) { + if (!isset($headers[$h[0]])) + $headers[$h[0]] = trim($h[1]); + elseif (is_array($headers[$h[0]])) { + $headers[$h[0]] = array_merge($headers[$h[0]], array(trim($h[1]))); + } + else { + $headers[$h[0]] = array_merge(array($headers[$h[0]]), array(trim($h[1]))); + } + + $key = $h[0]; + } + else { + if (substr($h[0], 0, 1) == "\t") + $headers[$key] .= "\r\n\t".trim($h[0]); + elseif (!$key) + $headers[0] = trim($h[0]); + } + } + + return $headers; + } +}