summaryrefslogtreecommitdiff
path: root/vendor/OAuth/Common/Http/Client
diff options
context:
space:
mode:
authorFrédéric Guillot <fguillot@users.noreply.github.com>2014-05-03 22:24:03 -0400
committerFrédéric Guillot <fguillot@users.noreply.github.com>2014-05-03 22:24:03 -0400
commit560a12f0bd6347a335f8ed5201d6d9562d03d4bc (patch)
tree00510d25c1cf5e747573543fa88d44ef003b1c9a /vendor/OAuth/Common/Http/Client
parent9531e439cd99fb7dbcfb039f422f1d1ba414ec30 (diff)
Add Google authentication
Diffstat (limited to 'vendor/OAuth/Common/Http/Client')
-rwxr-xr-xvendor/OAuth/Common/Http/Client/AbstractClient.php73
-rwxr-xr-xvendor/OAuth/Common/Http/Client/ClientInterface.php32
-rwxr-xr-xvendor/OAuth/Common/Http/Client/CurlClient.php142
-rwxr-xr-xvendor/OAuth/Common/Http/Client/StreamClient.php92
4 files changed, 339 insertions, 0 deletions
diff --git a/vendor/OAuth/Common/Http/Client/AbstractClient.php b/vendor/OAuth/Common/Http/Client/AbstractClient.php
new file mode 100755
index 00000000..94000b18
--- /dev/null
+++ b/vendor/OAuth/Common/Http/Client/AbstractClient.php
@@ -0,0 +1,73 @@
+<?php
+
+namespace OAuth\Common\Http\Client;
+
+/**
+ * Abstract HTTP client
+ */
+abstract class AbstractClient implements ClientInterface
+{
+ /**
+ * @var string The user agent string passed to services
+ */
+ protected $userAgent;
+
+ /**
+ * @var int The maximum number of redirects
+ */
+ protected $maxRedirects = 5;
+
+ /**
+ * @var int The maximum timeout
+ */
+ protected $timeout = 15;
+
+ /**
+ * Creates instance
+ *
+ * @param string $userAgent The UA string the client will use
+ */
+ public function __construct($userAgent = 'PHPoAuthLib')
+ {
+ $this->userAgent = $userAgent;
+ }
+
+ /**
+ * @param int $redirects Maximum redirects for client
+ *
+ * @return ClientInterface
+ */
+ public function setMaxRedirects($redirects)
+ {
+ $this->maxRedirects = $redirects;
+
+ return $this;
+ }
+
+ /**
+ * @param int $timeout Request timeout time for client in seconds
+ *
+ * @return ClientInterface
+ */
+ public function setTimeout($timeout)
+ {
+ $this->timeout = $timeout;
+
+ return $this;
+ }
+
+ /**
+ * @param array $headers
+ */
+ public function normalizeHeaders(&$headers)
+ {
+ // Normalize headers
+ array_walk(
+ $headers,
+ function (&$val, &$key) {
+ $key = ucfirst(strtolower($key));
+ $val = ucfirst(strtolower($key)) . ': ' . $val;
+ }
+ );
+ }
+}
diff --git a/vendor/OAuth/Common/Http/Client/ClientInterface.php b/vendor/OAuth/Common/Http/Client/ClientInterface.php
new file mode 100755
index 00000000..f9c20226
--- /dev/null
+++ b/vendor/OAuth/Common/Http/Client/ClientInterface.php
@@ -0,0 +1,32 @@
+<?php
+
+namespace OAuth\Common\Http\Client;
+
+use OAuth\Common\Http\Uri\UriInterface;
+use OAuth\Common\Http\Exception\TokenResponseException;
+
+/**
+ * Any HTTP clients to be used with the library should implement this interface.
+ */
+interface ClientInterface
+{
+ /**
+ * Any implementing HTTP providers should send a request to the provided endpoint with the parameters.
+ * They should return, in string form, the response body and throw an exception on error.
+ *
+ * @param UriInterface $endpoint
+ * @param mixed $requestBody
+ * @param array $extraHeaders
+ * @param string $method
+ *
+ * @return string
+ *
+ * @throws TokenResponseException
+ */
+ public function retrieveResponse(
+ UriInterface $endpoint,
+ $requestBody,
+ array $extraHeaders = array(),
+ $method = 'POST'
+ );
+}
diff --git a/vendor/OAuth/Common/Http/Client/CurlClient.php b/vendor/OAuth/Common/Http/Client/CurlClient.php
new file mode 100755
index 00000000..eae1be3e
--- /dev/null
+++ b/vendor/OAuth/Common/Http/Client/CurlClient.php
@@ -0,0 +1,142 @@
+<?php
+
+namespace OAuth\Common\Http\Client;
+
+use OAuth\Common\Http\Exception\TokenResponseException;
+use OAuth\Common\Http\Uri\UriInterface;
+
+/**
+ * Client implementation for cURL
+ */
+class CurlClient extends AbstractClient
+{
+ /**
+ * If true, explicitly sets cURL to use SSL version 3. Use this if cURL
+ * compiles with GnuTLS SSL.
+ *
+ * @var bool
+ */
+ private $forceSSL3 = false;
+
+ /**
+ * Additional parameters (as `key => value` pairs) to be passed to `curl_setopt`
+ *
+ * @var array
+ */
+ private $parameters = array();
+
+ /**
+ * Additional `curl_setopt` parameters
+ *
+ * @param array $parameters
+ */
+ public function setCurlParameters(array $parameters)
+ {
+ $this->parameters = $parameters;
+ }
+
+ /**
+ * @param bool $force
+ *
+ * @return CurlClient
+ */
+ public function setForceSSL3($force)
+ {
+ $this->forceSSL3 = $force;
+
+ return $this;
+ }
+
+ /**
+ * Any implementing HTTP providers should send a request to the provided endpoint with the parameters.
+ * They should return, in string form, the response body and throw an exception on error.
+ *
+ * @param UriInterface $endpoint
+ * @param mixed $requestBody
+ * @param array $extraHeaders
+ * @param string $method
+ *
+ * @return string
+ *
+ * @throws TokenResponseException
+ * @throws \InvalidArgumentException
+ */
+ public function retrieveResponse(
+ UriInterface $endpoint,
+ $requestBody,
+ array $extraHeaders = array(),
+ $method = 'POST'
+ ) {
+ // Normalize method name
+ $method = strtoupper($method);
+
+ $this->normalizeHeaders($extraHeaders);
+
+ if ($method === 'GET' && !empty($requestBody)) {
+ throw new \InvalidArgumentException('No body expected for "GET" request.');
+ }
+
+ if (!isset($extraHeaders['Content-Type']) && $method === 'POST' && is_array($requestBody)) {
+ $extraHeaders['Content-Type'] = 'Content-Type: application/x-www-form-urlencoded';
+ }
+
+ $extraHeaders['Host'] = 'Host: '.$endpoint->getHost();
+ $extraHeaders['Connection'] = 'Connection: close';
+
+ $ch = curl_init();
+
+ curl_setopt($ch, CURLOPT_URL, $endpoint->getAbsoluteUri());
+
+ if ($method === 'POST' || $method === 'PUT') {
+ if ($requestBody && is_array($requestBody)) {
+ $requestBody = http_build_query($requestBody, '', '&');
+ }
+
+ if ($method === 'PUT') {
+ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
+ } else {
+ curl_setopt($ch, CURLOPT_POST, true);
+ }
+
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
+ } else {
+ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
+ }
+
+ if ($this->maxRedirects > 0) {
+ curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
+ curl_setopt($ch, CURLOPT_MAXREDIRS, $this->maxRedirects);
+ }
+
+ curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($ch, CURLOPT_HEADER, false);
+ curl_setopt($ch, CURLOPT_HTTPHEADER, $extraHeaders);
+ curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
+
+ foreach ($this->parameters as $key => $value) {
+ curl_setopt($ch, $key, $value);
+ }
+
+ if ($this->forceSSL3) {
+ curl_setopt($ch, CURLOPT_SSLVERSION, 3);
+ }
+
+ $response = curl_exec($ch);
+ $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+
+ if (false === $response) {
+ $errNo = curl_errno($ch);
+ $errStr = curl_error($ch);
+ curl_close($ch);
+ if (empty($errStr)) {
+ throw new TokenResponseException('Failed to request resource.', $responseCode);
+ }
+ throw new TokenResponseException('cURL Error # '.$errNo.': '.$errStr, $responseCode);
+ }
+
+ curl_close($ch);
+
+ return $response;
+ }
+}
diff --git a/vendor/OAuth/Common/Http/Client/StreamClient.php b/vendor/OAuth/Common/Http/Client/StreamClient.php
new file mode 100755
index 00000000..7f3c5249
--- /dev/null
+++ b/vendor/OAuth/Common/Http/Client/StreamClient.php
@@ -0,0 +1,92 @@
+<?php
+
+namespace OAuth\Common\Http\Client;
+
+use OAuth\Common\Http\Exception\TokenResponseException;
+use OAuth\Common\Http\Uri\UriInterface;
+
+/**
+ * Client implementation for streams/file_get_contents
+ */
+class StreamClient extends AbstractClient
+{
+ /**
+ * Any implementing HTTP providers should send a request to the provided endpoint with the parameters.
+ * They should return, in string form, the response body and throw an exception on error.
+ *
+ * @param UriInterface $endpoint
+ * @param mixed $requestBody
+ * @param array $extraHeaders
+ * @param string $method
+ *
+ * @return string
+ *
+ * @throws TokenResponseException
+ * @throws \InvalidArgumentException
+ */
+ public function retrieveResponse(
+ UriInterface $endpoint,
+ $requestBody,
+ array $extraHeaders = array(),
+ $method = 'POST'
+ ) {
+ // Normalize method name
+ $method = strtoupper($method);
+
+ $this->normalizeHeaders($extraHeaders);
+
+ if ($method === 'GET' && !empty($requestBody)) {
+ throw new \InvalidArgumentException('No body expected for "GET" request.');
+ }
+
+ if (!isset($extraHeaders['Content-Type']) && $method === 'POST' && is_array($requestBody)) {
+ $extraHeaders['Content-Type'] = 'Content-Type: application/x-www-form-urlencoded';
+ }
+
+ $host = 'Host: '.$endpoint->getHost();
+ // Append port to Host if it has been specified
+ if ($endpoint->hasExplicitPortSpecified()) {
+ $host .= ':'.$endpoint->getPort();
+ }
+
+ $extraHeaders['Host'] = $host;
+ $extraHeaders['Connection'] = 'Connection: close';
+
+ if (is_array($requestBody)) {
+ $requestBody = http_build_query($requestBody, '', '&');
+ }
+ $extraHeaders['Content-length'] = 'Content-length: '.strlen($requestBody);
+
+ $context = $this->generateStreamContext($requestBody, $extraHeaders, $method);
+
+ $level = error_reporting(0);
+ $response = file_get_contents($endpoint->getAbsoluteUri(), false, $context);
+ error_reporting($level);
+ if (false === $response) {
+ $lastError = error_get_last();
+ if (is_null($lastError)) {
+ throw new TokenResponseException('Failed to request resource.');
+ }
+ throw new TokenResponseException($lastError['message']);
+ }
+
+ return $response;
+ }
+
+ private function generateStreamContext($body, $headers, $method)
+ {
+ return stream_context_create(
+ array(
+ 'http' => array(
+ 'method' => $method,
+ 'header' => implode("\r\n", array_values($headers)),
+ 'content' => $body,
+ 'protocol_version' => '1.1',
+ 'user_agent' => $this->userAgent,
+ 'max_redirects' => $this->maxRedirects,
+ 'timeout' => $this->timeout
+ ),
+ )
+ );
+ }
+}