simplesamlphp /
simplesamlphp-module-metaedit
| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | namespace SimpleSAML\Module\metaedit\Controller; |
||||
| 6 | |||||
| 7 | use Exception; |
||||
| 8 | use SimpleSAML\Auth; |
||||
| 9 | use SimpleSAML\Configuration; |
||||
| 10 | use SimpleSAML\Error; |
||||
| 11 | use SimpleSAML\Metadata; |
||||
| 12 | use SimpleSAML\Module\metaedit\MetaEditor as Editor; |
||||
| 13 | use SimpleSAML\SAML2\Constants as C; |
||||
| 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->getOptionalValue('auth', 'login-admin'); |
||||
| 88 | $useridattr = $this->moduleConfig->getOptionalValue('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( |
||||
| 101 | $this->moduleConfig->getOptionalArray('metahandlerConfig', ['directory' => '']), |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 102 | ); |
||||
| 103 | |||||
| 104 | $delete = $request->get('delete'); |
||||
| 105 | if ($delete !== null) { |
||||
| 106 | $premetadata = $mdh->getMetadata($delete, 'saml20-sp-remote'); |
||||
| 107 | $this->requireOwnership($premetadata, $userid); |
||||
|
0 ignored issues
–
show
It seems like
$premetadata can also be of type null; however, parameter $metadata of SimpleSAML\Module\metaed...tor::requireOwnership() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 108 | $mdh->deleteMetadata($delete, 'saml20-sp-remote'); |
||||
| 109 | } |
||||
| 110 | |||||
| 111 | $list = $mdh->getMetadataSet('saml20-sp-remote'); |
||||
| 112 | |||||
| 113 | $slist = ['mine' => [], 'others' => []]; |
||||
| 114 | foreach ($list as $listitem) { |
||||
| 115 | if (array_key_exists('owner', $listitem)) { |
||||
| 116 | if ($listitem['owner'] === $userid) { |
||||
| 117 | $slist['mine'][] = $listitem; |
||||
| 118 | continue; |
||||
| 119 | } |
||||
| 120 | } |
||||
| 121 | $slist['others'][] = $listitem; |
||||
| 122 | } |
||||
| 123 | |||||
| 124 | $t = new Template($this->config, 'metaedit:metalist.twig'); |
||||
| 125 | $t->data['metadata'] = $slist; |
||||
| 126 | $t->data['userid'] = $userid; |
||||
| 127 | |||||
| 128 | return $t; |
||||
| 129 | } |
||||
| 130 | |||||
| 131 | |||||
| 132 | /** |
||||
| 133 | * Editor |
||||
| 134 | * |
||||
| 135 | * @param \Symfony\Component\HttpFoundation\Request $request The current request. |
||||
| 136 | * |
||||
| 137 | * @return \SimpleSAML\XHTML\Template |
||||
| 138 | */ |
||||
| 139 | public function edit(Request $request): Template |
||||
| 140 | { |
||||
| 141 | $authsource = $this->moduleConfig->getOptionalValue('auth', 'login-admin'); |
||||
| 142 | $useridattr = $this->moduleConfig->getOptionalValue('useridattr', 'eduPersonPrincipalName'); |
||||
| 143 | |||||
| 144 | $as = new $this->authSimple($authsource); |
||||
| 145 | $as->requireAuth(); |
||||
| 146 | |||||
| 147 | $attributes = $as->getAttributes(); |
||||
| 148 | // Check if userid exists |
||||
| 149 | if (!isset($attributes[$useridattr])) { |
||||
| 150 | throw new Error\Exception('User ID is missing'); |
||||
| 151 | } |
||||
| 152 | $userid = $attributes[$useridattr][0]; |
||||
| 153 | |||||
| 154 | $entityId = $request->get('entityid'); |
||||
| 155 | $xmlMetadata = $request->get('xmlmetadata'); |
||||
| 156 | |||||
| 157 | $mdh = new Metadata\MetaDataStorageHandlerSerialize( |
||||
| 158 | $this->moduleConfig->getOptionalArray('metahandlerConfig', []), |
||||
|
0 ignored issues
–
show
It seems like
$this->moduleConfig->get...andlerConfig', array()) can also be of type null; however, parameter $config of SimpleSAML\Metadata\Meta...erialize::__construct() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 159 | ); |
||||
| 160 | |||||
| 161 | if ($entityId !== null) { |
||||
| 162 | $metadata = $mdh->getMetadata($entityId, 'saml20-sp-remote'); |
||||
| 163 | $this->requireOwnership($metadata, $userid); |
||||
|
0 ignored issues
–
show
It seems like
$metadata can also be of type null; however, parameter $metadata of SimpleSAML\Module\metaed...tor::requireOwnership() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 164 | } elseif ($xmlMetadata !== null) { |
||||
| 165 | $xmlUtils = new Utils\XML(); |
||||
| 166 | $xmlUtils->checkSAMLMessage($xmlMetadata, 'saml-meta'); |
||||
| 167 | $entities = Metadata\SAMLParser::parseDescriptorsString($xmlMetadata); |
||||
| 168 | $entity = array_pop($entities); |
||||
| 169 | $metadata = $entity->getMetadata20SP(); |
||||
| 170 | |||||
| 171 | /* Trim metadata endpoint arrays. */ |
||||
| 172 | $metadata['AssertionConsumerService'] = [ |
||||
| 173 | Utils\Config\Metadata::getDefaultEndpoint( |
||||
| 174 | $metadata['AssertionConsumerService'], |
||||
| 175 | [C::BINDING_HTTP_POST], |
||||
| 176 | ), |
||||
| 177 | ]; |
||||
| 178 | $metadata['SingleLogoutService'] = [ |
||||
| 179 | Utils\Config\Metadata::getDefaultEndpoint( |
||||
| 180 | $metadata['SingleLogoutService'], |
||||
| 181 | [C::BINDING_HTTP_REDIRECT], |
||||
| 182 | ), |
||||
| 183 | ]; |
||||
| 184 | } else { |
||||
| 185 | $metadata = [ |
||||
| 186 | 'owner' => $userid, |
||||
| 187 | ]; |
||||
| 188 | } |
||||
| 189 | |||||
| 190 | $editor = new Editor(); |
||||
| 191 | |||||
| 192 | if ($request->get('submit')) { |
||||
| 193 | $editor->checkForm($request->request->all()); |
||||
| 194 | $metadata = $editor->formToMeta($request->request->all(), [], ['owner' => $userid]); |
||||
| 195 | $wasEntityId = $request->get('was-entityid'); |
||||
| 196 | if (($wasEntityId !== null) && ($wasEntityId !== $metadata['entityid'])) { |
||||
| 197 | $premetadata = $mdh->getMetadata($wasEntityId, 'saml20-sp-remote'); |
||||
| 198 | $this->requireOwnership($premetadata, $userid); |
||||
| 199 | $mdh->deleteMetadata($wasEntityId, 'saml20-sp-remote'); |
||||
| 200 | } |
||||
| 201 | |||||
| 202 | try { |
||||
| 203 | $testmetadata = $mdh->getMetadata($metadata['entityid'], 'saml20-sp-remote'); |
||||
| 204 | } catch (Exception $e) { |
||||
| 205 | // catch |
||||
| 206 | $testmetadata = null; |
||||
| 207 | } |
||||
| 208 | |||||
| 209 | if ($testmetadata) { |
||||
| 210 | $this->requireOwnership($testmetadata, $userid); |
||||
| 211 | } |
||||
| 212 | |||||
| 213 | $result = $mdh->saveMetadata($metadata['entityid'], 'saml20-sp-remote', $metadata); |
||||
| 214 | if ($result === false) { |
||||
| 215 | throw new Error\Exception("Could not save metadata. See log for details"); |
||||
| 216 | } |
||||
| 217 | |||||
| 218 | return new Template($this->config, 'metaedit:saved.twig'); |
||||
| 219 | } |
||||
| 220 | |||||
| 221 | $form = $editor->metaToForm($metadata); |
||||
| 222 | |||||
| 223 | $t = new Template($this->config, 'metaedit:formedit.twig'); |
||||
| 224 | $t->data['form'] = $form; |
||||
| 225 | |||||
| 226 | return $t; |
||||
| 227 | } |
||||
| 228 | |||||
| 229 | |||||
| 230 | /** |
||||
| 231 | * Importer |
||||
| 232 | * |
||||
| 233 | * @return \SimpleSAML\XHTML\Template |
||||
| 234 | */ |
||||
| 235 | public function import(): Template |
||||
| 236 | { |
||||
| 237 | /* Load simpleSAMLphp, configuration and metadata */ |
||||
| 238 | return new Template($this->config, 'metaedit:xmlimport.twig'); |
||||
| 239 | } |
||||
| 240 | |||||
| 241 | |||||
| 242 | /** |
||||
| 243 | * @param array $metadata |
||||
| 244 | * @param string $userid |
||||
| 245 | * @return void |
||||
| 246 | */ |
||||
| 247 | private function requireOwnership(array $metadata, string $userid): void |
||||
| 248 | { |
||||
| 249 | if (!isset($metadata['owner'])) { |
||||
| 250 | throw new Exception('Metadata has no owner. Which means no one is granted access, not even you.'); |
||||
| 251 | } |
||||
| 252 | |||||
| 253 | if ($metadata['owner'] !== $userid) { |
||||
| 254 | throw new Exception( |
||||
| 255 | 'Metadata has an owner that is not equal to your userid, hence you are not granted access.', |
||||
| 256 | ); |
||||
| 257 | } |
||||
| 258 | } |
||||
| 259 | } |
||||
| 260 |