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.
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
23 require_once 'Zend/Oauth.php';
26 require_once 'Zend/Uri.php';
28 /** Zend_Oauth_Http_RequestToken */
29 require_once 'Zend/Oauth/Http/RequestToken.php';
31 /** Zend_Oauth_Http_UserAuthorization */
32 require_once 'Zend/Oauth/Http/UserAuthorization.php';
34 /** Zend_Oauth_Http_AccessToken */
35 require_once 'Zend/Oauth/Http/AccessToken.php';
37 /** Zend_Oauth_Token_AuthorizedRequest */
38 require_once 'Zend/Oauth/Token/AuthorizedRequest.php';
40 /** Zend_Oauth_Config */
41 require_once 'Zend/Oauth/Config.php';
46 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
47 * @license http://framework.zend.com/license/new-bsd New BSD License
49 class Zend_Oauth_Consumer extends Zend_Oauth
51 public $switcheroo = false; // replace later when this works
54 * Request Token retrieved from OAuth Provider
56 * @var Zend_Oauth_Token_Request
58 protected $_requestToken = null;
61 * Access token retrieved from OAuth Provider
63 * @var Zend_Oauth_Token_Access
65 protected $_accessToken = null;
68 * @var Zend_Oauth_Config
70 protected $_config = null;
73 * Constructor; create a new object with an optional array|Zend_Config
74 * instance containing initialising options.
76 * @param array|Zend_Config $options
79 public function __construct($options = null)
81 $this->_config = new Zend_Oauth_Config;
82 if ($options !== null) {
83 if ($options instanceof Zend_Config) {
84 $options = $options->toArray();
86 $this->_config->setOptions($options);
91 * Attempts to retrieve a Request Token from an OAuth Provider which is
92 * later exchanged for an authorized Access Token used to access the
93 * protected resources exposed by a web service API.
95 * @param null|array $customServiceParameters Non-OAuth Provider-specified parameters
96 * @param null|string $httpMethod
97 * @param null|Zend_Oauth_Http_RequestToken $request
98 * @return Zend_Oauth_Token_Request
100 public function getRequestToken(
101 array $customServiceParameters = null,
103 Zend_Oauth_Http_RequestToken $request = null
105 if ($request === null) {
106 $request = new Zend_Oauth_Http_RequestToken($this, $customServiceParameters);
107 } elseif($customServiceParameters !== null) {
108 $request->setParameters($customServiceParameters);
110 if ($httpMethod !== null) {
111 $request->setMethod($httpMethod);
113 $request->setMethod($this->getRequestMethod());
115 $this->_requestToken = $request->execute();
116 return $this->_requestToken;
120 * After a Request Token is retrieved, the user may be redirected to the
121 * OAuth Provider to authorize the application's access to their
122 * protected resources - the redirect URL being provided by this method.
123 * Once the user has authorized the application for access, they are
124 * redirected back to the application which can now exchange the previous
125 * Request Token for a fully authorized Access Token.
127 * @param null|array $customServiceParameters
128 * @param null|Zend_Oauth_Token_Request $token
129 * @param null|Zend_OAuth_Http_UserAuthorization $redirect
132 public function getRedirectUrl(
133 array $customServiceParameters = null,
134 Zend_Oauth_Token_Request $token = null,
135 Zend_Oauth_Http_UserAuthorization $redirect = null
137 if ($redirect === null) {
138 $redirect = new Zend_Oauth_Http_UserAuthorization($this, $customServiceParameters);
139 } elseif($customServiceParameters !== null) {
140 $redirect->setParameters($customServiceParameters);
142 if ($token !== null) {
143 $this->_requestToken = $token;
145 return $redirect->getUrl();
149 * Rather than retrieve a redirect URL for use, e.g. from a controller,
150 * one may perform an immediate redirect.
152 * Sends headers and exit()s on completion.
154 * @param null|array $customServiceParameters
155 * @param null|Zend_Oauth_Http_UserAuthorization $request
158 public function redirect(
159 array $customServiceParameters = null,
160 Zend_Oauth_Http_UserAuthorization $request = null
162 $redirectUrl = $this->getRedirectUrl($customServiceParameters, $request);
163 header('Location: ' . $redirectUrl);
168 * Retrieve an Access Token in exchange for a previously received/authorized
171 * @param array $queryData GET data returned in user's redirect from Provider
172 * @param Zend_Oauth_Token_Request Request Token information
173 * @param string $httpMethod
174 * @param Zend_Oauth_Http_AccessToken $request
175 * @return Zend_Oauth_Token_Access
176 * @throws Zend_Oauth_Exception on invalid authorization token, non-matching response authorization token, or unprovided authorization token
178 public function getAccessToken(
180 Zend_Oauth_Token_Request $token,
182 Zend_Oauth_Http_AccessToken $request = null
184 $authorizedToken = new Zend_Oauth_Token_AuthorizedRequest($queryData);
185 if (!$authorizedToken->isValid()) {
186 require_once 'Zend/Oauth/Exception.php';
187 throw new Zend_Oauth_Exception(
188 'Response from Service Provider is not a valid authorized request token');
190 if ($request === null) {
191 $request = new Zend_Oauth_Http_AccessToken($this);
194 // OAuth 1.0a Verifier
195 if (!is_null($authorizedToken->getParam('oauth_verifier'))) {
196 $params = array_merge($request->getParameters(), array(
197 'oauth_verifier' => $authorizedToken->getParam('oauth_verifier')
199 $request->setParameters($params);
201 if ($httpMethod !== null) {
202 $request->setMethod($httpMethod);
204 $request->setMethod($this->getRequestMethod());
207 if ($authorizedToken->getToken() !== $token->getToken()) {
208 require_once 'Zend/Oauth/Exception.php';
209 throw new Zend_Oauth_Exception(
210 'Authorized token from Service Provider does not match'
211 . ' supplied Request Token details'
215 require_once 'Zend/Oauth/Exception.php';
216 throw new Zend_Oauth_Exception('Request token must be passed to method');
218 $this->_requestToken = $token;
219 $this->_accessToken = $request->execute();
220 return $this->_accessToken;
224 * Return whatever the last Request Token retrieved was while using the
225 * current Consumer instance.
227 * @return Zend_Oauth_Token_Request
229 public function getLastRequestToken()
231 return $this->_requestToken;
235 * Return whatever the last Access Token retrieved was while using the
236 * current Consumer instance.
238 * @return Zend_Oauth_Token_Access
240 public function getLastAccessToken()
242 return $this->_accessToken;
246 * Alias to self::getLastAccessToken()
248 * @return Zend_Oauth_Token_Access
250 public function getToken()
252 return $this->_accessToken;
256 * Simple Proxy to the current Zend_Oauth_Config method. It's that instance
257 * which holds all configuration methods and values this object also presents
260 * @param string $method
263 * @throws Zend_Oauth_Exception if method does not exist in config object
265 public function __call($method, array $args)
267 if (!method_exists($this->_config, $method)) {
268 require_once 'Zend/Oauth/Exception.php';
269 throw new Zend_Oauth_Exception('Method does not exist: '.$method);
271 return call_user_func_array(array($this->_config,$method), $args);