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