]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - Zend/Oauth/Consumer.php
Release 6.5.0
[Github/sugarcrm.git] / Zend / Oauth / Consumer.php
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
20  */
21
22 /** Zend_Oauth */
23 require_once 'Zend/Oauth.php';
24
25 /** Zend_Uri */
26 require_once 'Zend/Uri.php';
27
28 /** Zend_Oauth_Http_RequestToken */
29 require_once 'Zend/Oauth/Http/RequestToken.php';
30
31 /** Zend_Oauth_Http_UserAuthorization */
32 require_once 'Zend/Oauth/Http/UserAuthorization.php';
33
34 /** Zend_Oauth_Http_AccessToken */
35 require_once 'Zend/Oauth/Http/AccessToken.php';
36
37 /** Zend_Oauth_Token_AuthorizedRequest */
38 require_once 'Zend/Oauth/Token/AuthorizedRequest.php';
39
40 /** Zend_Oauth_Config */
41 require_once 'Zend/Oauth/Config.php';
42
43 /**
44  * @category   Zend
45  * @package    Zend_Oauth
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
48  */
49 class Zend_Oauth_Consumer extends Zend_Oauth
50 {
51     public $switcheroo = false; // replace later when this works
52
53     /**
54      * Request Token retrieved from OAuth Provider
55      *
56      * @var Zend_Oauth_Token_Request
57      */
58     protected $_requestToken = null;
59
60     /**
61      * Access token retrieved from OAuth Provider
62      *
63      * @var Zend_Oauth_Token_Access
64      */
65     protected $_accessToken = null;
66
67     /**
68      * @var Zend_Oauth_Config
69      */
70     protected $_config = null;
71
72     /**
73      * Constructor; create a new object with an optional array|Zend_Config
74      * instance containing initialising options.
75      *
76      * @param  array|Zend_Config $options
77      * @return void
78      */
79     public function __construct($options = null)
80     {
81         $this->_config = new Zend_Oauth_Config;
82         if ($options !== null) {
83             if ($options instanceof Zend_Config) {
84                 $options = $options->toArray();
85             }
86             $this->_config->setOptions($options);
87         }
88     }
89
90     /**
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.
94      *
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
99      */
100     public function getRequestToken(
101         array $customServiceParameters = null,
102         $httpMethod = null,
103         Zend_Oauth_Http_RequestToken $request = null
104     ) {
105         if ($request === null) {
106             $request = new Zend_Oauth_Http_RequestToken($this, $customServiceParameters);
107         } elseif($customServiceParameters !== null) {
108             $request->setParameters($customServiceParameters);
109         }
110         if ($httpMethod !== null) {
111             $request->setMethod($httpMethod);
112         } else {
113             $request->setMethod($this->getRequestMethod());
114         }
115         $this->_requestToken = $request->execute();
116         return $this->_requestToken;
117     }
118
119     /**
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.
126      *
127      * @param  null|array $customServiceParameters
128      * @param  null|Zend_Oauth_Token_Request $token
129      * @param  null|Zend_OAuth_Http_UserAuthorization $redirect
130      * @return string
131      */
132     public function getRedirectUrl(
133         array $customServiceParameters = null,
134         Zend_Oauth_Token_Request $token = null,
135         Zend_Oauth_Http_UserAuthorization $redirect = null
136     ) {
137         if ($redirect === null) {
138             $redirect = new Zend_Oauth_Http_UserAuthorization($this, $customServiceParameters);
139         } elseif($customServiceParameters !== null) {
140             $redirect->setParameters($customServiceParameters);
141         }
142         if ($token !== null) {
143             $this->_requestToken = $token;
144         }
145         return $redirect->getUrl();
146     }
147
148     /**
149      * Rather than retrieve a redirect URL for use, e.g. from a controller,
150      * one may perform an immediate redirect.
151      *
152      * Sends headers and exit()s on completion.
153      *
154      * @param  null|array $customServiceParameters
155      * @param  null|Zend_Oauth_Http_UserAuthorization $request
156      * @return void
157      */
158     public function redirect(
159         array $customServiceParameters = null,
160         Zend_Oauth_Http_UserAuthorization $request = null
161     ) {
162         $redirectUrl = $this->getRedirectUrl($customServiceParameters, $request);
163         header('Location: ' . $redirectUrl);
164         exit(1);
165     }
166
167     /**
168      * Retrieve an Access Token in exchange for a previously received/authorized
169      * Request Token.
170      *
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
177      */
178     public function getAccessToken(
179         $queryData,
180         Zend_Oauth_Token_Request $token,
181         $httpMethod = null,
182         Zend_Oauth_Http_AccessToken $request = null
183     ) {
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');
189         }
190         if ($request === null) {
191             $request = new Zend_Oauth_Http_AccessToken($this);
192         }
193
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')
198             ));
199             $request->setParameters($params);
200         }
201         if ($httpMethod !== null) {
202             $request->setMethod($httpMethod);
203         } else {
204             $request->setMethod($this->getRequestMethod());
205         }
206         if (isset($token)) {
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'
212                 );
213             }
214         } else {
215             require_once 'Zend/Oauth/Exception.php';
216             throw new Zend_Oauth_Exception('Request token must be passed to method');
217         }
218         $this->_requestToken = $token;
219         $this->_accessToken = $request->execute();
220         return $this->_accessToken;
221     }
222
223     /**
224      * Return whatever the last Request Token retrieved was while using the
225      * current Consumer instance.
226      *
227      * @return Zend_Oauth_Token_Request
228      */
229     public function getLastRequestToken()
230     {
231         return $this->_requestToken;
232     }
233
234     /**
235      * Return whatever the last Access Token retrieved was while using the
236      * current Consumer instance.
237      *
238      * @return Zend_Oauth_Token_Access
239      */
240     public function getLastAccessToken()
241     {
242         return $this->_accessToken;
243     }
244
245     /**
246      * Alias to self::getLastAccessToken()
247      *
248      * @return Zend_Oauth_Token_Access
249      */
250     public function getToken()
251     {
252         return $this->_accessToken;
253     }
254
255     /**
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
258      * as it's API.
259      *
260      * @param  string $method
261      * @param  array $args
262      * @return mixed
263      * @throws Zend_Oauth_Exception if method does not exist in config object
264      */
265     public function __call($method, array $args)
266     {
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);
270         }
271         return call_user_func_array(array($this->_config,$method), $args);
272     }
273 }