PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Oauth/Http/AccessToken.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 189 lines | 90 code | 15 blank | 84 comment | 3 complexity | 986d7e25f96d5bed67914e08404bbb5f 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-2015 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. /** Zend_Oauth_Http */
  22. #require_once 'Zend/Oauth/Http.php';
  23. /** Zend_Oauth_Token_Access */
  24. #require_once 'Zend/Oauth/Token/Access.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Oauth
  28. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Oauth_Http_AccessToken extends Zend_Oauth_Http
  32. {
  33. /**
  34. * Singleton instance if required of the HTTP client
  35. *
  36. * @var Zend_Http_Client
  37. */
  38. protected $_httpClient = null;
  39. /**
  40. * Initiate a HTTP request to retrieve an Access Token.
  41. *
  42. * @return Zend_Oauth_Token_Access
  43. */
  44. public function execute()
  45. {
  46. $params = $this->assembleParams();
  47. $response = $this->startRequestCycle($params);
  48. $return = new Zend_Oauth_Token_Access($response);
  49. return $return;
  50. }
  51. /**
  52. * Assemble all parameters for an OAuth Access Token request.
  53. *
  54. * @return array
  55. */
  56. public function assembleParams()
  57. {
  58. $params = array(
  59. 'oauth_consumer_key' => $this->_consumer->getConsumerKey(),
  60. 'oauth_nonce' => $this->_httpUtility->generateNonce(),
  61. 'oauth_signature_method' => $this->_consumer->getSignatureMethod(),
  62. 'oauth_timestamp' => $this->_httpUtility->generateTimestamp(),
  63. 'oauth_token' => $this->_consumer->getLastRequestToken()->getToken(),
  64. 'oauth_version' => $this->_consumer->getVersion(),
  65. );
  66. if (!empty($this->_parameters)) {
  67. $params = array_merge($params, $this->_parameters);
  68. }
  69. $params['oauth_signature'] = $this->_httpUtility->sign(
  70. $params,
  71. $this->_consumer->getSignatureMethod(),
  72. $this->_consumer->getConsumerSecret(),
  73. $this->_consumer->getLastRequestToken()->getTokenSecret(),
  74. $this->_preferredRequestMethod,
  75. $this->_consumer->getAccessTokenUrl()
  76. );
  77. return $params;
  78. }
  79. /**
  80. * Generate and return a HTTP Client configured for the Header Request Scheme
  81. * specified by OAuth, for use in requesting an Access Token.
  82. *
  83. * @param array $params
  84. * @return Zend_Http_Client
  85. */
  86. public function getRequestSchemeHeaderClient(array $params)
  87. {
  88. $params = $this->_cleanParamsOfIllegalCustomParameters($params);
  89. $headerValue = $this->_toAuthorizationHeader($params);
  90. $client = Zend_Oauth::getHttpClient();
  91. $client->setUri($this->_consumer->getAccessTokenUrl());
  92. $client->setHeaders('Authorization', $headerValue);
  93. $client->setMethod($this->_preferredRequestMethod);
  94. return $client;
  95. }
  96. /**
  97. * Generate and return a HTTP Client configured for the POST Body Request
  98. * Scheme specified by OAuth, for use in requesting an Access Token.
  99. *
  100. * @param array $params
  101. * @return Zend_Http_Client
  102. */
  103. public function getRequestSchemePostBodyClient(array $params)
  104. {
  105. $params = $this->_cleanParamsOfIllegalCustomParameters($params);
  106. $client = Zend_Oauth::getHttpClient();
  107. $client->setUri($this->_consumer->getAccessTokenUrl());
  108. $client->setMethod($this->_preferredRequestMethod);
  109. $client->setRawData(
  110. $this->_httpUtility->toEncodedQueryString($params)
  111. );
  112. $client->setHeaders(
  113. Zend_Http_Client::CONTENT_TYPE,
  114. Zend_Http_Client::ENC_URLENCODED
  115. );
  116. return $client;
  117. }
  118. /**
  119. * Generate and return a HTTP Client configured for the Query String Request
  120. * Scheme specified by OAuth, for use in requesting an Access Token.
  121. *
  122. * @param array $params
  123. * @param string $url
  124. * @return Zend_Http_Client
  125. */
  126. public function getRequestSchemeQueryStringClient(array $params, $url)
  127. {
  128. $params = $this->_cleanParamsOfIllegalCustomParameters($params);
  129. return parent::getRequestSchemeQueryStringClient($params, $url);
  130. }
  131. /**
  132. * Attempt a request based on the current configured OAuth Request Scheme and
  133. * return the resulting HTTP Response.
  134. *
  135. * @param array $params
  136. * @return Zend_Http_Response
  137. */
  138. protected function _attemptRequest(array $params)
  139. {
  140. switch ($this->_preferredRequestScheme) {
  141. case Zend_Oauth::REQUEST_SCHEME_HEADER:
  142. $httpClient = $this->getRequestSchemeHeaderClient($params);
  143. break;
  144. case Zend_Oauth::REQUEST_SCHEME_POSTBODY:
  145. $httpClient = $this->getRequestSchemePostBodyClient($params);
  146. break;
  147. case Zend_Oauth::REQUEST_SCHEME_QUERYSTRING:
  148. $httpClient = $this->getRequestSchemeQueryStringClient($params,
  149. $this->_consumer->getAccessTokenUrl());
  150. break;
  151. }
  152. return $httpClient->request();
  153. }
  154. /**
  155. * Access Token requests specifically may not contain non-OAuth parameters.
  156. * So these should be striped out and excluded. Detection is easy since
  157. * specified OAuth parameters start with "oauth_", Extension params start
  158. * with "xouth_", and no other parameters should use these prefixes.
  159. *
  160. * xouth params are not currently allowable.
  161. *
  162. * @param array $params
  163. * @return array
  164. */
  165. protected function _cleanParamsOfIllegalCustomParameters(array $params)
  166. {
  167. foreach ($params as $key=>$value) {
  168. if (!preg_match("/^oauth_/", $key)) {
  169. unset($params[$key]);
  170. }
  171. }
  172. return $params;
  173. }
  174. }