PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Oauth/Consumer.php

https://github.com/Ikke/Centurion
PHP | 272 lines | 116 code | 21 blank | 135 comment | 16 complexity | c90030a98daac037b676ad934b462ce1 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. /** Zend_Oauth */
  22. //$1 'Zend/Oauth.php';
  23. /** Zend_Uri */
  24. //$1 'Zend/Uri.php';
  25. /** Zend_Oauth_Http_RequestToken */
  26. //$1 'Zend/Oauth/Http/RequestToken.php';
  27. /** Zend_Oauth_Http_UserAuthorization */
  28. //$1 'Zend/Oauth/Http/UserAuthorization.php';
  29. /** Zend_Oauth_Http_AccessToken */
  30. //$1 'Zend/Oauth/Http/AccessToken.php';
  31. /** Zend_Oauth_Token_AuthorizedRequest */
  32. //$1 'Zend/Oauth/Token/AuthorizedRequest.php';
  33. /** Zend_Oauth_Config */
  34. //$1 '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 (!is_null($options)) {
  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 (is_null($request)) {
  93. $request = new Zend_Oauth_Http_RequestToken($this, $customServiceParameters);
  94. } elseif(!is_null($customServiceParameters)) {
  95. $request->setParameters($customServiceParameters);
  96. }
  97. if (!is_null($httpMethod)) {
  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 (is_null($redirect)) {
  124. $redirect = new Zend_Oauth_Http_UserAuthorization($this, $customServiceParameters);
  125. } elseif(!is_null($customServiceParameters)) {
  126. $redirect->setParameters($customServiceParameters);
  127. }
  128. if (!is_null($token)) {
  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_Http_UserAuthorization $request
  141. * @return void
  142. */
  143. public function redirect(
  144. array $customServiceParameters = null,
  145. Zend_Oauth_Http_UserAuthorization $request = null
  146. ) {
  147. $redirectUrl = $this->getRedirectUrl($customServiceParameters, $request);
  148. header('Location: ' . $redirectUrl);
  149. exit(1);
  150. }
  151. /**
  152. * Retrieve an Access Token in exchange for a previously received/authorized
  153. * Request Token.
  154. *
  155. * @param array $queryData GET data returned in user's redirect from Provider
  156. * @param Zend_Oauth_Token_Request Request Token information
  157. * @param string $httpMethod
  158. * @param Zend_Oauth_Http_AccessToken $request
  159. * @return Zend_Oauth_Token_Access
  160. * @throws Zend_Oauth_Exception on invalid authorization token, non-matching response authorization token, or unprovided authorization token
  161. */
  162. public function getAccessToken(
  163. $queryData,
  164. Zend_Oauth_Token_Request $token,
  165. $httpMethod = null,
  166. Zend_Oauth_Http_AccessToken $request = null
  167. ) {
  168. $authorizedToken = new Zend_Oauth_Token_AuthorizedRequest($queryData);
  169. if (!$authorizedToken->isValid()) {
  170. //$1 'Zend/Oauth/Exception.php';
  171. throw new Zend_Oauth_Exception(
  172. 'Response from Service Provider is not a valid authorized request token');
  173. }
  174. if (is_null($request)) {
  175. $request = new Zend_Oauth_Http_AccessToken($this);
  176. }
  177. // OAuth 1.0a Verifier
  178. if (!is_null($authorizedToken->getParam('oauth_verifier'))) {
  179. $request->setParameters(array(
  180. 'oauth_verifier' => $authorizedToken->getParam('oauth_verifier')
  181. ));
  182. }
  183. if (!is_null($httpMethod)) {
  184. $request->setMethod($httpMethod);
  185. } else {
  186. $request->setMethod($this->getRequestMethod());
  187. }
  188. if (isset($token)) {
  189. if ($authorizedToken->getToken() !== $token->getToken()) {
  190. //$1 'Zend/Oauth/Exception.php';
  191. throw new Zend_Oauth_Exception(
  192. 'Authorized token from Service Provider does not match'
  193. . ' supplied Request Token details'
  194. );
  195. }
  196. } else {
  197. //$1 'Zend/Oauth/Exception.php';
  198. throw new Zend_Oauth_Exception('Request token must be passed to method');
  199. }
  200. $this->_requestToken = $token;
  201. $this->_accessToken = $request->execute();
  202. return $this->_accessToken;
  203. }
  204. /**
  205. * Return whatever the last Request Token retrieved was while using the
  206. * current Consumer instance.
  207. *
  208. * @return Zend_Oauth_Token_Request
  209. */
  210. public function getLastRequestToken()
  211. {
  212. return $this->_requestToken;
  213. }
  214. /**
  215. * Return whatever the last Access Token retrieved was while using the
  216. * current Consumer instance.
  217. *
  218. * @return Zend_Oauth_Token_Access
  219. */
  220. public function getLastAccessToken()
  221. {
  222. return $this->_accessToken;
  223. }
  224. /**
  225. * Alias to self::getLastAccessToken()
  226. *
  227. * @return Zend_Oauth_Token_Access
  228. */
  229. public function getToken()
  230. {
  231. return $this->_accessToken;
  232. }
  233. /**
  234. * Simple Proxy to the current Zend_Oauth_Config method. It's that instance
  235. * which holds all configuration methods and values this object also presents
  236. * as it's API.
  237. *
  238. * @param string $method
  239. * @param array $args
  240. * @return mixed
  241. * @throws Zend_Oauth_Exception if method does not exist in config object
  242. */
  243. public function __call($method, array $args)
  244. {
  245. if (!method_exists($this->_config, $method)) {
  246. //$1 'Zend/Oauth/Exception.php';
  247. throw new Zend_Oauth_Exception('Method does not exist: '.$method);
  248. }
  249. return call_user_func_array(array($this->_config,$method), $args);
  250. }
  251. }