1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
4
|
|
|
* @license https://flipboxfactory.com/software/saml-core/license |
5
|
|
|
* @link https://www.flipboxfactory.com/software/saml-core/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace flipbox\saml\core; |
9
|
|
|
|
10
|
|
|
use craft\base\Plugin; |
11
|
|
|
use craft\events\RegisterTemplateRootsEvent; |
12
|
|
|
use craft\events\RegisterUrlRulesEvent; |
13
|
|
|
use craft\helpers\StringHelper; |
14
|
|
|
use flipbox\saml\core\helpers\UrlHelper; |
15
|
|
|
use craft\web\twig\variables\CraftVariable; |
16
|
|
|
use craft\web\View; |
17
|
|
|
use flipbox\craft\psr3\Logger; |
18
|
|
|
use flipbox\saml\core\models\AbstractSettings; |
19
|
|
|
use flipbox\saml\core\models\SettingsInterface; |
20
|
|
|
use flipbox\saml\core\services\AbstractCp; |
21
|
|
|
use flipbox\saml\core\services\bindings\Factory; |
22
|
|
|
use flipbox\saml\core\services\Cp; |
23
|
|
|
use flipbox\saml\core\services\EditProvider; |
24
|
|
|
use flipbox\saml\core\services\messages\LogoutRequest; |
25
|
|
|
use flipbox\saml\core\services\messages\LogoutResponse; |
26
|
|
|
use flipbox\saml\core\services\Metadata; |
27
|
|
|
use flipbox\saml\core\services\ProviderIdentityServiceInterface; |
28
|
|
|
use flipbox\saml\core\services\ProviderServiceInterface; |
29
|
|
|
use SAML2\Compat\AbstractContainer; |
30
|
|
|
use yii\base\Event; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Class AbstractPlugin |
34
|
|
|
* @package flipbox\saml\core |
35
|
|
|
*/ |
36
|
|
|
abstract class AbstractPlugin extends Plugin |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
const SAML_CORE_HANDLE = 'saml-core'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
public $hasCpSettings = true; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var bool |
48
|
|
|
*/ |
49
|
|
|
public $hasCpSection = true; |
50
|
|
|
|
51
|
|
|
abstract public function loadSaml2Container(): AbstractContainer; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
abstract public function getProviderRecordClass(); |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
abstract public function getProviderIdentityRecordClass(); |
62
|
|
|
|
63
|
|
|
public function init() |
64
|
|
|
{ |
65
|
|
|
parent::init(); |
66
|
|
|
|
67
|
|
|
$this->initCore(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* |
72
|
|
|
*/ |
73
|
|
|
public function initCore() |
74
|
|
|
{ |
75
|
|
|
\Craft::setAlias('@flipbox/saml/core/web/assets', __DIR__ . '/web/assets'); |
76
|
|
|
$this->registerTemplates(); |
77
|
|
|
|
78
|
|
|
$this->setComponents([ |
79
|
|
|
'cp' => Cp::class, |
80
|
|
|
'psr3logger' => [ |
81
|
|
|
'class' => Logger::class, |
82
|
|
|
], |
83
|
|
|
'metadata' => Metadata::class, |
84
|
|
|
'logoutRequest' => LogoutRequest::class, |
85
|
|
|
'logoutResponse' => LogoutResponse::class, |
86
|
|
|
'editProvider' => [ |
87
|
|
|
'class' => EditProvider::class, |
88
|
|
|
'plugin' => $this, |
89
|
|
|
], |
90
|
|
|
]); |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set saml plugin to the craft variable |
94
|
|
|
*/ |
95
|
|
|
Event::on( |
96
|
|
|
CraftVariable::class, |
97
|
|
|
CraftVariable::EVENT_INIT, |
98
|
|
|
function (Event $event) { |
99
|
|
|
/** @var CraftVariable $variable */ |
100
|
|
|
$variable = $event->sender; |
101
|
|
|
$variable->set($this->getPluginVariableHandle(), self::getInstance()); |
102
|
|
|
$variable->set('samlCp', $this->getCp()); |
103
|
|
|
} |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
Event::on( |
107
|
|
|
View::class, |
108
|
|
|
View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, |
109
|
|
|
function (RegisterTemplateRootsEvent $e) { |
110
|
|
|
if (is_dir($baseDir = (dirname(__FILE__) . DIRECTORY_SEPARATOR . 'templates'))) { |
111
|
|
|
$e->roots[static::SAML_CORE_HANDLE] = $baseDir; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @inheritdoc |
119
|
|
|
*/ |
120
|
|
|
public function getSettingsResponse() |
121
|
|
|
{ |
122
|
|
|
|
123
|
|
|
\Craft::$app->getResponse()->redirect( |
124
|
|
|
UrlHelper::cpUrl(static::getInstance()->getHandle() . '/settings') |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
\Craft::$app->end(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
|
private function getSubNav() |
134
|
|
|
{ |
135
|
|
|
return [ |
136
|
|
|
'saml.setup' => [ |
137
|
|
|
'url' => $this->getHandle() . '/', |
138
|
|
|
'label' => \Craft::t( |
139
|
|
|
$this->getHandle(), |
140
|
|
|
'Setup' |
141
|
|
|
), |
142
|
|
|
], |
143
|
|
|
'saml.myProvider' => [ |
144
|
|
|
'url' => $this->getHandle() . '/metadata/my-provider', |
145
|
|
|
'label' => \Craft::t( |
146
|
|
|
$this->getHandle(), |
147
|
|
|
'My Provider' |
148
|
|
|
), |
149
|
|
|
], |
150
|
|
|
'saml.providers' => [ |
151
|
|
|
'url' => $this->getHandle() . '/metadata', |
152
|
|
|
'label' => \Craft::t( |
153
|
|
|
$this->getHandle(), |
154
|
|
|
'Provider List' |
155
|
|
|
), |
156
|
|
|
], |
157
|
|
|
'saml.keychain' => [ |
158
|
|
|
'url' => $this->getHandle() . '/keychain', |
159
|
|
|
'label' => \Craft::t( |
160
|
|
|
$this->getHandle(), |
161
|
|
|
'Keychain' |
162
|
|
|
), |
163
|
|
|
], |
164
|
|
|
'saml.settings' => [ |
165
|
|
|
'url' => $this->getHandle() . '/settings', |
166
|
|
|
'label' => \Craft::t( |
167
|
|
|
$this->getHandle(), |
168
|
|
|
'Settings' |
169
|
|
|
), |
170
|
|
|
], |
171
|
|
|
]; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @inheritdoc |
176
|
|
|
*/ |
177
|
|
|
public function getCpNavItem() |
178
|
|
|
{ |
179
|
|
|
return array_merge(parent::getCpNavItem(), [ |
180
|
|
|
'subnav' => $this->getSubNav(), |
181
|
|
|
]); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
public function getPluginVariableHandle() |
188
|
|
|
{ |
189
|
|
|
return StringHelper::camelCase($this->handle); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Registering the core templates for SP and IDP to use. |
194
|
|
|
*/ |
195
|
|
|
protected function registerTemplates() |
196
|
|
|
{ |
197
|
|
|
Event::on( |
198
|
|
|
View::class, |
199
|
|
|
View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, |
200
|
|
|
function (RegisterTemplateRootsEvent $e) { |
201
|
|
|
if (is_dir($baseDir = $this->getTemplateRoot())) { |
202
|
|
|
$e->roots[$this->getTemplateRootKey()] = $baseDir; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @return string |
210
|
|
|
*/ |
211
|
|
|
public function getMyType() |
212
|
|
|
{ |
213
|
|
|
return $this->getSettings()->getMyType(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
|
|
public function getRemoteType() |
220
|
|
|
{ |
221
|
|
|
$type = SettingsInterface::SP; |
222
|
|
|
if ($this->getMyType() === SettingsInterface::SP) { |
223
|
|
|
$type = SettingsInterface::IDP; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $type; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
|
|
public function getTemplateRoot() |
233
|
|
|
{ |
234
|
|
|
return dirname(__FILE__) . DIRECTORY_SEPARATOR . 'templates'; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @return string |
239
|
|
|
*/ |
240
|
|
|
public function getTemplateRootKey() |
241
|
|
|
{ |
242
|
|
|
return $this->getHandle() . '-' . 'core'; |
243
|
|
|
} |
244
|
|
|
/** |
245
|
|
|
* EVENTs |
246
|
|
|
*/ |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param RegisterUrlRulesEvent $event |
250
|
|
|
*/ |
251
|
|
|
public static function onRegisterCpUrlRules(RegisterUrlRulesEvent $event) |
252
|
|
|
{ |
253
|
|
|
$handle = static::getInstance()->getHandle(); |
254
|
|
|
$event->rules = array_merge( |
255
|
|
|
$event->rules, |
256
|
|
|
[ |
257
|
|
|
$handle . '/' => $handle . '/cp/view/general/setup', |
258
|
|
|
$handle . '/settings' => $handle . '/cp/view/general/settings', |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Keychain |
262
|
|
|
*/ |
263
|
|
|
$handle . '/keychain' => $handle . '/cp/view/keychain/index', |
264
|
|
|
$handle . '/keychain/new' => $handle . '/cp/view/keychain/edit', |
265
|
|
|
$handle . '/keychain/new-openssl' => $handle . '/cp/view/keychain/edit/openssl', |
266
|
|
|
$handle . '/keychain/<keypairId:\d+>' => $handle . '/cp/view/keychain/edit', |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Metadata |
270
|
|
|
*/ |
271
|
|
|
$handle . '/metadata' => $handle . '/cp/view/metadata/default', |
272
|
|
|
$handle . '/metadata/new' => $handle . '/cp/view/metadata/edit', |
273
|
|
|
$handle . '/metadata/new-idp' => $handle . '/cp/view/metadata/edit/new-idp', |
274
|
|
|
$handle . '/metadata/new-sp' => $handle . '/cp/view/metadata/edit/new-sp', |
275
|
|
|
$handle . '/metadata/my-provider' => $handle . '/cp/view/metadata/edit/my-provider', |
276
|
|
|
$handle . '/metadata/<providerId:\d+>' => $handle . '/cp/view/metadata/edit', |
277
|
|
|
] |
278
|
|
|
); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @param RegisterUrlRulesEvent $event |
283
|
|
|
*/ |
284
|
|
|
public static function onRegisterSiteUrlRules(RegisterUrlRulesEvent $event) |
285
|
|
|
{ |
286
|
|
|
$handle = static::getInstance()->getHandle(); |
287
|
|
|
$event->rules = array_merge( |
288
|
|
|
$event->rules, |
289
|
|
|
[ |
290
|
|
|
/** |
291
|
|
|
* LOGIN |
292
|
|
|
*/ |
293
|
|
|
sprintf( |
294
|
|
|
'POST,GET %s', |
295
|
|
|
UrlHelper::buildEndpointPath( |
296
|
|
|
static::getInstance()->getSettings(), |
297
|
|
|
UrlHelper::LOGIN_ENDPOINT |
298
|
|
|
) |
299
|
|
|
) => $handle . '/login', |
300
|
|
|
sprintf( |
301
|
|
|
'POST,GET %s/<uid:[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}>', |
302
|
|
|
UrlHelper::buildEndpointPath( |
303
|
|
|
static::getInstance()->getSettings(), |
304
|
|
|
UrlHelper::LOGIN_ENDPOINT |
305
|
|
|
) |
306
|
|
|
) => $handle . '/login', |
307
|
|
|
sprintf( |
308
|
|
|
'POST,GET %s', |
309
|
|
|
UrlHelper::buildEndpointPath( |
310
|
|
|
static::getInstance()->getSettings(), |
311
|
|
|
UrlHelper::LOGIN_REQUEST_ENDPOINT |
312
|
|
|
) |
313
|
|
|
) => $handle . '/login/request', |
314
|
|
|
sprintf( |
315
|
|
|
'POST,GET %s/<uid:[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}>', |
316
|
|
|
UrlHelper::buildEndpointPath( |
317
|
|
|
static::getInstance()->getSettings(), |
318
|
|
|
UrlHelper::LOGIN_REQUEST_ENDPOINT |
319
|
|
|
) |
320
|
|
|
) => $handle . '/login/request', |
321
|
|
|
/** |
322
|
|
|
* LOGOUT |
323
|
|
|
*/ |
324
|
|
|
sprintf( |
325
|
|
|
'POST,GET %s/<uid:[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}>', |
326
|
|
|
UrlHelper::buildEndpointPath( |
327
|
|
|
static::getInstance()->getSettings(), |
328
|
|
|
UrlHelper::LOGOUT_ENDPOINT |
329
|
|
|
) |
330
|
|
|
) => $handle . '/logout', |
331
|
|
|
sprintf( |
332
|
|
|
'POST,GET %s', |
333
|
|
|
UrlHelper::buildEndpointPath( |
334
|
|
|
static::getInstance()->getSettings(), |
335
|
|
|
UrlHelper::LOGOUT_ENDPOINT |
336
|
|
|
) |
337
|
|
|
) => $handle . '/logout', |
338
|
|
|
sprintf( |
339
|
|
|
'POST,GET %s', |
340
|
|
|
UrlHelper::buildEndpointPath( |
341
|
|
|
static::getInstance()->getSettings(), |
342
|
|
|
UrlHelper::LOGOUT_REQUEST_ENDPOINT |
343
|
|
|
) |
344
|
|
|
) => $handle . '/logout/request', |
345
|
|
|
sprintf( |
346
|
|
|
'POST,GET %s/<uid:[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}>', |
347
|
|
|
UrlHelper::buildEndpointPath( |
348
|
|
|
static::getInstance()->getSettings(), |
349
|
|
|
UrlHelper::LOGOUT_REQUEST_ENDPOINT |
350
|
|
|
) |
351
|
|
|
) => $handle . '/logout/request', |
352
|
|
|
|
353
|
|
|
] |
354
|
|
|
); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* @return AbstractSettings |
359
|
|
|
*/ |
360
|
|
|
public function getSettings() |
361
|
|
|
{ |
362
|
|
|
return parent::getSettings(); |
|
|
|
|
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Components |
367
|
|
|
*/ |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
371
|
|
|
* @returns AbstractCp |
372
|
|
|
*/ |
373
|
|
|
public function getCp() |
374
|
|
|
{ |
375
|
|
|
|
376
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
377
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
378
|
|
|
return $this->get('cp'); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* @returns EditProvider |
383
|
|
|
*/ |
384
|
|
|
public function getEditProvider() |
385
|
|
|
{ |
386
|
|
|
|
387
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
388
|
|
|
return $this->get('editProvider'); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* @returns ProviderServiceInterface |
393
|
|
|
*/ |
394
|
|
|
public function getProvider() |
395
|
|
|
{ |
396
|
|
|
|
397
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
398
|
|
|
return $this->get('provider'); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* @returns ProviderIdentityServiceInterface |
403
|
|
|
*/ |
404
|
|
|
public function getProviderIdentity() |
405
|
|
|
{ |
406
|
|
|
return $this->get('providerIdentity'); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
411
|
|
|
* @return Metadata |
412
|
|
|
*/ |
413
|
|
|
public function getMetadata() |
414
|
|
|
{ |
415
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
416
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
417
|
|
|
return $this->get('metadata'); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
422
|
|
|
* @return LogoutRequest |
423
|
|
|
* @throws \yii\base\InvalidConfigException |
424
|
|
|
*/ |
425
|
|
|
public function getLogoutRequest() |
426
|
|
|
{ |
427
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
428
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
429
|
|
|
return $this->get('logoutRequest'); |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
434
|
|
|
* @return LogoutResponse |
435
|
|
|
* @throws \yii\base\InvalidConfigException |
436
|
|
|
*/ |
437
|
|
|
public function getLogoutResponse() |
438
|
|
|
{ |
439
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
440
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
441
|
|
|
return $this->get('logoutResponse'); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* Bindings |
446
|
|
|
*/ |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* @return Factory |
450
|
|
|
* @throws \yii\base\InvalidConfigException |
451
|
|
|
*/ |
452
|
|
|
public function getBindingFactory() |
453
|
|
|
{ |
454
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
455
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
456
|
|
|
return $this->get('bindingFactory'); |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* @return Logger |
461
|
|
|
* @throws \yii\base\InvalidConfigException |
462
|
|
|
*/ |
463
|
|
|
public function getPsr3Logger() |
464
|
|
|
{ |
465
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
466
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
467
|
|
|
return $this->get('psr3logger'); |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* Log Functions |
473
|
|
|
*/ |
474
|
|
|
|
475
|
|
|
/** |
476
|
|
|
* @param $message |
477
|
|
|
*/ |
478
|
|
|
public static function error($message) |
479
|
|
|
{ |
480
|
|
|
\Craft::error($message, static::getInstance()->getHandle()); |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* @param $message |
485
|
|
|
*/ |
486
|
|
|
public static function warning($message) |
487
|
|
|
{ |
488
|
|
|
\Craft::warning($message, static::getInstance()->getHandle()); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* @param $message |
493
|
|
|
*/ |
494
|
|
|
public static function info($message) |
495
|
|
|
{ |
496
|
|
|
\Craft::info($message, static::getInstance()->getHandle()); |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
/** |
500
|
|
|
* @param $message |
501
|
|
|
*/ |
502
|
|
|
public static function debug($message) |
503
|
|
|
{ |
504
|
|
|
\Craft::debug($message, static::getInstance()->getHandle()); |
505
|
|
|
} |
506
|
|
|
} |
507
|
|
|
|