File indexing completed on 2024-12-22 05:36:53
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Oauth 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 * @version $Id$ 0020 */ 0021 0022 /** Zend_Oauth */ 0023 // require_once 'Zend/Oauth.php'; 0024 0025 /** Zend_Uri */ 0026 // require_once 'Zend/Uri.php'; 0027 0028 /** Zend_Oauth_Http_RequestToken */ 0029 // require_once 'Zend/Oauth/Http/RequestToken.php'; 0030 0031 /** Zend_Oauth_Http_UserAuthorization */ 0032 // require_once 'Zend/Oauth/Http/UserAuthorization.php'; 0033 0034 /** Zend_Oauth_Http_AccessToken */ 0035 // require_once 'Zend/Oauth/Http/AccessToken.php'; 0036 0037 /** Zend_Oauth_Token_AuthorizedRequest */ 0038 // require_once 'Zend/Oauth/Token/AuthorizedRequest.php'; 0039 0040 /** Zend_Oauth_Config */ 0041 // require_once 'Zend/Oauth/Config.php'; 0042 0043 /** 0044 * @category Zend 0045 * @package Zend_Oauth 0046 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0047 * @license http://framework.zend.com/license/new-bsd New BSD License 0048 */ 0049 class Zend_Oauth_Consumer extends Zend_Oauth 0050 { 0051 public $switcheroo = false; // replace later when this works 0052 0053 /** 0054 * Request Token retrieved from OAuth Provider 0055 * 0056 * @var Zend_Oauth_Token_Request 0057 */ 0058 protected $_requestToken = null; 0059 0060 /** 0061 * Access token retrieved from OAuth Provider 0062 * 0063 * @var Zend_Oauth_Token_Access 0064 */ 0065 protected $_accessToken = null; 0066 0067 /** 0068 * @var Zend_Oauth_Config 0069 */ 0070 protected $_config = null; 0071 0072 /** 0073 * Constructor; create a new object with an optional array|Zend_Config 0074 * instance containing initialising options. 0075 * 0076 * @param array|Zend_Config $options 0077 * @return void 0078 */ 0079 public function __construct($options = null) 0080 { 0081 $this->_config = new Zend_Oauth_Config; 0082 if ($options !== null) { 0083 if ($options instanceof Zend_Config) { 0084 $options = $options->toArray(); 0085 } 0086 $this->_config->setOptions($options); 0087 } 0088 } 0089 0090 /** 0091 * Attempts to retrieve a Request Token from an OAuth Provider which is 0092 * later exchanged for an authorized Access Token used to access the 0093 * protected resources exposed by a web service API. 0094 * 0095 * @param null|array $customServiceParameters Non-OAuth Provider-specified parameters 0096 * @param null|string $httpMethod 0097 * @param null|Zend_Oauth_Http_RequestToken $request 0098 * @return Zend_Oauth_Token_Request 0099 */ 0100 public function getRequestToken( 0101 array $customServiceParameters = null, 0102 $httpMethod = null, 0103 Zend_Oauth_Http_RequestToken $request = null 0104 ) { 0105 if ($request === null) { 0106 $request = new Zend_Oauth_Http_RequestToken($this, $customServiceParameters); 0107 } elseif($customServiceParameters !== null) { 0108 $request->setParameters($customServiceParameters); 0109 } 0110 if ($httpMethod !== null) { 0111 $request->setMethod($httpMethod); 0112 } else { 0113 $request->setMethod($this->getRequestMethod()); 0114 } 0115 $this->_requestToken = $request->execute(); 0116 return $this->_requestToken; 0117 } 0118 0119 /** 0120 * After a Request Token is retrieved, the user may be redirected to the 0121 * OAuth Provider to authorize the application's access to their 0122 * protected resources - the redirect URL being provided by this method. 0123 * Once the user has authorized the application for access, they are 0124 * redirected back to the application which can now exchange the previous 0125 * Request Token for a fully authorized Access Token. 0126 * 0127 * @param null|array $customServiceParameters 0128 * @param null|Zend_Oauth_Token_Request $token 0129 * @param null|Zend_OAuth_Http_UserAuthorization $redirect 0130 * @return string 0131 */ 0132 public function getRedirectUrl( 0133 array $customServiceParameters = null, 0134 Zend_Oauth_Token_Request $token = null, 0135 Zend_Oauth_Http_UserAuthorization $redirect = null 0136 ) { 0137 if ($redirect === null) { 0138 $redirect = new Zend_Oauth_Http_UserAuthorization($this, $customServiceParameters); 0139 } elseif($customServiceParameters !== null) { 0140 $redirect->setParameters($customServiceParameters); 0141 } 0142 if ($token !== null) { 0143 $this->_requestToken = $token; 0144 } 0145 return $redirect->getUrl(); 0146 } 0147 0148 /** 0149 * Rather than retrieve a redirect URL for use, e.g. from a controller, 0150 * one may perform an immediate redirect. 0151 * 0152 * Sends headers and exit()s on completion. 0153 * 0154 * @param null|array $customServiceParameters 0155 * @param null|Zend_Oauth_Token_Request $token 0156 * @param null|Zend_Oauth_Http_UserAuthorization $request 0157 * @return void 0158 */ 0159 public function redirect( 0160 array $customServiceParameters = null, 0161 Zend_Oauth_Token_Request $token = null, 0162 Zend_Oauth_Http_UserAuthorization $request = null 0163 ) { 0164 if ($token instanceof Zend_Oauth_Http_UserAuthorization) { 0165 $request = $token; 0166 $token = null; 0167 } 0168 $redirectUrl = $this->getRedirectUrl($customServiceParameters, $token, $request); 0169 header('Location: ' . $redirectUrl); 0170 exit(1); 0171 } 0172 0173 /** 0174 * Retrieve an Access Token in exchange for a previously received/authorized 0175 * Request Token. 0176 * 0177 * @param array $queryData GET data returned in user's redirect from Provider 0178 * @param Zend_Oauth_Token_Request Request Token information 0179 * @param string $httpMethod 0180 * @param Zend_Oauth_Http_AccessToken $request 0181 * @return Zend_Oauth_Token_Access 0182 * @throws Zend_Oauth_Exception on invalid authorization token, non-matching response authorization token, or unprovided authorization token 0183 */ 0184 public function getAccessToken( 0185 $queryData, 0186 Zend_Oauth_Token_Request $token, 0187 $httpMethod = null, 0188 Zend_Oauth_Http_AccessToken $request = null 0189 ) { 0190 $authorizedToken = new Zend_Oauth_Token_AuthorizedRequest($queryData); 0191 if (!$authorizedToken->isValid()) { 0192 // require_once 'Zend/Oauth/Exception.php'; 0193 throw new Zend_Oauth_Exception( 0194 'Response from Service Provider is not a valid authorized request token'); 0195 } 0196 if ($request === null) { 0197 $request = new Zend_Oauth_Http_AccessToken($this); 0198 } 0199 0200 // OAuth 1.0a Verifier 0201 if ($authorizedToken->getParam('oauth_verifier') !== null) { 0202 $params = array_merge($request->getParameters(), array( 0203 'oauth_verifier' => $authorizedToken->getParam('oauth_verifier') 0204 )); 0205 $request->setParameters($params); 0206 } 0207 if ($httpMethod !== null) { 0208 $request->setMethod($httpMethod); 0209 } else { 0210 $request->setMethod($this->getRequestMethod()); 0211 } 0212 if (isset($token)) { 0213 if ($authorizedToken->getToken() !== $token->getToken()) { 0214 // require_once 'Zend/Oauth/Exception.php'; 0215 throw new Zend_Oauth_Exception( 0216 'Authorized token from Service Provider does not match' 0217 . ' supplied Request Token details' 0218 ); 0219 } 0220 } else { 0221 // require_once 'Zend/Oauth/Exception.php'; 0222 throw new Zend_Oauth_Exception('Request token must be passed to method'); 0223 } 0224 $this->_requestToken = $token; 0225 $this->_accessToken = $request->execute(); 0226 return $this->_accessToken; 0227 } 0228 0229 /** 0230 * Return whatever the last Request Token retrieved was while using the 0231 * current Consumer instance. 0232 * 0233 * @return Zend_Oauth_Token_Request 0234 */ 0235 public function getLastRequestToken() 0236 { 0237 return $this->_requestToken; 0238 } 0239 0240 /** 0241 * Return whatever the last Access Token retrieved was while using the 0242 * current Consumer instance. 0243 * 0244 * @return Zend_Oauth_Token_Access 0245 */ 0246 public function getLastAccessToken() 0247 { 0248 return $this->_accessToken; 0249 } 0250 0251 /** 0252 * Alias to self::getLastAccessToken() 0253 * 0254 * @return Zend_Oauth_Token_Access 0255 */ 0256 public function getToken() 0257 { 0258 return $this->_accessToken; 0259 } 0260 0261 /** 0262 * Simple Proxy to the current Zend_Oauth_Config method. It's that instance 0263 * which holds all configuration methods and values this object also presents 0264 * as it's API. 0265 * 0266 * @param string $method 0267 * @param array $args 0268 * @return mixed 0269 * @throws Zend_Oauth_Exception if method does not exist in config object 0270 */ 0271 public function __call($method, array $args) 0272 { 0273 if (!method_exists($this->_config, $method)) { 0274 // require_once 'Zend/Oauth/Exception.php'; 0275 throw new Zend_Oauth_Exception('Method does not exist: '.$method); 0276 } 0277 return call_user_func_array(array($this->_config,$method), $args); 0278 } 0279 }