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