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

/lib/Zend/Oauth/Consumer.php

https://bitbucket.org/acidel/buykoala
PHP | 279 lines | 122 code | 21 blank | 136 comment | 17 complexity | 512fc4c7f746d71e20e2345906fc399f 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: Consumer.php 23170 2010-10-19 18:29:24Z mabe $
  20. */
  21. /** Zend_Oauth */
  22. #require_once 'Zend/Oauth.php';
  23. /** Zend_Uri */
  24. #require_once 'Zend/Uri.php';
  25. /** Zend_Oauth_Http_RequestToken */
  26. #require_once 'Zend/Oauth/Http/RequestToken.php';
  27. /** Zend_Oauth_Http_UserAuthorization */
  28. #require_once 'Zend/Oauth/Http/UserAuthorization.php';
  29. /** Zend_Oauth_Http_AccessToken */
  30. #require_once 'Zend/Oauth/Http/AccessToken.php';
  31. /** Zend_Oauth_Token_AuthorizedRequest */
  32. #require_once 'Zend/Oauth/Token/AuthorizedRequest.php';
  33. /** Zend_Oauth_Config */
  34. #require_once 'Zend/Oauth/Config.php';
  35. /**
  36. * @category Zend
  37. * @package Zend_Oauth
  38. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. class Zend_Oauth_Consumer extends Zend_Oauth
  42. {
  43. public $switcheroo = false; // replace later when this works
  44. /**
  45. * Request Token retrieved from OAuth Provider
  46. *
  47. * @var Zend_Oauth_Token_Request
  48. */
  49. protected $_requestToken = null;
  50. /**
  51. * Access token retrieved from OAuth Provider
  52. *
  53. * @var Zend_Oauth_Token_Access
  54. */
  55. protected $_accessToken = null;
  56. /**
  57. * @var Zend_Oauth_Config
  58. */
  59. protected $_config = null;
  60. /**
  61. * Constructor; create a new object with an optional array|Zend_Config
  62. * instance containing initialising options.
  63. *
  64. * @param array|Zend_Config $options
  65. * @return void
  66. */
  67. public function __construct($options = null)
  68. {
  69. $this->_config = new Zend_Oauth_Config;
  70. if ($options !== null) {
  71. if ($options instanceof Zend_Config) {
  72. $options = $options->toArray();
  73. }
  74. $this->_config->setOptions($options);
  75. }
  76. }
  77. /**
  78. * Attempts to retrieve a Request Token from an OAuth Provider which is
  79. * later exchanged for an authorized Access Token used to access the
  80. * protected resources exposed by a web service API.
  81. *
  82. * @param null|array $customServiceParameters Non-OAuth Provider-specified parameters
  83. * @param null|string $httpMethod
  84. * @param null|Zend_Oauth_Http_RequestToken $request
  85. * @return Zend_Oauth_Token_Request
  86. */
  87. public function getRequestToken(
  88. array $customServiceParameters = null,
  89. $httpMethod = null,
  90. Zend_Oauth_Http_RequestToken $request = null
  91. ) {
  92. if ($request === null) {
  93. $request = new Zend_Oauth_Http_RequestToken($this, $customServiceParameters);
  94. } elseif($customServiceParameters !== null) {
  95. $request->setParameters($customServiceParameters);
  96. }
  97. if ($httpMethod !== null) {
  98. $request->setMethod($httpMethod);
  99. } else {
  100. $request->setMethod($this->getRequestMethod());
  101. }
  102. $this->_requestToken = $request->execute();
  103. return $this->_requestToken;
  104. }
  105. /**
  106. * After a Request Token is retrieved, the user may be redirected to the
  107. * OAuth Provider to authorize the application's access to their
  108. * protected resources - the redirect URL being provided by this method.
  109. * Once the user has authorized the application for access, they are
  110. * redirected back to the application which can now exchange the previous
  111. * Request Token for a fully authorized Access Token.
  112. *
  113. * @param null|array $customServiceParameters
  114. * @param null|Zend_Oauth_Token_Request $token
  115. * @param null|Zend_OAuth_Http_UserAuthorization $redirect
  116. * @return string
  117. */
  118. public function getRedirectUrl(
  119. array $customServiceParameters = null,
  120. Zend_Oauth_Token_Request $token = null,
  121. Zend_Oauth_Http_UserAuthorization $redirect = null
  122. ) {
  123. if ($redirect === null) {
  124. $redirect = new Zend_Oauth_Http_UserAuthorization($this, $customServiceParameters);
  125. } elseif($customServiceParameters !== null) {
  126. $redirect->setParameters($customServiceParameters);
  127. }
  128. if ($token !== null) {
  129. $this->_requestToken = $token;
  130. }
  131. return $redirect->getUrl();
  132. }
  133. /**
  134. * Rather than retrieve a redirect URL for use, e.g. from a controller,
  135. * one may perform an immediate redirect.
  136. *
  137. * Sends headers and exit()s on completion.
  138. *
  139. * @param null|array $customServiceParameters
  140. * @param null|Zend_Oauth_Token_Request $token
  141. * @param null|Zend_Oauth_Http_UserAuthorization $request
  142. * @return void
  143. */
  144. public function redirect(
  145. array $customServiceParameters = null,
  146. Zend_Oauth_Token_Request $token = null,
  147. Zend_Oauth_Http_UserAuthorization $request = null
  148. ) {
  149. if ($token instanceof Zend_Oauth_Http_UserAuthorization) {
  150. $request = $token;
  151. $token = null;
  152. }
  153. $redirectUrl = $this->getRedirectUrl($customServiceParameters, $token, $request);
  154. header('Location: ' . $redirectUrl);
  155. exit(1);
  156. }
  157. /**
  158. * Retrieve an Access Token in exchange for a previously received/authorized
  159. * Request Token.
  160. *
  161. * @param array $queryData GET data returned in user's redirect from Provider
  162. * @param Zend_Oauth_Token_Request Request Token information
  163. * @param string $httpMethod
  164. * @param Zend_Oauth_Http_AccessToken $request
  165. * @return Zend_Oauth_Token_Access
  166. * @throws Zend_Oauth_Exception on invalid authorization token, non-matching response authorization token, or unprovided authorization token
  167. */
  168. public function getAccessToken(
  169. $queryData,
  170. Zend_Oauth_Token_Request $token,
  171. $httpMethod = null,
  172. Zend_Oauth_Http_AccessToken $request = null
  173. ) {
  174. $authorizedToken = new Zend_Oauth_Token_AuthorizedRequest($queryData);
  175. if (!$authorizedToken->isValid()) {
  176. #require_once 'Zend/Oauth/Exception.php';
  177. throw new Zend_Oauth_Exception(
  178. 'Response from Service Provider is not a valid authorized request token');
  179. }
  180. if ($request === null) {
  181. $request = new Zend_Oauth_Http_AccessToken($this);
  182. }
  183. // OAuth 1.0a Verifier
  184. if ($authorizedToken->getParam('oauth_verifier') !== null) {
  185. $params = array_merge($request->getParameters(), array(
  186. 'oauth_verifier' => $authorizedToken->getParam('oauth_verifier')
  187. ));
  188. $request->setParameters($params);
  189. }
  190. if ($httpMethod !== null) {
  191. $request->setMethod($httpMethod);
  192. } else {
  193. $request->setMethod($this->getRequestMethod());
  194. }
  195. if (isset($token)) {
  196. if ($authorizedToken->getToken() !== $token->getToken()) {
  197. #require_once 'Zend/Oauth/Exception.php';
  198. throw new Zend_Oauth_Exception(
  199. 'Authorized token from Service Provider does not match'
  200. . ' supplied Request Token details'
  201. );
  202. }
  203. } else {
  204. #require_once 'Zend/Oauth/Exception.php';
  205. throw new Zend_Oauth_Exception('Request token must be passed to method');
  206. }
  207. $this->_requestToken = $token;
  208. $this->_accessToken = $request->execute();
  209. return $this->_accessToken;
  210. }
  211. /**
  212. * Return whatever the last Request Token retrieved was while using the
  213. * current Consumer instance.
  214. *
  215. * @return Zend_Oauth_Token_Request
  216. */
  217. public function getLastRequestToken()
  218. {
  219. return $this->_requestToken;
  220. }
  221. /**
  222. * Return whatever the last Access Token retrieved was while using the
  223. * current Consumer instance.
  224. *
  225. * @return Zend_Oauth_Token_Access
  226. */
  227. public function getLastAccessToken()
  228. {
  229. return $this->_accessToken;
  230. }
  231. /**
  232. * Alias to self::getLastAccessToken()
  233. *
  234. * @return Zend_Oauth_Token_Access
  235. */
  236. public function getToken()
  237. {
  238. return $this->_accessToken;
  239. }
  240. /**
  241. * Simple Proxy to the current Zend_Oauth_Config method. It's that instance
  242. * which holds all configuration methods and values this object also presents
  243. * as it's API.
  244. *
  245. * @param string $method
  246. * @param array $args
  247. * @return mixed
  248. * @throws Zend_Oauth_Exception if method does not exist in config object
  249. */
  250. public function __call($method, array $args)
  251. {
  252. if (!method_exists($this->_config, $method)) {
  253. #require_once 'Zend/Oauth/Exception.php';
  254. throw new Zend_Oauth_Exception('Method does not exist: '.$method);
  255. }
  256. return call_user_func_array(array($this->_config,$method), $args);
  257. }
  258. }