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