/vendor/zend/Zend/OAuth/Consumer.php

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