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\helpers\StringHelper; |
13
|
|
|
use craft\web\twig\variables\CraftVariable; |
14
|
|
|
use craft\web\View; |
15
|
|
|
use flipbox\saml\core\services\AbstractCp; |
16
|
|
|
use flipbox\saml\core\services\bindings\AbstractHttpPost; |
17
|
|
|
use flipbox\saml\core\services\bindings\AbstractHttpRedirect; |
18
|
|
|
use flipbox\saml\core\services\messages\MetadataServiceInterface; |
19
|
|
|
use flipbox\saml\core\services\messages\SamlRequestInterface; |
20
|
|
|
use flipbox\saml\core\services\messages\SamlResponseInterface; |
21
|
|
|
use flipbox\saml\core\services\ProviderIdentityServiceInterface; |
22
|
|
|
use flipbox\saml\core\services\ProviderServiceInterface; |
23
|
|
|
use yii\base\Event; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class AbstractPlugin |
27
|
|
|
* @package flipbox\saml\core |
28
|
|
|
*/ |
29
|
|
|
abstract class AbstractPlugin extends Plugin |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Saml Constants |
34
|
|
|
*/ |
35
|
|
|
const SP = 'sp'; |
36
|
|
|
const IDP = 'idp'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var bool |
40
|
|
|
*/ |
41
|
|
|
public $hasCpSettings = true; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var bool |
45
|
|
|
*/ |
46
|
|
|
public $hasCpSection = true; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
abstract public function getMyType(); |
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
|
|
|
/** |
79
|
|
|
* Set saml plugin to the craft variable |
80
|
|
|
*/ |
81
|
|
|
Event::on( |
82
|
|
|
CraftVariable::class, |
83
|
|
|
CraftVariable::EVENT_INIT, |
84
|
|
|
function (Event $event) { |
85
|
|
|
/** @var CraftVariable $variable */ |
86
|
|
|
$variable = $event->sender; |
87
|
|
|
$variable->set($this->getPluginVariableHandle(), self::getInstance()); |
88
|
|
|
} |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
private function getSubNav() |
96
|
|
|
{ |
97
|
|
|
return [ |
98
|
|
|
'saml.setup' => [ |
99
|
|
|
'url' => $this->getHandle() . '/', |
100
|
|
|
'label' => \Craft::t( |
101
|
|
|
$this->getHandle(), |
102
|
|
|
'Setup' |
103
|
|
|
), |
104
|
|
|
], |
105
|
|
|
'saml.myProvider' => [ |
106
|
|
|
'url' => $this->getHandle() . '/metadata/my-provider', |
107
|
|
|
'label' => \Craft::t( |
108
|
|
|
$this->getHandle(), |
109
|
|
|
'My Provider' |
110
|
|
|
), |
111
|
|
|
], |
112
|
|
|
'saml.providers' => [ |
113
|
|
|
'url' => $this->getHandle() . '/metadata', |
114
|
|
|
'label' => \Craft::t( |
115
|
|
|
$this->getHandle(), |
116
|
|
|
'Provider List' |
117
|
|
|
), |
118
|
|
|
], |
119
|
|
|
'saml.keychain' => [ |
120
|
|
|
'url' => $this->getHandle() . '/keychain', |
121
|
|
|
'label' => \Craft::t( |
122
|
|
|
$this->getHandle(), |
123
|
|
|
'Keychain' |
124
|
|
|
), |
125
|
|
|
], |
126
|
|
|
'saml.settings' => [ |
127
|
|
|
'url' => $this->getHandle() . '/settings', |
128
|
|
|
'label' => \Craft::t( |
129
|
|
|
$this->getHandle(), |
130
|
|
|
'Settings' |
131
|
|
|
), |
132
|
|
|
], |
133
|
|
|
]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @inheritdoc |
138
|
|
|
*/ |
139
|
|
|
public function getCpNavItem() |
140
|
|
|
{ |
141
|
|
|
return array_merge(parent::getCpNavItem(), [ |
142
|
|
|
'subnav' => $this->getSubNav() |
143
|
|
|
]); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public function getPluginVariableHandle() |
150
|
|
|
{ |
151
|
|
|
return StringHelper::camelCase($this->handle); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Registering the core templates for SP and IDP to use. |
156
|
|
|
*/ |
157
|
|
|
protected function registerTemplates() |
158
|
|
|
{ |
159
|
|
|
Event::on( |
160
|
|
|
View::class, |
161
|
|
|
View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, |
162
|
|
|
function (RegisterTemplateRootsEvent $e) { |
163
|
|
|
if (is_dir($baseDir = $this->getTemplateRoot())) { |
164
|
|
|
$e->roots[$this->getTemplateRootKey()] = $baseDir; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
|
|
public function getRemoteType() |
174
|
|
|
{ |
175
|
|
|
$type = static::SP; |
176
|
|
|
if ($this->getMyType() === static::SP) { |
177
|
|
|
$type = static::IDP; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $type; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
public function getTemplateRoot() |
187
|
|
|
{ |
188
|
|
|
return dirname(__FILE__) . DIRECTORY_SEPARATOR . 'templates'; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
|
|
public function getTemplateRootKey() |
195
|
|
|
{ |
196
|
|
|
return $this->getHandle() . '-' . 'core'; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Components |
201
|
|
|
*/ |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
205
|
|
|
* @returns AbstractCp |
206
|
|
|
*/ |
207
|
|
|
public function getCp() |
208
|
|
|
{ |
209
|
|
|
|
210
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
211
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
212
|
|
|
return $this->get('cp'); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
217
|
|
|
* @returns ProviderServiceInterface |
218
|
|
|
*/ |
219
|
|
|
public function getProvider() |
220
|
|
|
{ |
221
|
|
|
|
222
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
223
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
224
|
|
|
return $this->get('provider'); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
229
|
|
|
* @returns ProviderIdentityServiceInterface |
230
|
|
|
*/ |
231
|
|
|
public function getProviderIdentity() |
232
|
|
|
{ |
233
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
234
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
235
|
|
|
return $this->get('providerIdentity'); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
240
|
|
|
* @return MetadataServiceInterface |
241
|
|
|
*/ |
242
|
|
|
public function getMetadata() |
243
|
|
|
{ |
244
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
245
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
246
|
|
|
return $this->get('metadata'); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
251
|
|
|
* @return SamlRequestInterface |
252
|
|
|
* @throws \yii\base\InvalidConfigException |
253
|
|
|
*/ |
254
|
|
|
public function getLogoutRequest() |
255
|
|
|
{ |
256
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
257
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
258
|
|
|
return $this->get('logoutRequest'); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
263
|
|
|
* @return SamlResponseInterface |
264
|
|
|
* @throws \yii\base\InvalidConfigException |
265
|
|
|
*/ |
266
|
|
|
public function getLogoutResponse() |
267
|
|
|
{ |
268
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
269
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
270
|
|
|
return $this->get('logoutResponse'); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Bindings |
275
|
|
|
*/ |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
279
|
|
|
* @return AbstractHttpPost |
280
|
|
|
*/ |
281
|
|
|
public function getHttpPost() |
282
|
|
|
{ |
283
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
284
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
285
|
|
|
return $this->get('httpPost'); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
290
|
|
|
* @return AbstractHttpRedirect |
291
|
|
|
*/ |
292
|
|
|
public function getHttpRedirect() |
293
|
|
|
{ |
294
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
295
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
296
|
|
|
return $this->get('httpRedirect'); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Log Functions |
302
|
|
|
*/ |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @param $message |
306
|
|
|
*/ |
307
|
|
|
public static function error($message) |
308
|
|
|
{ |
309
|
|
|
\Craft::error($message, static::getInstance()->getHandle()); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @param $message |
314
|
|
|
*/ |
315
|
|
|
public static function warning($message) |
316
|
|
|
{ |
317
|
|
|
\Craft::warning($message, static::getInstance()->getHandle()); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @param $message |
322
|
|
|
*/ |
323
|
|
|
public static function info($message) |
324
|
|
|
{ |
325
|
|
|
\Craft::info($message, static::getInstance()->getHandle()); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @param $message |
330
|
|
|
*/ |
331
|
|
|
public static function debug($message) |
332
|
|
|
{ |
333
|
|
|
\Craft::debug($message, static::getInstance()->getHandle()); |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|