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