Skip to content

Commit e81586f

Browse files
committed
Create MetadataBuilder to replace the old SAMLBuilder
1 parent 968e7b8 commit e81586f

10 files changed

Lines changed: 1091 additions & 1493 deletions

File tree

modules/admin/src/Controller/Federation.php

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use SimpleSAML\Module\adfs\IdP\ADFS as ADFS_IdP;
2121
use SimpleSAML\Module\admin\Event\FederationPageEvent;
2222
use SimpleSAML\Module\saml\IdP\SAML2 as SAML2_IdP;
23+
use SimpleSAML\Module\saml\MetadataBuilder;
2324
use SimpleSAML\Utils;
2425
use SimpleSAML\XHTML\Template;
2526
use Symfony\Component\HttpFoundation\Request;
@@ -248,15 +249,15 @@ private function getHostedIdP(): array
248249
sprintf('The entityID cannot be longer than %d characters.', C::SAML2INT_ENTITYID_MAX_LENGTH),
249250
);
250251

251-
$builder = new SAMLBuilder($entity['entityid']);
252-
$builder->addMetadataIdP20($entity['metadata_array']);
253-
$builder->addOrganizationInfo($entity['metadata_array']);
254-
255-
$entity['metadata'] = Signer::sign(
256-
$builder->getEntityDescriptorText(),
257-
$entity['metadata_array'],
258-
'SAML 2 IdP',
252+
$builder = new MetadataBuilder(
253+
$this->config,
254+
Configuration::loadFromArray($entity['metadata_array']),
259255
);
256+
$document = $builder->buildDocument()->toXML();
257+
$document->ownerDocument->formatOutput = true;
258+
$document->ownerDocument->encoding = 'UTF-8';
259+
260+
$entity['metadata'] = $document->ownerDocument?->saveXML();
260261
$entities[$index] = $entity;
261262
}
262263
} catch (Exception $e) {
@@ -294,20 +295,15 @@ private function getHostedIdP(): array
294295
sprintf('The entityID cannot be longer than %d characters.', C::SAML2INT_ENTITYID_MAX_LENGTH),
295296
);
296297

297-
$builder = new SAMLBuilder($entity['entityid']);
298-
$builder->addSecurityTokenServiceType($entity['metadata_array']);
299-
$builder->addOrganizationInfo($entity['metadata_array']);
300-
if (isset($entity['metadata_array']['contacts'])) {
301-
foreach ($entity['metadata_array']['contacts'] as $contact) {
302-
$builder->addContact(Utils\Config\Metadata::getContact($contact));
303-
}
304-
}
305-
306-
$entity['metadata'] = Signer::sign(
307-
$builder->getEntityDescriptorText(),
308-
$entity['metadata_array'],
309-
'ADFS IdP',
298+
$builder = new MetadataBuilder(
299+
$this->config,
300+
Configuration::loadFromArray($entity['metadata_array']),
310301
);
302+
$document = $builder->buildDocument()->toXML();
303+
$document->ownerDocument->formatOutput = true;
304+
$document->ownerDocument->encoding = 'UTF-8';
305+
306+
$entity['metadata'] = $document->ownerDocument->saveXML();
311307
$entities[$index] = $entity;
312308
}
313309
} catch (Exception $e) {
@@ -379,23 +375,18 @@ private function getHostedSP(): array
379375
),
380376
);
381377

382-
$builder = new SAMLBuilder($source->getEntityId());
383-
$builder->addMetadataSP20($metadata, $source->getSupportedProtocols());
384-
$builder->addOrganizationInfo($metadata);
385-
$xml = $builder->getEntityDescriptorText(true);
386-
387-
// sanitize the resulting array
388-
unset($metadata['metadata-set']);
389-
unset($metadata['entityid']);
378+
$builder = new MetadataBuilder(Configuration::getInstance(), Configuration::loadFromArray($metadata));
379+
$entityDescriptor = $builder->buildDocument();
380+
$document = $entityDescriptor->toXML();
381+
$document->ownerDocument->formatOutput = true;
382+
$document->ownerDocument->encoding = 'UTF-8';
383+
$xml = $document->ownerDocument->saveXML();
390384

391385
// sanitize the attributes array to remove friendly names
392386
if (isset($metadata['attributes']) && is_array($metadata['attributes'])) {
393387
$metadata['attributes'] = array_values($metadata['attributes']);
394388
}
395389

396-
// sign the metadata if enabled
397-
$xml = Signer::sign($xml, $source->getMetadata()->toArray(), 'SAML 2 SP');
398-
399390
$entities[] = [
400391
'authid' => $source->getAuthId(),
401392
'entityid' => $source->getEntityId(),

modules/saml/src/Controller/Metadata.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use SimpleSAML\Metadata\MetaDataStorageHandler;
1313
use SimpleSAML\Module;
1414
use SimpleSAML\Module\saml\IdP\SAML2 as SAML2_IdP;
15+
use SimpleSAML\Module\saml\MetadataBuilder;
1516
use SimpleSAML\Utils;
1617
use Symfony\Component\HttpFoundation\Request;
1718
use Symfony\Component\HttpFoundation\Response;
@@ -93,14 +94,11 @@ public function metadata(Request $request): Response
9394
}
9495
$metaArray = SAML2_IdP::getHostedMetadata($idpentityid, $this->mdHandler);
9596

96-
$metaBuilder = new SSPMetadata\SAMLBuilder($idpentityid);
97-
$metaBuilder->addMetadataIdP20($metaArray);
98-
$metaBuilder->addOrganizationInfo($metaArray);
99-
100-
$metaxml = $metaBuilder->getEntityDescriptorText();
101-
102-
// sign the metadata if enabled
103-
$metaxml = SSPMetadata\Signer::sign($metaxml, $metaArray, 'SAML 2 IdP');
97+
$builder = new MetadataBuilder($this->config, Configuration::loadFromArray($metaArray));
98+
$document = $builder->buildDocument()->toXML();
99+
$document->ownerDocument->formatOutput = true;
100+
$document->ownerDocument->encoding = 'UTF-8';
101+
$metaxml = $document->ownerDocument->saveXML();
104102

105103
$response = new Response();
106104
$response->setEtag(hash('sha256', $metaxml));

modules/saml/src/Controller/ServiceProvider.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use SimpleSAML\Metadata;
2727
use SimpleSAML\Module;
2828
use SimpleSAML\Module\saml\Auth\Source\SP;
29+
use SimpleSAML\Module\saml\MetadataBuilder;
2930
use SimpleSAML\Session;
3031
use SimpleSAML\Store\StoreFactory;
3132
use SimpleSAML\Utils;
@@ -713,11 +714,13 @@ public function metadata(Request $request, string $sourceId): Response
713714
$spconfig = $source->getMetadata();
714715
$metaArray20 = $source->getHostedMetadata();
715716

716-
$metaBuilder = new Metadata\SAMLBuilder($entityId);
717-
$metaBuilder->addMetadataSP20($metaArray20, $source->getSupportedProtocols());
718-
$metaBuilder->addOrganizationInfo($metaArray20);
717+
$builder = new MetadataBuilder($this->config, Configuration::loadFromArray($metaArray20));
718+
$entityDescriptor = $builder->buildDocument();
719+
$document = $entityDescriptor->toXML();
720+
$document->ownerDocument->formatOutput = true;
721+
$document->ownerDocument->encoding = 'UTF-8';
719722

720-
$xml = $metaBuilder->getEntityDescriptorText();
723+
$xml = $document->ownerDocument->saveXML();
721724

722725
// sign the metadata if enabled
723726
$metaxml = Metadata\Signer::sign($xml, $spconfig->toArray(), 'SAML 2 SP');

0 commit comments

Comments
 (0)