ServiceBase.php
7.17 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/**
* OAuthService class file.
*
* @author Maxim Zemskov <nodge@yandex.ru>
* @link http://github.com/Nodge/yii2-eauth/
* @license http://www.opensource.org/licenses/bsd-license.php
*/
namespace nodge\eauth\oauth;
use Yii;
use OAuth\Common\Http\Uri\Uri;
use OAuth\Common\Http\Client\ClientInterface;
use OAuth\Common\Token\TokenInterface;
use OAuth\Common\Storage\TokenStorageInterface;
use nodge\eauth\EAuth;
use nodge\eauth\IAuthService;
use nodge\eauth\ErrorException;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
/**
* EOAuthService is a base class for all OAuth providers.
*
* @package application.extensions.eauth
*/
abstract class ServiceBase extends \nodge\eauth\ServiceBase implements IAuthService
{
/**
* @var string Base url for API calls.
*/
protected $baseApiUrl;
/**
* @var int Default token lifetime. Used when service wont provide expires_in param.
*/
protected $tokenDefaultLifetime = null;
/**
* @var array TokenStorage class. Null means default value from EAuth component config.
*/
protected $tokenStorage;
/**
* @var array HttpClient class. Null means default value from EAuth component config.
*/
protected $httpClient;
/**
* @var TokenStorageInterface
*/
private $_tokenStorage;
/**
* @var ClientInterface
*/
private $_httpClient;
/**
* Initialize the component.
*
* @param EAuth $component the component instance.
* @param array $options properties initialization.
*/
// public function init($component, $options = []) {
// parent::init($component, $options);
// }
/**
* For OAuth we can check existing access token.
* Useful for API calls.
*
* @return bool
* @throws ErrorException
*/
public function getIsAuthenticated()
{
if (!$this->authenticated) {
try {
$proxy = $this->getProxy();
$this->authenticated = $proxy->hasValidAccessToken();
} catch (\OAuth\Common\Exception\Exception $e) {
throw new ErrorException($e->getMessage(), $e->getCode(), 1, $e->getFile(), $e->getLine(), $e);
}
}
return parent::getIsAuthenticated();
}
/**
* @return \nodge\eauth\oauth1\ServiceProxy|\nodge\eauth\oauth2\ServiceProxy
*/
abstract protected function getProxy();
/**
* @return string the current url
*/
protected function getCallbackUrl()
{
return Url::to('', true);
}
/**
* @param array $config
*/
public function setTokenStorage(array $config)
{
$this->tokenStorage = ArrayHelper::merge($this->tokenStorage, $config);
}
/**
* @return TokenStorageInterface
*/
protected function getTokenStorage()
{
if (!isset($this->_tokenStorage)) {
$config = $this->tokenStorage;
if (!isset($config)) {
$config = $this->getComponent()->getTokenStorage();
}
$this->_tokenStorage = Yii::createObject($config);
}
return $this->_tokenStorage;
}
/**
* @param array $config
*/
public function setHttpClient(array $config)
{
$this->httpClient = ArrayHelper::merge($this->httpClient, $config);
}
/**
* @return ClientInterface
*/
protected function getHttpClient()
{
if (!isset($this->_httpClient)) {
$config = $this->httpClient;
if (!isset($config)) {
$config = $this->getComponent()->getHttpClient();
}
$this->_httpClient = Yii::createObject($config);
}
return $this->_httpClient;
}
/**
* @return int
*/
public function getTokenDefaultLifetime()
{
return $this->tokenDefaultLifetime;
}
/**
* Returns the protected resource.
*
* @param string $url url to request.
* @param array $options HTTP request options. Keys: query, data, headers.
* @param boolean $parseResponse Whether to parse response.
* @return mixed the response.
* @throws ErrorException
*/
public function makeSignedRequest($url, $options = [], $parseResponse = true)
{
if (!$this->getIsAuthenticated()) {
throw new ErrorException(Yii::t('eauth', 'Unable to complete the signed request because the user was not authenticated.'), 401);
}
if (stripos($url, 'http') !== 0) {
$url = $this->baseApiUrl . $url;
}
$url = new Uri($url);
if (isset($options['query'])) {
foreach ($options['query'] as $key => $value) {
$url->addToQuery($key, $value);
}
}
$data = isset($options['data']) ? $options['data'] : [];
$method = !empty($data) ? 'POST' : 'GET';
$headers = isset($options['headers']) ? $options['headers'] : [];
$response = $this->getProxy()->request($url, $method, $data, $headers);
if ($parseResponse) {
$response = $this->parseResponseInternal($response);
}
return $response;
}
/**
* Returns the public resource.
*
* @param string $url url to request.
* @param array $options HTTP request options. Keys: query, data, headers.
* @param boolean $parseResponse Whether to parse response.
* @return mixed the response.
*/
public function makeRequest($url, $options = [], $parseResponse = true) {
if (stripos($url, 'http') !== 0) {
$url = $this->baseApiUrl . $url;
}
$url = new Uri($url);
if (isset($options['query'])) {
foreach ($options['query'] as $key => $value) {
$url->addToQuery($key, $value);
}
}
$data = isset($options['data']) ? $options['data'] : [];
$method = !empty($data) ? 'POST' : 'GET';
$headers = isset($options['headers']) ? $options['headers'] : [];
$headers = array_merge($this->getProxy()->getExtraApiHeaders(), $headers);
$response = $this->getHttpClient()->retrieveResponse($url, $data, $headers, $method);
if ($parseResponse) {
$response = $this->parseResponseInternal($response);
}
return $response;
}
/**
* Parse response and check for errors.
*
* @param string $response
* @return mixed
* @throws ErrorException
*/
protected function parseResponseInternal($response)
{
try {
$result = $this->parseResponse($response);
if (!isset($result)) {
throw new ErrorException(Yii::t('eauth', 'Invalid response format.'), 500);
}
$error = $this->fetchResponseError($result);
if (isset($error) && !empty($error['message'])) {
throw new ErrorException($error['message'], $error['code']);
}
return $result;
} catch (\Exception $e) {
throw new ErrorException($e->getMessage(), $e->getCode());
}
}
/**
* @param string $response
* @return mixed
*/
protected function parseResponse($response)
{
return json_decode($response, true);
}
/**
* Returns the error array.
*
* @param array $response
* @return array the error array with 2 keys: code and message. Should be null if no errors.
*/
protected function fetchResponseError($response)
{
if (isset($response['error'])) {
return [
'code' => 500,
'message' => 'Unknown error occurred.',
];
}
return null;
}
/**
* @return array|null An array with valid access_token information.
*/
protected function getAccessTokenData()
{
if (!$this->getIsAuthenticated()) {
return null;
}
$token = $this->getProxy()->getAccessToken();
if (!isset($token)) {
return null;
}
return [
'access_token' => $token->getAccessToken(),
'refresh_token' => $token->getRefreshToken(),
'expires' => $token->getEndOfLife(),
'params' => $token->getExtraParams(),
];
}
/**
* @param array $data
* @return string|null
*/
public function getAccessTokenResponseError($data)
{
return isset($data['error']) ? $data['error'] : null;
}
}