1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: dsmrt |
5
|
|
|
* Date: 2/10/18 |
6
|
|
|
* Time: 10:47 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\saml\core\controllers; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\records\Site; |
13
|
|
|
use flipbox\keychain\records\KeyChainRecord; |
14
|
|
|
use flipbox\saml\core\controllers\cp\view\AbstractController; |
|
|
|
|
15
|
|
|
use flipbox\saml\core\controllers\cp\view\metadata\AbstractEditController; |
16
|
|
|
use flipbox\saml\core\controllers\cp\view\metadata\VariablesTrait; |
17
|
|
|
use flipbox\saml\core\exceptions\InvalidMetadata; |
18
|
|
|
use flipbox\saml\core\helpers\SerializeHelper; |
19
|
|
|
use flipbox\saml\core\models\GroupOptions; |
20
|
|
|
use flipbox\saml\core\models\MetadataOptions; |
21
|
|
|
use flipbox\saml\core\models\SettingsInterface; |
22
|
|
|
use flipbox\saml\core\records\AbstractProvider; |
23
|
|
|
use flipbox\saml\core\records\ProviderInterface; |
24
|
|
|
use yii\web\NotFoundHttpException; |
25
|
|
|
|
26
|
|
|
abstract class AbstractMetadataController extends AbstractController implements \flipbox\saml\core\EnsureSAMLPlugin |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
use VariablesTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string |
33
|
|
|
* @throws InvalidMetadata |
34
|
|
|
* @throws \yii\web\ForbiddenHttpException |
35
|
|
|
*/ |
36
|
|
|
public function actionIndex() |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
$this->requireAdmin(false); |
40
|
|
|
|
41
|
|
|
/** @var AbstractProvider $provider */ |
42
|
|
|
$provider = $this->getPlugin()->getProvider()->findByEntityId( |
43
|
|
|
$this->getPlugin()->getSettings()->getEntityId() |
44
|
|
|
)->one(); |
45
|
|
|
|
46
|
|
|
if (! $provider) { |
47
|
|
|
throw new InvalidMetadata('Metadata for this server is missing. Please configure this plugin.'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
SerializeHelper::xmlContentType(); |
51
|
|
|
return $provider->toXmlString(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return \yii\web\Response |
56
|
|
|
* @throws \Exception |
57
|
|
|
* @throws \yii\web\BadRequestHttpException |
58
|
|
|
*/ |
59
|
|
|
public function actionAutoCreate() |
60
|
|
|
{ |
61
|
|
|
$this->requireAdmin(false); |
62
|
|
|
$this->requirePostRequest(); |
63
|
|
|
|
64
|
|
|
$providerRecord = $this->processSaveAction(); |
65
|
|
|
if (is_null($providerRecord->uid)) { |
|
|
|
|
66
|
|
|
$providerRecord->generateUid(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$entityDescriptor = $this->getPlugin()->getMetadata()->create( |
70
|
|
|
$this->getPlugin()->getSettings(), |
|
|
|
|
71
|
|
|
$providerRecord |
|
|
|
|
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$provider = $this->getPlugin()->getProvider()->create( |
75
|
|
|
$entityDescriptor, |
76
|
|
|
$providerRecord->keychain |
|
|
|
|
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$providerRecord->entityId = $provider->getEntityId(); |
|
|
|
|
80
|
|
|
$providerRecord->metadata = $provider->metadata; |
|
|
|
|
81
|
|
|
$providerRecord->setMetadataModel($provider->getMetadataModel()); |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
if (! $this->getPlugin()->getProvider()->save($providerRecord)) { |
85
|
|
|
return $this->renderTemplate( |
86
|
|
|
$this->getPlugin()->getEditProvider()->getTemplateIndex() . AbstractEditController::TEMPLATE_INDEX . DIRECTORY_SEPARATOR . 'edit', |
87
|
|
|
array_merge( |
88
|
|
|
[ |
89
|
|
|
'provider' => $providerRecord, |
90
|
|
|
'keychain' => $providerRecord->keychain ?: new KeyChainRecord(), |
|
|
|
|
91
|
|
|
], |
92
|
|
|
$this->getPlugin()->getEditProvider()->prepVariables($providerRecord) |
93
|
|
|
) |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->redirectToPostedUrl(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return \yii\web\Response |
102
|
|
|
* @throws \Exception |
103
|
|
|
* @throws \yii\web\BadRequestHttpException |
104
|
|
|
*/ |
105
|
|
|
public function actionSave() |
106
|
|
|
{ |
107
|
|
|
|
108
|
|
|
$this->requireAdmin(false); |
109
|
|
|
$this->requirePostRequest(); |
110
|
|
|
|
111
|
|
|
/** @var AbstractProvider $record */ |
112
|
|
|
$record = $this->processSaveAction(); |
113
|
|
|
|
114
|
|
|
if ($record->hasErrors() || ! $this->getPlugin()->getProvider()->save($record)) { |
115
|
|
|
return $this->renderTemplate( |
116
|
|
|
$this->getPlugin()->getEditProvider()->getTemplateIndex() . AbstractEditController::TEMPLATE_INDEX . DIRECTORY_SEPARATOR . 'edit', |
117
|
|
|
array_merge( |
118
|
|
|
[ |
119
|
|
|
'provider' => $record, |
120
|
|
|
'keychain' => $record->keychain ?: new KeyChainRecord(), |
121
|
|
|
], |
122
|
|
|
$this->getPlugin()->getEditProvider()->prepVariables($record) |
123
|
|
|
) |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
Craft::$app->getSession()->setNotice(Craft::t($this->getPlugin()->getHandle(), 'Provider saved.')); |
|
|
|
|
128
|
|
|
|
129
|
|
|
return $this->redirectToPostedUrl(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Actions |
134
|
|
|
*/ |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return \yii\web\Response |
138
|
|
|
* @throws \yii\web\BadRequestHttpException |
139
|
|
|
* @throws \yii\web\ForbiddenHttpException |
140
|
|
|
* @throws \Exception |
141
|
|
|
*/ |
142
|
|
|
public function actionChangeStatus() |
143
|
|
|
{ |
144
|
|
|
|
145
|
|
|
$this->requireAdmin(false); |
146
|
|
|
$this->requirePostRequest(); |
147
|
|
|
|
148
|
|
|
$providerId = Craft::$app->request->getRequiredBodyParam('identifier'); |
149
|
|
|
|
150
|
|
|
/** @var string $recordClass */ |
151
|
|
|
$recordClass = $this->getPlugin()->getProviderRecordClass(); |
152
|
|
|
|
153
|
|
|
/** @var AbstractProvider $record */ |
154
|
|
|
$record = $recordClass::find()->where([ |
155
|
|
|
'id' => $providerId, |
156
|
|
|
])->one(); |
157
|
|
|
|
158
|
|
|
$record->enabled = ! $record->enabled; |
159
|
|
|
|
160
|
|
|
if (! $this->getPlugin()->getProvider()->save($record)) { |
161
|
|
|
return $this->renderTemplate( |
162
|
|
|
$this->getPlugin()->getEditProvider()->getTemplateIndex() . AbstractEditController::TEMPLATE_INDEX . DIRECTORY_SEPARATOR . 'edit', |
163
|
|
|
array_merge( |
164
|
|
|
[ |
165
|
|
|
'provider' => $record, |
166
|
|
|
'keychain' => $record->keychain ?: new KeyChainRecord(), |
167
|
|
|
], |
168
|
|
|
$this->getPlugin()->getEditProvider()->prepVariables($record) |
169
|
|
|
) |
170
|
|
|
); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $this->redirectToPostedUrl(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return \yii\web\Response |
178
|
|
|
* @throws \yii\web\BadRequestHttpException |
179
|
|
|
* @throws \yii\web\ForbiddenHttpException |
180
|
|
|
*/ |
181
|
|
|
public function actionDelete() |
182
|
|
|
{ |
183
|
|
|
$this->requireAdmin(false); |
184
|
|
|
$this->requirePostRequest(); |
185
|
|
|
|
186
|
|
|
$providerId = Craft::$app->request->getRequiredBodyParam('identifier'); |
187
|
|
|
|
188
|
|
|
/** @var string $recordClass */ |
189
|
|
|
$recordClass = $this->getPlugin()->getProviderRecordClass(); |
190
|
|
|
|
191
|
|
|
/** @var ProviderInterface $record */ |
192
|
|
|
$record = $recordClass::find()->where([ |
193
|
|
|
'id' => $providerId, |
194
|
|
|
])->one(); |
195
|
|
|
|
196
|
|
|
if (! $this->getPlugin()->getProvider()->delete($record)) { |
197
|
|
|
return $this->renderTemplate( |
198
|
|
|
$this->getPlugin()->getEditProvider()->getTemplateIndex() . AbstractEditController::TEMPLATE_INDEX . DIRECTORY_SEPARATOR . 'edit', |
199
|
|
|
array_merge( |
200
|
|
|
[ |
201
|
|
|
'provider' => $record, |
202
|
|
|
'keychain' => $record->keychain ?: new KeyChainRecord(), |
|
|
|
|
203
|
|
|
], |
204
|
|
|
$this->getPlugin()->getEditProvider()->prepVariables($record) |
205
|
|
|
) |
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $this->redirectToPostedUrl(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return ProviderInterface |
214
|
|
|
* @throws \Exception |
215
|
|
|
*/ |
216
|
|
|
protected function processSaveAction() |
217
|
|
|
{ |
218
|
|
|
|
219
|
|
|
$providerId = Craft::$app->request->getParam('identifier'); |
220
|
|
|
$entityId = Craft::$app->request->getParam('entityId'); |
221
|
|
|
$keyId = Craft::$app->request->getParam('keychain'); |
222
|
|
|
$providerType = Craft::$app->request->getParam('providerType'); |
223
|
|
|
$providerSite = Craft::$app->request->getParam('providerSite'); |
224
|
|
|
$metadata = Craft::$app->request->getParam('metadata-text'); |
225
|
|
|
$metadataUrl = Craft::$app->request->getParam('metadata-url-text'); |
226
|
|
|
$metadataUrlInterval = Craft::$app->request->getParam('metadata-url-interval-text'); |
227
|
|
|
$mapping = Craft::$app->request->getParam('mapping', []); |
228
|
|
|
$label = Craft::$app->request->getRequiredParam('label'); |
229
|
|
|
$nameIdOverride = Craft::$app->request->getParam('nameIdOverride'); |
230
|
|
|
|
231
|
|
|
|
232
|
|
|
$plugin = $this->getPlugin(); |
233
|
|
|
|
234
|
|
|
$recordClass = $this->getPlugin()->getProviderRecordClass(); |
235
|
|
|
/** @var AbstractProvider $record */ |
236
|
|
|
if ($providerId) { |
237
|
|
|
$record = $recordClass::find()->where([ |
238
|
|
|
'id' => $providerId, |
239
|
|
|
])->one(); |
240
|
|
|
|
241
|
|
|
if (! $record) { |
242
|
|
|
throw new \Exception("Provider with ID: {$providerId} not found."); |
243
|
|
|
} |
244
|
|
|
} else { |
245
|
|
|
$record = new $recordClass(); |
246
|
|
|
|
247
|
|
|
//enabled is default |
248
|
|
|
$record->enabled = true; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
$record->entityId = $entityId; |
252
|
|
|
if ($providerSite) { |
253
|
|
|
if ($site = Site::findOne([ |
254
|
|
|
'id' => $providerSite, |
255
|
|
|
])) { |
256
|
|
|
$record->setSite($site); |
257
|
|
|
} else { |
258
|
|
|
// @todo add |
259
|
|
|
var_dump('wat?'); |
|
|
|
|
260
|
|
|
exit; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
// Metadata |
265
|
|
|
if (! $metadata && $metadataUrl) { |
266
|
|
|
$metadataModel = $this->getPlugin()->getMetadata()->fetchByUrl($metadataUrl); |
267
|
|
|
$record->metadata = $metadataModel->toXML()->ownerDocument->saveXML(); |
268
|
|
|
$record->setMetadataModel($metadataModel); |
269
|
|
|
} else { |
270
|
|
|
$record->metadata = $metadata; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
// Mapping |
274
|
|
|
if (is_array($mapping)) { |
275
|
|
|
$record->setMapping( |
276
|
|
|
$mapping |
277
|
|
|
); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
$record->providerType = $providerType; |
281
|
|
|
$record->nameIdOverride = $nameIdOverride; |
282
|
|
|
|
283
|
|
|
// IDP Plugin on SP Provider ONLY |
284
|
|
|
if ($this->getPlugin()->getMyType() === SettingsInterface::IDP |
285
|
|
|
&& |
286
|
|
|
$providerType === SettingsInterface::SP |
287
|
|
|
) { |
288
|
|
|
// Encryption settings |
289
|
|
|
$record->encryptAssertions = Craft::$app->request->getParam('encryptAssertions') ?: 0; |
290
|
|
|
$record->encryptionMethod = Craft::$app->request->getParam('encryptionMethod'); |
291
|
|
|
$record->setGroupOptions( |
292
|
|
|
$groupOptions = new GroupOptions([ |
293
|
|
|
'options' => Craft::$app->request->getParam('groupOptions', []) ?: [], |
294
|
|
|
]) |
295
|
|
|
); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
$record->setMetadataOptions( |
299
|
|
|
new MetadataOptions([ |
300
|
|
|
'url' => $metadataUrl, |
301
|
|
|
'expiryInterval' => $metadataUrlInterval, |
302
|
|
|
]) |
303
|
|
|
); |
304
|
|
|
|
305
|
|
|
// Group properties |
306
|
|
|
$record->syncGroups = Craft::$app->request->getParam('syncGroups') ?: 0; |
307
|
|
|
|
308
|
|
|
$record->groupsAttributeName = |
309
|
|
|
Craft::$app->request->getParam('groupsAttributeName') ?: |
310
|
|
|
AbstractProvider::DEFAULT_GROUPS_ATTRIBUTE_NAME; |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* check for label and add error if it's empty |
314
|
|
|
*/ |
315
|
|
|
if ($label) { |
316
|
|
|
$record->label = $label; |
317
|
|
|
} else { |
318
|
|
|
$record->addError('label', Craft::t($plugin->getHandle(), "Label is required.")); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
|
322
|
|
|
if ($keyId) { |
323
|
|
|
/** @var KeyChainRecord $keychain */ |
324
|
|
|
if ($keychain = KeyChainRecord::find()->where([ |
325
|
|
|
'id' => $keyId, |
326
|
|
|
])->one()) { |
327
|
|
|
$record->setKeychain( |
328
|
|
|
$keychain |
329
|
|
|
); |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Metadata should exist for the remote provider |
335
|
|
|
*/ |
336
|
|
|
if ($plugin->getRemoteType() === $providerType && ! $record->metadata) { |
337
|
|
|
$record->addError('metadata-text', Craft::t($plugin->getHandle(), "Metadata cannot be empty.")); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
return $record; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* @param $keyId |
346
|
|
|
* @return \craft\web\Response|\yii\console\Response |
347
|
|
|
* @throws NotFoundHttpException |
348
|
|
|
* @throws \yii\web\ForbiddenHttpException |
349
|
|
|
* @throws \yii\web\HttpException |
350
|
|
|
* @throws \yii\web\RangeNotSatisfiableHttpException |
351
|
|
|
*/ |
352
|
|
|
public function actionDownloadCertificate($keyId) |
353
|
|
|
{ |
354
|
|
|
$this->requireAdmin(false); |
355
|
|
|
|
356
|
|
|
/** @var KeyChainRecord $keychain */ |
357
|
|
|
if (! $keychain = KeyChainRecord::find()->where([ |
358
|
|
|
'id' => $keyId, |
359
|
|
|
])->one()) { |
360
|
|
|
throw new NotFoundHttpException('Key not found'); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
return Craft::$app->response->sendContentAsFile($keychain->getDecryptedCertificate(), 'certificate.crt'); |
364
|
|
|
} |
365
|
|
|
} |
366
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: