1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\Module\metaedit\Controller; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use SAML2\Constants; |
9
|
|
|
use SimpleSAML\Auth; |
10
|
|
|
use SimpleSAML\Configuration; |
11
|
|
|
use SimpleSAML\Error; |
12
|
|
|
use SimpleSAML\Metadata; |
13
|
|
|
use SimpleSAML\Module\metaedit\MetaEditor as Editor; |
14
|
|
|
use SimpleSAML\Session; |
15
|
|
|
use SimpleSAML\Utils; |
16
|
|
|
use SimpleSAML\XHTML\Template; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
|
19
|
|
|
use function array_key_exists; |
20
|
|
|
use function array_pop; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Controller class for the metaedit module. |
24
|
|
|
* |
25
|
|
|
* This class serves the different views available in the module. |
26
|
|
|
* |
27
|
|
|
* @package simplesamlphp/simplesamlphp-module-metaedit |
28
|
|
|
*/ |
29
|
|
|
class MetaEditor |
30
|
|
|
{ |
31
|
|
|
/** @var \SimpleSAML\Configuration */ |
32
|
|
|
protected Configuration $config; |
33
|
|
|
|
34
|
|
|
/** @var \SimpleSAML\Configuration */ |
35
|
|
|
protected Configuration $moduleConfig; |
36
|
|
|
|
37
|
|
|
/** @var \SimpleSAML\Session */ |
38
|
|
|
protected Session $session; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var \SimpleSAML\Auth\Simple|string |
42
|
|
|
* @psalm-var \SimpleSAML\Auth\Simple|class-string |
43
|
|
|
*/ |
44
|
|
|
protected $authSimple = Auth\Simple::class; |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Controller constructor. |
49
|
|
|
* |
50
|
|
|
* It initializes the global configuration and session for the controllers implemented here. |
51
|
|
|
* |
52
|
|
|
* @param \SimpleSAML\Configuration $config The configuration to use by the controllers. |
53
|
|
|
* @param \SimpleSAML\Session $session The session to use by the controllers. |
54
|
|
|
* |
55
|
|
|
* @throws \Exception |
56
|
|
|
*/ |
57
|
|
|
public function __construct( |
58
|
|
|
Configuration $config, |
59
|
|
|
Session $session |
60
|
|
|
) { |
61
|
|
|
$this->config = $config; |
62
|
|
|
$this->moduleConfig = Configuration::getConfig('module_metaedit.php'); |
63
|
|
|
$this->session = $session; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Inject the \SimpleSAML\Auth\Simple dependency. |
69
|
|
|
* |
70
|
|
|
* @param \SimpleSAML\Auth\Simple $authSimple |
71
|
|
|
*/ |
72
|
|
|
public function setAuthSimple(Auth\Simple $authSimple): void |
73
|
|
|
{ |
74
|
|
|
$this->authSimple = $authSimple; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Main index |
80
|
|
|
* |
81
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request The current request. |
82
|
|
|
* |
83
|
|
|
* @return \SimpleSAML\XHTML\Template |
84
|
|
|
*/ |
85
|
|
|
public function main(Request $request): Template |
86
|
|
|
{ |
87
|
|
|
$authsource = $this->moduleConfig->getValue('auth', 'login-admin'); |
88
|
|
|
$useridattr = $this->moduleConfig->getValue('useridattr', 'eduPersonPrincipalName'); |
89
|
|
|
|
90
|
|
|
$as = new $this->authSimple($authsource); |
91
|
|
|
$as->requireAuth(); |
92
|
|
|
$attributes = $as->getAttributes(); |
93
|
|
|
|
94
|
|
|
// Check if userid exists |
95
|
|
|
if (!isset($attributes[$useridattr])) { |
96
|
|
|
throw new Error\Exception('User ID is missing'); |
97
|
|
|
} |
98
|
|
|
$userid = $attributes[$useridattr][0]; |
99
|
|
|
|
100
|
|
|
$mdh = new Metadata\MetaDataStorageHandlerSerialize($this->moduleConfig->getArray('metahandlerConfig', ['directory' => ''])); |
101
|
|
|
|
102
|
|
|
$delete = $request->get('delete'); |
103
|
|
|
if ($delete !== null) { |
104
|
|
|
$premetadata = $mdh->getMetadata($delete, 'saml20-sp-remote'); |
105
|
|
|
$this->requireOwnership($premetadata, $userid); |
|
|
|
|
106
|
|
|
$mdh->deleteMetadata($delete, 'saml20-sp-remote'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$list = $mdh->getMetadataSet('saml20-sp-remote'); |
110
|
|
|
|
111
|
|
|
$slist = ['mine' => [], 'others' => []]; |
112
|
|
|
foreach ($list as $listitem) { |
113
|
|
|
if (array_key_exists('owner', $listitem)) { |
114
|
|
|
if ($listitem['owner'] === $userid) { |
115
|
|
|
$slist['mine'][] = $listitem; |
116
|
|
|
continue; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
$slist['others'][] = $listitem; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$t = new Template($this->config, 'metaedit:metalist.twig'); |
123
|
|
|
$t->data['metadata'] = $slist; |
124
|
|
|
$t->data['userid'] = $userid; |
125
|
|
|
|
126
|
|
|
return $t; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Editor |
132
|
|
|
* |
133
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request The current request. |
134
|
|
|
* |
135
|
|
|
* @return \SimpleSAML\XHTML\Template |
136
|
|
|
*/ |
137
|
|
|
public function edit(Request $request): Template |
138
|
|
|
{ |
139
|
|
|
$authsource = $this->moduleConfig->getValue('auth', 'login-admin'); |
140
|
|
|
$useridattr = $this->moduleConfig->getValue('useridattr', 'eduPersonPrincipalName'); |
141
|
|
|
|
142
|
|
|
$as = new $this->authSimple($authsource); |
143
|
|
|
$as->requireAuth(); |
144
|
|
|
|
145
|
|
|
$attributes = $as->getAttributes(); |
146
|
|
|
// Check if userid exists |
147
|
|
|
if (!isset($attributes[$useridattr])) { |
148
|
|
|
throw new Error\Exception('User ID is missing'); |
149
|
|
|
} |
150
|
|
|
$userid = $attributes[$useridattr][0]; |
151
|
|
|
|
152
|
|
|
$entityId = $request->get('entityid'); |
153
|
|
|
$xmlMetadata = $request->get('xmlmetadata'); |
154
|
|
|
|
155
|
|
|
$mdh = new Metadata\MetaDataStorageHandlerSerialize($this->moduleConfig->getArray('metahandlerConfig', [])); |
156
|
|
|
|
157
|
|
|
if ($entityId !== null) { |
158
|
|
|
$metadata = $mdh->getMetadata($entityId, 'saml20-sp-remote'); |
159
|
|
|
$this->requireOwnership($metadata, $userid); |
|
|
|
|
160
|
|
|
} elseif ($xmlMetadata !== null) { |
161
|
|
|
$xmlUtils = new Utils\XML(); |
162
|
|
|
$xmlUtils->checkSAMLMessage($xmlMetadata, 'saml-meta'); |
163
|
|
|
$entities = Metadata\SAMLParser::parseDescriptorsString($xmlMetadata); |
164
|
|
|
$entity = array_pop($entities); |
165
|
|
|
$metadata = $entity->getMetadata20SP(); |
166
|
|
|
|
167
|
|
|
/* Trim metadata endpoint arrays. */ |
168
|
|
|
$metadata['AssertionConsumerService'] = [ |
169
|
|
|
Utils\Config\Metadata::getDefaultEndpoint( |
170
|
|
|
$metadata['AssertionConsumerService'], |
171
|
|
|
[Constants::BINDING_HTTP_POST] |
172
|
|
|
) |
173
|
|
|
]; |
174
|
|
|
$metadata['SingleLogoutService'] = [ |
175
|
|
|
Utils\Config\Metadata::getDefaultEndpoint( |
176
|
|
|
$metadata['SingleLogoutService'], |
177
|
|
|
[Constants::BINDING_HTTP_REDIRECT] |
178
|
|
|
) |
179
|
|
|
]; |
180
|
|
|
} else { |
181
|
|
|
$metadata = [ |
182
|
|
|
'owner' => $userid, |
183
|
|
|
]; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$editor = new Editor(); |
187
|
|
|
|
188
|
|
|
if ($request->get('submit')) { |
189
|
|
|
$editor->checkForm($request->request->all()); |
190
|
|
|
$metadata = $editor->formToMeta($request->request->all(), [], ['owner' => $userid]); |
191
|
|
|
$wasEntityId = $request->get('was-entityid'); |
192
|
|
|
if (($wasEntityId !== null) && ($wasEntityId !== $metadata['entityid'])) { |
193
|
|
|
$premetadata = $mdh->getMetadata($wasEntityId, 'saml20-sp-remote'); |
194
|
|
|
$this->requireOwnership($premetadata, $userid); |
195
|
|
|
$mdh->deleteMetadata($wasEntityId, 'saml20-sp-remote'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
try { |
199
|
|
|
$testmetadata = $mdh->getMetadata($metadata['entityid'], 'saml20-sp-remote'); |
200
|
|
|
} catch (Exception $e) { |
201
|
|
|
// catch |
202
|
|
|
$testmetadata = null; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
if ($testmetadata) { |
206
|
|
|
$this->requireOwnership($testmetadata, $userid); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$result = $mdh->saveMetadata($metadata['entityid'], 'saml20-sp-remote', $metadata); |
210
|
|
|
if ($result === false) { |
211
|
|
|
throw new Error\Exception("Could not save metadata. See log for details"); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return new Template($this->config, 'metaedit:saved.twig'); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$form = $editor->metaToForm($metadata); |
218
|
|
|
|
219
|
|
|
$t = new Template($this->config, 'metaedit:formedit.twig'); |
220
|
|
|
$t->data['form'] = $form; |
221
|
|
|
|
222
|
|
|
return $t; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Importer |
228
|
|
|
* |
229
|
|
|
* @return \SimpleSAML\XHTML\Template |
230
|
|
|
*/ |
231
|
|
|
public function import(): Template |
232
|
|
|
{ |
233
|
|
|
/* Load simpleSAMLphp, configuration and metadata */ |
234
|
|
|
return new Template($this->config, 'metaedit:xmlimport.twig'); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param array $metadata |
240
|
|
|
* @param string $userid |
241
|
|
|
* @return void |
242
|
|
|
*/ |
243
|
|
|
private function requireOwnership(array $metadata, string $userid): void |
244
|
|
|
{ |
245
|
|
|
if (!isset($metadata['owner'])) { |
246
|
|
|
throw new Exception('Metadata has no owner. Which means no one is granted access, not even you.'); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
if ($metadata['owner'] !== $userid) { |
250
|
|
|
throw new Exception( |
251
|
|
|
'Metadata has an owner that is not equal to your userid, hence you are not granted access.' |
252
|
|
|
); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|