PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/OAuth/Consumer.php

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