/library/Zend/OAuth/Http.php

https://github.com/Exercise/zf2 · PHP · 266 lines · 119 code · 16 blank · 131 comment · 15 complexity · 5970ac23e29617167510e3f317d6c5a5 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_OAuth
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\OAuth;
  25. /**
  26. * @uses Zend\OAuth\OAuth
  27. * @uses Zend\OAuth\Exception
  28. * @uses Zend\OAuth\Http\Utility
  29. * @uses Zend\Uri\Url
  30. * @category Zend
  31. * @package Zend_OAuth
  32. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Http
  36. {
  37. /**
  38. * Array of all custom service parameters to be sent in the HTTP request
  39. * in addition to the usual OAuth parameters.
  40. *
  41. * @var array
  42. */
  43. protected $_parameters = array();
  44. /**
  45. * Reference to the Zend_OAuth_Consumer instance in use.
  46. *
  47. * @var string
  48. */
  49. protected $_consumer = null;
  50. /**
  51. * OAuth specifies three request methods, this holds the current preferred
  52. * one which by default uses the Authorization Header approach for passing
  53. * OAuth parameters, and a POST body for non-OAuth custom parameters.
  54. *
  55. * @var string
  56. */
  57. protected $_preferredRequestScheme = null;
  58. /**
  59. * Request Method for the HTTP Request.
  60. *
  61. * @var string
  62. */
  63. protected $_preferredRequestMethod = OAuth::POST;
  64. /**
  65. * Instance of the general Zend\OAuth\Http\Utility class.
  66. *
  67. * @var Zend\OAuth\Http\Utility
  68. */
  69. protected $_httpUtility = null;
  70. /**
  71. * Constructor
  72. *
  73. * @param Zend\OAuth\Consumer $consumer
  74. * @param null|array $parameters
  75. * @param null|Zend\OAuth\Http\Utility $utility
  76. * @return void
  77. */
  78. public function __construct(
  79. Consumer $consumer,
  80. array $parameters = null,
  81. Http\Utility $utility = null
  82. ) {
  83. $this->_consumer = $consumer;
  84. $this->_preferredRequestScheme = $this->_consumer->getRequestScheme();
  85. if ($parameters !== null) {
  86. $this->setParameters($parameters);
  87. }
  88. if ($utility !== null) {
  89. $this->_httpUtility = $utility;
  90. } else {
  91. $this->_httpUtility = new Http\Utility;
  92. }
  93. }
  94. /**
  95. * Set a preferred HTTP request method.
  96. *
  97. * @param string $method
  98. * @return Zend\OAuth\Http
  99. */
  100. public function setMethod($method)
  101. {
  102. if (!in_array($method, array(OAuth::POST, OAuth::GET))) {
  103. throw new Exception('invalid HTTP method: ' . $method);
  104. }
  105. $this->_preferredRequestMethod = $method;
  106. return $this;
  107. }
  108. /**
  109. * Preferred HTTP request method accessor.
  110. *
  111. * @return string
  112. */
  113. public function getMethod()
  114. {
  115. return $this->_preferredRequestMethod;
  116. }
  117. /**
  118. * Mutator to set an array of custom parameters for the HTTP request.
  119. *
  120. * @param array $customServiceParameters
  121. * @return Zend\OAuth\Http
  122. */
  123. public function setParameters(array $customServiceParameters)
  124. {
  125. $this->_parameters = $customServiceParameters;
  126. return $this;
  127. }
  128. /**
  129. * Accessor for an array of custom parameters.
  130. *
  131. * @return array
  132. */
  133. public function getParameters()
  134. {
  135. return $this->_parameters;
  136. }
  137. /**
  138. * Return the Consumer instance in use.
  139. *
  140. * @return Zend\OAuth\Consumer
  141. */
  142. public function getConsumer()
  143. {
  144. return $this->_consumer;
  145. }
  146. /**
  147. * Commence a request cycle where the current HTTP method and OAuth
  148. * request scheme set an upper preferred HTTP request style and where
  149. * failures generate a new HTTP request style further down the OAuth
  150. * preference list for OAuth Request Schemes.
  151. * On success, return the Request object that results for processing.
  152. *
  153. * @todo Remove cycling?; Replace with upfront do-or-die configuration
  154. * @param array $params
  155. * @return Zend\Http\Response
  156. * @throws Zend\OAuth\Exception on HTTP request errors
  157. */
  158. public function startRequestCycle(array $params)
  159. {
  160. $response = null;
  161. $body = null;
  162. $status = null;
  163. try {
  164. $response = $this->_attemptRequest($params);
  165. } catch (\Zend\Http\Client\Exception $e) {
  166. throw new Exception('Error in HTTP request', null, $e);
  167. }
  168. if ($response !== null) {
  169. $body = $response->getBody();
  170. $status = $response->getStatus();
  171. }
  172. if ($response === null // Request failure/exception
  173. || $status == 500 // Internal Server Error
  174. || $status == 400 // Bad Request
  175. || $status == 401 // Unauthorized
  176. || empty($body) // Missing token
  177. ) {
  178. $this->_assessRequestAttempt($response);
  179. $response = $this->startRequestCycle($params);
  180. }
  181. return $response;
  182. }
  183. /**
  184. * Return an instance of Zend_Http_Client configured to use the Query
  185. * String scheme for an OAuth driven HTTP request.
  186. *
  187. * @param array $params
  188. * @param string $url
  189. * @return Zend\Http\Client
  190. */
  191. public function getRequestSchemeQueryStringClient(array $params, $url)
  192. {
  193. $client = OAuth::getHttpClient();
  194. $client->setUri($url);
  195. $client->getUri()->setQuery(
  196. $this->_httpUtility->toEncodedQueryString($params)
  197. );
  198. $client->setMethod($this->_preferredRequestMethod);
  199. return $client;
  200. }
  201. /**
  202. * Manages the switch from OAuth request scheme to another lower preference
  203. * scheme during a request cycle.
  204. *
  205. * @param Zend\Http\Response
  206. * @return void
  207. * @throws Zend\OAuth\Exception if unable to retrieve valid token response
  208. */
  209. protected function _assessRequestAttempt(\Zend\Http\Response $response = null)
  210. {
  211. switch ($this->_preferredRequestScheme) {
  212. case OAuth::REQUEST_SCHEME_HEADER:
  213. $this->_preferredRequestScheme = OAuth\OAuth::REQUEST_SCHEME_POSTBODY;
  214. break;
  215. case OAuth::REQUEST_SCHEME_POSTBODY:
  216. $this->_preferredRequestScheme = OAuth\OAuth::REQUEST_SCHEME_QUERYSTRING;
  217. break;
  218. default:
  219. throw new Exception(
  220. 'Could not retrieve a valid Token response from Token URL:'
  221. . ($response !== null
  222. ? PHP_EOL . $response->getBody()
  223. : ' No body - check for headers')
  224. );
  225. }
  226. }
  227. /**
  228. * Generates a valid OAuth Authorization header based on the provided
  229. * parameters and realm.
  230. *
  231. * @param array $params
  232. * @param string $realm
  233. * @return string
  234. */
  235. protected function _toAuthorizationHeader(array $params, $realm = null)
  236. {
  237. $headerValue = array();
  238. $headerValue[] = 'OAuth realm="' . $realm . '"';
  239. foreach ($params as $key => $value) {
  240. if (!preg_match("/^oauth_/", $key)) {
  241. continue;
  242. }
  243. $headerValue[] = Http\Utility::urlEncode($key)
  244. . '="'
  245. . Http\Utility::urlEncode($value)
  246. . '"';
  247. }
  248. return implode(",", $headerValue);
  249. }
  250. }