EAuth.php
6.49 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
314
315
316
<?php
/**
* EAuth 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 common\components\nodge\eauth\src;
use Yii;
use yii\base\Object;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
/**
* The EAuth class provides simple authentication via OpenID and OAuth providers.
*
* @package application.extensions.eauth
*/
class EAuth extends Object
{
/**
* @var array Authorization services and their settings.
*/
protected $services = [];
/**
* @var boolean Whether to use popup window for the authorization dialog.
*/
protected $popup = true;
/**
* @var string|bool Cache component name to use. False to disable cache.
*/
public $cache = null;
/**
* @var integer the number of seconds in which the cached value will expire. 0 means never expire.
*/
public $cacheExpire = 0;
/**
* @var string popup redirect view with custom js code
*/
protected $redirectWidget = '\\nodge\\eauth\\RedirectWidget';
/**
* @var array TokenStorage class.
*/
protected $tokenStorage = [
'class' => 'nodge\eauth\oauth\SessionTokenStorage',
];
/**
* @var array HttpClient class.
*/
protected $httpClient = [
'class' => 'nodge\eauth\oauth\HttpClient',
// 'useStreamsFallback' => false,
];
/**
* Initialize the component.
*/
public function init()
{
parent::init();
// set default cache on production environments
if (!isset($this->cache) && YII_ENV_PROD) {
$this->cache = 'cache';
}
}
/**
* @param array $services
*/
public function setServices($services)
{
$this->services = $services;
}
/**
* Returns services settings declared in the authorization classes.
* For perfomance reasons it uses cache to store settings array.
*
* @return \stdClass[] services settings.
*/
public function getServices()
{
$services = false;
if (!empty($this->cache) && Yii::$app->has($this->cache)) {
/** @var $cache \yii\caching\Cache */
$cache = Yii::$app->get($this->cache);
$services = $cache->get('EAuth.services');
}
if (false === $services || !is_array($services)) {
$services = [];
foreach ($this->services as $service => $options) {
/** @var $class ServiceBase */
$class = $this->getIdentity($service);
$services[$service] = (object)[
'id' => $class->getServiceName(),
'title' => $class->getServiceTitle(),
'type' => $class->getServiceType(),
'jsArguments' => $class->getJsArguments(),
];
}
if (isset($cache)) {
$cache->set('EAuth.services', $services, $this->cacheExpire);
}
}
return $services;
}
/**
* @param bool $usePopup
*/
public function setPopup($usePopup)
{
$this->popup = $usePopup;
}
/**
* @return bool
*/
public function getPopup()
{
return $this->popup;
}
/**
* @param string|bool $cache
*/
public function setCache($cache)
{
$this->cache = $cache;
}
/**
* @return string|bool
*/
public function getCache()
{
return $this->cache;
}
/**
* @param int $cacheExpire
*/
public function setCacheExpire($cacheExpire)
{
$this->cacheExpire = $cacheExpire;
}
/**
* @return int
*/
public function getCacheExpire()
{
return $this->cacheExpire;
}
/**
* @param string $redirectWidget
*/
public function setRedirectWidget($redirectWidget)
{
$this->redirectWidget = $redirectWidget;
}
/**
* @return string
*/
public function getRedirectWidget()
{
return $this->redirectWidget;
}
/**
* @param array $config
*/
public function setTokenStorage(array $config)
{
$this->tokenStorage = ArrayHelper::merge($this->tokenStorage, $config);
}
/**
* @return array
*/
public function getTokenStorage()
{
return $this->tokenStorage;
}
/**
* @param array $config
*/
public function setHttpClient(array $config)
{
$this->httpClient = ArrayHelper::merge($this->httpClient, $config);
}
/**
* @return array
*/
public function getHttpClient()
{
return $this->httpClient;
}
/**
* Returns the settings of the service.
*
* @param string $service the service name.
* @return \stdClass the service settings.
* @throws ErrorException
*/
protected function getService($service)
{
$service = strtolower($service);
$services = $this->getServices();
if (!isset($services[$service])) {
throw new ErrorException(Yii::t('eauth', 'Undefined service name: {service}.', ['service' => $service]), 500);
}
return $services[$service];
}
/**
* Returns the type of the service.
*
* @param string $service the service name.
* @return string the service type.
*/
public function getServiceType($service)
{
$service = $this->getService($service);
return $service->type;
}
/**
* Returns the service identity class.
*
* @param string $service the service name.
* @return IAuthService the identity class.
* @throws ErrorException
*/
public function getIdentity($service)
{
$service = strtolower($service);
if (!isset($this->services[$service])) {
throw new ErrorException(Yii::t('eauth', 'Undefined service name: {service}.', ['service' => $service]), 500);
}
$service = $this->services[$service];
$service['component'] = $this;
/** @var $identity IAuthService */
$identity = Yii::createObject($service);
return $identity;
}
/**
* Redirects to url. If the authorization dialog opened in the popup window,
* it will be closed instead of redirect. Set $jsRedirect=true if you want
* to redirect anyway.
*
* @param mixed $url url to redirect. Can be route or normal url. See {@link CHtml::normalizeUrl}.
* @param boolean $jsRedirect whether to use redirect while popup window is used. Defaults to true.
* @param array $params
*/
public function redirect($url, $jsRedirect = true, $params = [])
{
/** @var RedirectWidget $widget */
$widget = Yii::createObject([
'class' => $this->redirectWidget,
'url' => Url::to($url),
'redirect' => $jsRedirect,
'params' => $params
]);
ob_start();
$widget->run();
$output = ob_get_clean();
$response = Yii::$app->getResponse();
$response->content = $output;
$response->send();
exit();
}
/**
* Serialize the identity class.
*
* @param ServiceBase $identity the class instance.
* @return string serialized value.
*/
public function toString($identity)
{
return serialize($identity);
}
/**
* Serialize the identity class.
*
* @param string $identity serialized value.
* @return ServiceBase the class instance.
*/
public function fromString($identity)
{
return unserialize($identity);
}
}