forked from ziolek/yii-eauth-nk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EAuthServiceBase.php
490 lines (432 loc) · 12.7 KB
/
EAuthServiceBase.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<?php
/**
* EAuthServiceBase class file.
*
* @author Maxim Zemskov <[email protected]>
* @link http://github.com/Nodge/yii-eauth/
* @license http://www.opensource.org/licenses/bsd-license.php
*/
require_once 'IAuthService.php';
/**
* EAuthServiceBase is a base class for providers.
* @package application.extensions.eauth
*/
abstract class EAuthServiceBase extends CComponent implements IAuthService {
/**
* @var string the service name.
*/
protected $name;
/**
*
* @var string the service title to display in views.
*/
protected $title;
/**
* @var string the service type (e.g. OpenID, OAuth).
*/
protected $type;
/**
* @var array arguments for the jQuery.eauth() javascript function.
*/
protected $jsArguments = array();
/**
* @var array authorization attributes.
* @see getAttribute
* @see getItem
*/
protected $attributes = array();
/**
* @var boolean whether user was successfuly authenticated.
* @see getIsAuthenticated
*/
protected $authenticated = false;
/**
* @var boolean whether is attributes was fetched.
*/
private $fetched = false;
/**
* @var EAuth the {@link EAuth} application component.
*/
private $component;
/**
* @var string the redirect url after successful authorization.
*/
private $redirectUrl = '';
/**
* @var string the redirect url after unsuccessful authorization (e.g. user canceled).
*/
private $cancelUrl = '';
/**
* PHP getter magic method.
* This method is overridden so that service attributes can be accessed like properties.
* @param string $name property name.
* @return mixed property value.
* @see getAttribute
*/
public function __get($name) {
if ($this->hasAttribute($name))
return $this->getAttribute($name);
else
return parent::__get($name);
}
/**
* Checks if a attribute value is null.
* This method overrides the parent implementation by checking
* if the attribute is null or not.
* @param string $name the attribute name.
* @return boolean whether the attribute value is null.
*/
public function __isset($name) {
if ($this->hasAttribute($name))
return true;
else
return parent::__isset($name);
}
/**
* Initialize the component.
* Sets the default {@link redirectUrl} and {@link cancelUrl}.
* @param EAuth $component the component instance.
* @param array $options properties initialization.
*/
public function init($component, $options = array()) {
if (isset($component))
$this->setComponent($component);
foreach ($options as $key => $val)
$this->$key = $val;
$this->setRedirectUrl(Yii::app()->user->returnUrl);
$server = Yii::app()->request->getHostInfo();
$path = Yii::app()->request->getPathInfo();
$this->setCancelUrl($server.'/'.$path);
}
/**
* Returns service name(id).
* @return string the service name(id).
*/
public function getServiceName() {
return $this->name;
}
/**
* Returns service title.
* @return string the service title.
*/
public function getServiceTitle() {
return Yii::t('eauth', $this->title);
}
/**
* Returns service type (e.g. OpenID, OAuth).
* @return string the service type (e.g. OpenID, OAuth).
*/
public function getServiceType() {
return $this->type;
}
/**
* Returns arguments for the jQuery.eauth() javascript function.
* @return array the arguments for the jQuery.eauth() javascript function.
*/
public function getJsArguments() {
return $this->jsArguments;
}
/**
* Sets {@link EAuth} application component
* @param EAuth $component the application auth component.
*/
public function setComponent($component) {
$this->component = $component;
}
/**
* Returns the {@link EAuth} application component.
* @return EAuth the {@link EAuth} application component.
*/
public function getComponent() {
return $this->component;
}
/**
* Sets redirect url after successful authorization.
* @param string url to redirect.
*/
public function setRedirectUrl($url) {
$this->redirectUrl = $url;
}
/**
* Returns the redirect url after successful authorization.
* @return string the redirect url after successful authorization.
*/
public function getRedirectUrl() {
return $this->redirectUrl;
}
/**
* Sets redirect url after unsuccessful authorization (e.g. user canceled).
* @param string url to redirect.
*/
public function setCancelUrl($url) {
$this->cancelUrl = $url;
}
/**
* Returns the redirect url after unsuccessful authorization (e.g. user canceled).
* @return string the redirect url after unsuccessful authorization (e.g. user canceled).
*/
public function getCancelUrl() {
return $this->cancelUrl;
}
/**
* Authenticate the user.
* @return boolean whether user was successfuly authenticated.
*/
public function authenticate() {
return $this->getIsAuthenticated();
}
/**
* Whether user was successfuly authenticated.
* @return boolean whether user was successfuly authenticated.
*/
public function getIsAuthenticated() {
return $this->authenticated;
}
/**
* Redirect to the url. If url is null, {@link redirectUrl} will be used.
* @param string $url url to redirect.
*/
public function redirect($url = null) {
$this->component->redirect(isset($url) ? $url : $this->redirectUrl, true);
}
/**
* Redirect to the {@link cancelUrl} or simply close the popup window.
*/
public function cancel($url = null) {
$this->component->redirect(isset($url) ? $url : $this->cancelUrl, false);
}
/**
* Makes the curl request to the url.
* @param string $url url to request.
* @param array $options HTTP request options. Keys: query, data, referer.
* @param boolean $parseJson Whether to parse response in json format.
* @return stdClass the response.
*/
protected function makeRequest($url, $options = array(), $parseJson = true) {
$ch = $this->initRequest($url, $options);
$result = curl_exec($ch);
$headers = curl_getinfo($ch);
if (curl_errno($ch) > 0)
throw new EAuthException(curl_error($ch), curl_errno($ch));
if ($headers['http_code'] != 200) {
Yii::log(
'Invalid response http code: '.$headers['http_code'].'.'.PHP_EOL.
'URL: '.$url.PHP_EOL.
'Options: '.var_export($options, true).PHP_EOL.
'Result: '.$result,
CLogger::LEVEL_ERROR, 'application.extensions.eauth'
);
throw new EAuthException(Yii::t('eauth', 'Invalid response http code: {code}.', array('{code}' => $headers['http_code'])), $headers['http_code']);
}
curl_close($ch);
if ($parseJson)
$result = $this->parseJson($result);
return $result;
}
/**
* Initializes a new session and return a cURL handle.
* @param string $url url to request.
* @param array $options HTTP request options. Keys: query, data, referer.
* @param boolean $parseJson Whether to parse response in json format.
* @return cURL handle.
*/
protected function initRequest($url, $options = array()) {
$ch = curl_init();
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // error with open_basedir or safe mode
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
if (isset($options['referer']))
curl_setopt($ch, CURLOPT_REFERER, $options['referer']);
if (isset($options['headers']))
curl_setopt($ch, CURLOPT_HTTPHEADER, $options['headers']);
if (isset($options['query'])) {
$url_parts = parse_url($url);
if (isset($url_parts['query'])) {
$query = $url_parts['query'];
if (strlen($query) > 0)
$query .= '&';
$query .= http_build_query($options['query']);
$url = str_replace($url_parts['query'], $query, $url);
}
else {
$url_parts['query'] = $options['query'];
$new_query = http_build_query($url_parts['query']);
$url .= '?'.$new_query;
}
}
if (isset($options['data'])) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $options['data']);
}
curl_setopt($ch, CURLOPT_URL, $url);
return $ch;
}
/**
* Parse response from {@link makeRequest} in json format and check OAuth errors.
* @param string $response Json string.
* @return object result.
*/
protected function parseJson($response) {
try {
$result = json_decode($response);
$error = $this->fetchJsonError($result);
if (!isset($result)) {
throw new EAuthException(Yii::t('eauth', 'Invalid response format.', array()), 500);
}
else if (isset($error) && !empty($error['message'])) {
throw new EAuthException($error['message'], $error['code']);
}
else
return $result;
}
catch(Exception $e) {
throw new EAuthException($e->getMessage(), $e->getCode());
}
}
/**
* Returns the error info from json.
* @param stdClass $json the json response.
* @return array the error array with 2 keys: code and message. Should be null if no errors.
*/
protected function fetchJsonError($json) {
if (isset($json->error)) {
return array(
'code' => 500,
'message' => 'Unknown error occurred.',
);
}
else
return null;
}
/**
* @return string a prefix for the name of the session variables storing eauth session data.
*/
protected function getStateKeyPrefix() {
return '__eauth_'.$this->getServiceName().'__';
}
/**
* Stores a variable in eauth session.
* @param string $key variable name.
* @param mixed $value variable value.
* @param mixed $defaultValue default value. If $value===$defaultValue, the variable will be
* removed from the session.
* @see getState
*/
protected function setState($key, $value, $defaultValue = null) {
$session = Yii::app()->session;
$key = $this->getStateKeyPrefix().$key;
if($value === $defaultValue)
unset($session[$key]);
else
$session[$key] = $value;
}
/**
* Returns a value indicating whether there is a state of the specified name.
* @param string $key state name.
* @return boolean whether there is a state of the specified name.
*/
protected function hasState($key) {
$session = Yii::app()->session;
$key = $this->getStateKeyPrefix().$key;
return isset($session[$key]);
}
/**
* Returns the value of a variable that is stored in eauth session.
* @param string $key variable name.
* @param mixed $defaultValue default value.
* @return mixed the value of the variable. If it doesn't exist in the session,
* the provided default value will be returned.
* @see setState
*/
protected function getState($key, $defaultValue = null) {
$session = Yii::app()->session;
$key = $this->getStateKeyPrefix().$key;
return isset($session[$key]) ? $session[$key] : $defaultValue;
}
/**
* Fetch attributes array.
* @return boolean whether the attributes was successfully fetched.
*/
protected function fetchAttributes() {
return true;
}
/**
* Fetch attributes array.
* This function is internally used to handle fetched state.
*/
protected function _fetchAttributes() {
if (!$this->fetched) {
$this->fetched = true;
$result = $this->fetchAttributes();
if (isset($result))
$this->fetched = $result;
}
}
/**
* Returns the user unique id.
* @return mixed the user id.
*/
public function getId() {
$this->_fetchAttributes();
return $this->attributes['id'];
}
/**
* Returns the array that contains all available authorization attributes.
* @return array the attributes.
*/
public function getAttributes() {
$this->_fetchAttributes();
$attributes = array();
foreach ($this->attributes as $key => $val) {
$attributes[$key] = $this->getAttribute($key);
}
return $attributes;
}
/**
* Returns the authorization attribute value.
* @param string $key the attribute name.
* @param mixed $default the default value.
* @return mixed the attribute value.
*/
public function getAttribute($key, $default = null) {
$this->_fetchAttributes();
$getter = 'get'.$key;
if (method_exists($this, $getter))
return $this->$getter();
else
return isset($this->attributes[$key]) ? $this->attributes[$key] : $default;
}
/**
* Whether the authorization attribute exists.
* @param string $key the attribute name.
* @return boolean true if attribute exists, false otherwise.
*/
public function hasAttribute($key) {
$this->_fetchAttributes();
return isset($this->attributes[$key]);
}
/**
* Returns the object with a human-readable representation of the current authorization.
* @return stdClass the object.
*/
public function getItem() {
$item = new stdClass;
$item->title = $this->getAttribute('name');
if (empty($this->title))
$item->title = $this->getId();
if ($this->hasAttribute('url'))
$item->url = $this->getAttribute('url');
return $item;
}
/**
* Returns the array that contains all available authorization attributes.
* @return array the attributes.
* @deprecated because getAttributes is more semantic.
*/
public function getItemAttributes() {
return $this->getAttributes();
}
}