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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
|
<?php
declare(strict_types=1);
namespace SimpleSAML\Metadata;
use SAML2\Constants;
use SAML2\XML\md\AttributeAuthorityDescriptor;
use SAML2\XML\md\AttributeConsumingService;
use SAML2\XML\md\EndpointType;
use SAML2\XML\md\EntityDescriptor;
use SAML2\XML\md\IDPSSODescriptor;
use SAML2\XML\md\IndexedEndpointType;
use SAML2\XML\md\Organization;
use SAML2\XML\md\RequestedAttribute;
use SAML2\XML\md\RoleDescriptor;
use SAML2\XML\md\SPSSODescriptor;
use SAML2\XML\mdattr\EntityAttributes;
use SAML2\XML\mdrpi\RegistrationInfo;
use SAML2\XML\mdui\DiscoHints;
use SAML2\XML\mdui\Keywords;
use SAML2\XML\mdui\Logo;
use SAML2\XML\mdui\UIInfo;
use SAML2\XML\saml\Attribute;
use SAML2\XML\saml\AttributeValue;
use SAML2\XML\shibmd\Scope;
use SimpleSAML\Configuration;
use SimpleSAML\Logger;
use SimpleSAML\Module\adfs\SAML2\XML\fed\SecurityTokenServiceType;
use SimpleSAML\Utils;
/**
* Class for generating SAML 2.0 metadata from SimpleSAMLphp metadata arrays.
*
* This class builds SAML 2.0 metadata for an entity by examining the metadata for the entity.
*
* @package SimpleSAMLphp
*/
class SAMLBuilder
{
/**
* The EntityDescriptor we are building.
*
* @var \SAML2\XML\md\EntityDescriptor
*/
private $entityDescriptor;
/**
* The maximum time in seconds the metadata should be cached.
*
* @var int|null
*/
private $maxCache = null;
/**
* The maximum time in seconds since the current time that this metadata should be considered valid.
*
* @var int|null
*/
private $maxDuration = null;
/**
* Initialize the SAML builder.
*
* @param string $entityId The entity id of the entity.
* @param int|null $maxCache The maximum time in seconds the metadata should be cached. Defaults to null
* @param int|null $maxDuration The maximum time in seconds this metadata should be considered valid. Defaults
* to null.
* @return void
*/
public function __construct($entityId, $maxCache = null, $maxDuration = null)
{
assert(is_string($entityId));
$this->maxCache = $maxCache;
$this->maxDuration = $maxDuration;
$this->entityDescriptor = new EntityDescriptor();
$this->entityDescriptor->setEntityID($entityId);
}
/**
* @param array $metadata
* @return void
*/
private function setExpiration(array $metadata): void
{
if (array_key_exists('expire', $metadata)) {
if ($metadata['expire'] - time() < $this->maxDuration) {
$this->maxDuration = $metadata['expire'] - time();
}
}
if ($this->maxCache !== null) {
$this->entityDescriptor->setCacheDuration('PT' . $this->maxCache . 'S');
}
if ($this->maxDuration !== null) {
$this->entityDescriptor->setValidUntil(time() + $this->maxDuration);
}
}
/**
* Retrieve the EntityDescriptor element which is generated for this entity.
*
* @return \DOMElement The EntityDescriptor element of this entity.
*/
public function getEntityDescriptor()
{
$xml = $this->entityDescriptor->toXML();
$xml->ownerDocument->appendChild($xml);
return $xml;
}
/**
* Retrieve the EntityDescriptor as text.
*
* This function serializes this EntityDescriptor, and returns it as text.
*
* @param bool $formatted Whether the returned EntityDescriptor should be formatted first.
*
* @return string The serialized EntityDescriptor.
*/
public function getEntityDescriptorText($formatted = true)
{
assert(is_bool($formatted));
$xml = $this->getEntityDescriptor();
if ($formatted) {
Utils\XML::formatDOMElement($xml);
}
return $xml->ownerDocument->saveXML();
}
/**
* Add a SecurityTokenServiceType for ADFS metadata.
*
* @param array $metadata The metadata with the information about the SecurityTokenServiceType.
* @return void
*/
public function addSecurityTokenServiceType($metadata)
{
assert(is_array($metadata));
assert(isset($metadata['entityid']));
assert(isset($metadata['metadata-set']));
$metadata = Configuration::loadFromArray($metadata, $metadata['entityid']);
$defaultEndpoint = $metadata->getDefaultEndpoint('SingleSignOnService');
/**
* @psalm-var \SAML2\XML\md\RoleDescriptor $e
* @psalm-suppress UndefinedClass
*/
$e = new SecurityTokenServiceType();
$e->setLocation($defaultEndpoint['Location']);
$this->addCertificate($e, $metadata);
$this->entityDescriptor->addRoleDescriptor($e);
}
/**
* Add extensions to the metadata.
*
* @param \SimpleSAML\Configuration $metadata The metadata to get extensions from.
* @param \SAML2\XML\md\RoleDescriptor $e Reference to the element where the Extensions element should be included.
* @return void
*/
private function addExtensions(Configuration $metadata, RoleDescriptor $e): void
{
if ($metadata->hasValue('tags')) {
$a = new Attribute();
$a->setName('tags');
foreach ($metadata->getArray('tags') as $tag) {
$a->addAttributeValue(new AttributeValue($tag));
}
$e->setExtensions(array_merge($e->getExtensions(), [$a]));
}
if ($metadata->hasValue('hint.cidr')) {
$a = new Attribute();
$a->setName('hint.cidr');
foreach ($metadata->getArray('hint.cidr') as $hint) {
$a->addAttributeValue(new AttributeValue($hint));
}
$e->setExtensions(array_merge($e->getExtensions(), [$a]));
}
if ($metadata->hasValue('scope')) {
foreach ($metadata->getArray('scope') as $scopetext) {
$s = new Scope();
$s->setScope($scopetext);
// Check whether $ ^ ( ) * | \ are in a scope -> assume regex.
if (1 === preg_match('/[\$\^\)\(\*\|\\\\]/', $scopetext)) {
$s->setIsRegexpScope(true);
} else {
$s->setIsRegexpScope(false);
}
$e->setExtensions(array_merge($e->getExtensions(), [$s]));
}
}
if ($metadata->hasValue('EntityAttributes')) {
$ea = new EntityAttributes();
foreach ($metadata->getArray('EntityAttributes') as $attributeName => $attributeValues) {
$a = new Attribute();
$a->setName($attributeName);
$a->setNameFormat('urn:oasis:names:tc:SAML:2.0:attrname-format:uri');
// Attribute names that is not URI is prefixed as this: '{nameformat}name'
if (preg_match('/^\{(.*?)\}(.*)$/', $attributeName, $matches)) {
$a->setName($matches[2]);
$nameFormat = $matches[1];
if ($nameFormat !== Constants::NAMEFORMAT_UNSPECIFIED) {
$a->setNameFormat($nameFormat);
}
}
foreach ($attributeValues as $attributeValue) {
$a->addAttributeValue(new AttributeValue($attributeValue));
}
$ea->addChildren($a);
}
$this->entityDescriptor->setExtensions(
array_merge($this->entityDescriptor->getExtensions(), [$ea])
);
}
if ($metadata->hasValue('RegistrationInfo')) {
$ri = new RegistrationInfo();
foreach ($metadata->getArray('RegistrationInfo') as $riName => $riValues) {
switch ($riName) {
case 'authority':
$ri->setRegistrationAuthority($riValues);
break;
case 'instant':
$ri->setRegistrationInstant(\SAML2\Utils::xsDateTimeToTimestamp($riValues));
break;
case 'policies':
$ri->setRegistrationPolicy($riValues);
break;
}
}
$this->entityDescriptor->setExtensions(
array_merge($this->entityDescriptor->getExtensions(), [$ri])
);
}
if ($metadata->hasValue('UIInfo')) {
$ui = new UIInfo();
foreach ($metadata->getArray('UIInfo') as $uiName => $uiValues) {
switch ($uiName) {
case 'DisplayName':
$ui->setDisplayName($uiValues);
break;
case 'Description':
$ui->setDescription($uiValues);
break;
case 'InformationURL':
$ui->setInformationURL($uiValues);
break;
case 'PrivacyStatementURL':
$ui->setPrivacyStatementURL($uiValues);
break;
case 'Keywords':
foreach ($uiValues as $lang => $keywords) {
$uiItem = new Keywords();
$uiItem->setLanguage($lang);
$uiItem->setKeywords($keywords);
$ui->addKeyword($uiItem);
}
break;
case 'Logo':
foreach ($uiValues as $logo) {
$uiItem = new Logo();
$uiItem->setUrl($logo['url']);
$uiItem->setWidth($logo['width']);
$uiItem->setHeight($logo['height']);
if (isset($logo['lang'])) {
$uiItem->setLanguage($logo['lang']);
}
$ui->addLogo($uiItem);
}
break;
}
}
$e->setExtensions(array_merge($e->getExtensions(), [$ui]));
}
if ($metadata->hasValue('DiscoHints')) {
$dh = new DiscoHints();
foreach ($metadata->getArray('DiscoHints') as $dhName => $dhValues) {
switch ($dhName) {
case 'IPHint':
$dh->setIPHint($dhValues);
break;
case 'DomainHint':
$dh->setDomainHint($dhValues);
break;
case 'GeolocationHint':
$dh->setGeolocationHint($dhValues);
break;
}
}
$e->setExtensions(array_merge($e->getExtensions(), [$dh]));
}
}
/**
* Add an Organization element based on data passed as parameters
*
* @param array $orgName An array with the localized OrganizationName.
* @param array $orgDisplayName An array with the localized OrganizationDisplayName.
* @param array $orgURL An array with the localized OrganizationURL.
* @return void
*/
public function addOrganization(array $orgName, array $orgDisplayName, array $orgURL)
{
$org = new Organization();
$org->setOrganizationName($orgName);
$org->setOrganizationDisplayName($orgDisplayName);
$org->setOrganizationURL($orgURL);
$this->entityDescriptor->setOrganization($org);
}
/**
* Add an Organization element based on metadata array.
*
* @param array $metadata The metadata we should extract the organization information from.
* @return void
*/
public function addOrganizationInfo(array $metadata)
{
if (
empty($metadata['OrganizationName']) ||
empty($metadata['OrganizationDisplayName']) ||
empty($metadata['OrganizationURL'])
) {
// empty or incomplete organization information
return;
}
$orgName = Utils\Arrays::arrayize($metadata['OrganizationName'], 'en');
$orgDisplayName = Utils\Arrays::arrayize($metadata['OrganizationDisplayName'], 'en');
$orgURL = Utils\Arrays::arrayize($metadata['OrganizationURL'], 'en');
$this->addOrganization($orgName, $orgDisplayName, $orgURL);
}
/**
* Add a list of endpoints to metadata.
*
* @param array $endpoints The endpoints.
* @param bool $indexed Whether the endpoints should be indexed.
*
* @return array An array of endpoint objects,
* either \SAML2\XML\md\EndpointType or \SAML2\XML\md\IndexedEndpointType.
*/
private static function createEndpoints(array $endpoints, bool $indexed): array
{
$ret = [];
foreach ($endpoints as &$ep) {
if ($indexed) {
$t = new IndexedEndpointType();
if (!isset($ep['index'])) {
// Find the maximum index
$maxIndex = -1;
foreach ($endpoints as $ep) {
if (!isset($ep['index'])) {
continue;
}
if ($ep['index'] > $maxIndex) {
$maxIndex = $ep['index'];
}
}
$ep['index'] = $maxIndex + 1;
}
$t->setIndex($ep['index']);
} else {
$t = new EndpointType();
}
$t->setBinding($ep['Binding']);
$t->setLocation($ep['Location']);
if (isset($ep['ResponseLocation'])) {
$t->setResponseLocation($ep['ResponseLocation']);
}
if (isset($ep['hoksso:ProtocolBinding'])) {
$t->setAttributeNS(
Constants::NS_HOK,
'hoksso:ProtocolBinding',
Constants::BINDING_HTTP_REDIRECT
);
}
$ret[] = $t;
}
return $ret;
}
/**
* Add an AttributeConsumingService element to the metadata.
*
* @param \SAML2\XML\md\SPSSODescriptor $spDesc The SPSSODescriptor element.
* @param \SimpleSAML\Configuration $metadata The metadata.
* @return void
*/
private function addAttributeConsumingService(
SPSSODescriptor $spDesc,
Configuration $metadata
): void {
$attributes = $metadata->getArray('attributes', []);
$name = $metadata->getLocalizedString('name', null);
if ($name === null || count($attributes) == 0) {
// we cannot add an AttributeConsumingService without name and attributes
return;
}
$attributesrequired = $metadata->getArray('attributes.required', []);
/*
* Add an AttributeConsumingService element with information as name and description and list
* of requested attributes
*/
$attributeconsumer = new AttributeConsumingService();
$attributeconsumer->setIndex($metadata->getInteger('attributes.index', 0));
if ($metadata->hasValue('attributes.isDefault')) {
$attributeconsumer->setIsDefault($metadata->getBoolean('attributes.isDefault', false));
}
$attributeconsumer->setServiceName($name);
$attributeconsumer->setServiceDescription($metadata->getLocalizedString('description', []));
$nameFormat = $metadata->getString('attributes.NameFormat', Constants::NAMEFORMAT_UNSPECIFIED);
foreach ($attributes as $friendlyName => $attribute) {
$t = new RequestedAttribute();
$t->setName($attribute);
if (!is_int($friendlyName)) {
$t->setFriendlyName($friendlyName);
}
if ($nameFormat !== Constants::NAMEFORMAT_UNSPECIFIED) {
$t->setNameFormat($nameFormat);
}
if (in_array($attribute, $attributesrequired, true)) {
$t->setIsRequired(true);
}
$attributeconsumer->addRequestedAttribute($t);
}
$spDesc->addAttributeConsumingService($attributeconsumer);
}
/**
* Add a specific type of metadata to an entity.
*
* @param string $set The metadata set this metadata comes from.
* @param array $metadata The metadata.
* @return void
*/
public function addMetadata($set, $metadata)
{
assert(is_string($set));
assert(is_array($metadata));
$this->setExpiration($metadata);
switch ($set) {
case 'saml20-sp-remote':
$this->addMetadataSP20($metadata);
break;
case 'saml20-idp-remote':
$this->addMetadataIdP20($metadata);
break;
case 'shib13-sp-remote':
$this->addMetadataSP11($metadata);
break;
case 'shib13-idp-remote':
$this->addMetadataIdP11($metadata);
break;
case 'attributeauthority-remote':
$this->addAttributeAuthority($metadata);
break;
default:
Logger::warning('Unable to generate metadata for unknown type \'' . $set . '\'.');
}
}
/**
* Add SAML 2.0 SP metadata.
*
* @param array $metadata The metadata.
* @param array $protocols The protocols supported. Defaults to \SAML2\Constants::NS_SAMLP.
* @return void
*/
public function addMetadataSP20($metadata, $protocols = [Constants::NS_SAMLP])
{
assert(is_array($metadata));
assert(is_array($protocols));
assert(isset($metadata['entityid']));
assert(isset($metadata['metadata-set']));
$metadata = Configuration::loadFromArray($metadata, $metadata['entityid']);
$e = new SPSSODescriptor();
$e->setProtocolSupportEnumeration($protocols);
if ($metadata->hasValue('saml20.sign.assertion')) {
$e->setWantAssertionsSigned($metadata->getBoolean('saml20.sign.assertion'));
}
if ($metadata->hasValue('redirect.validate')) {
$e->setAuthnRequestsSigned($metadata->getBoolean('redirect.validate'));
} elseif ($metadata->hasValue('validate.authnrequest')) {
$e->setAuthnRequestsSigned($metadata->getBoolean('validate.authnrequest'));
}
$this->addExtensions($metadata, $e);
$this->addCertificate($e, $metadata);
$e->setSingleLogoutService(self::createEndpoints($metadata->getEndpoints('SingleLogoutService'), false));
$e->setNameIDFormat($metadata->getArrayizeString('NameIDFormat', []));
$endpoints = $metadata->getEndpoints('AssertionConsumerService');
foreach ($metadata->getArrayizeString('AssertionConsumerService.artifact', []) as $acs) {
$endpoints[] = [
'Binding' => Constants::BINDING_HTTP_ARTIFACT,
'Location' => $acs,
];
}
$e->setAssertionConsumerService(self::createEndpoints($endpoints, true));
$this->addAttributeConsumingService($e, $metadata);
$this->entityDescriptor->addRoleDescriptor($e);
foreach ($metadata->getArray('contacts', []) as $contact) {
if (array_key_exists('contactType', $contact) && array_key_exists('emailAddress', $contact)) {
$this->addContact($contact['contactType'], Utils\Config\Metadata::getContact($contact));
}
}
}
/**
* Add metadata of a SAML 2.0 identity provider.
*
* @param array $metadata The metadata.
* @return void
*/
public function addMetadataIdP20($metadata)
{
assert(is_array($metadata));
assert(isset($metadata['entityid']));
assert(isset($metadata['metadata-set']));
$metadata = Configuration::loadFromArray($metadata, $metadata['entityid']);
$e = new IDPSSODescriptor();
$e->setProtocolSupportEnumeration(array_merge($e->getProtocolSupportEnumeration(), [Constants::NS_SAMLP]));
if ($metadata->hasValue('sign.authnrequest')) {
$e->setWantAuthnRequestsSigned($metadata->getBoolean('sign.authnrequest'));
} elseif ($metadata->hasValue('redirect.sign')) {
$e->setWantAuthnRequestsSigned($metadata->getBoolean('redirect.sign'));
}
$this->addExtensions($metadata, $e);
$this->addCertificate($e, $metadata);
if ($metadata->hasValue('ArtifactResolutionService')) {
$e->setArtifactResolutionService(self::createEndpoints(
$metadata->getEndpoints('ArtifactResolutionService'),
true
));
}
$e->setSingleLogoutService(self::createEndpoints($metadata->getEndpoints('SingleLogoutService'), false));
$e->setNameIDFormat($metadata->getArrayizeString('NameIDFormat', []));
$e->setSingleSignOnService(self::createEndpoints($metadata->getEndpoints('SingleSignOnService'), false));
$this->entityDescriptor->addRoleDescriptor($e);
foreach ($metadata->getArray('contacts', []) as $contact) {
if (array_key_exists('contactType', $contact) && array_key_exists('emailAddress', $contact)) {
$this->addContact($contact['contactType'], Utils\Config\Metadata::getContact($contact));
}
}
}
/**
* Add metadata of a SAML 1.1 service provider.
*
* @param array $metadata The metadata.
* @return void
*/
public function addMetadataSP11($metadata)
{
assert(is_array($metadata));
assert(isset($metadata['entityid']));
assert(isset($metadata['metadata-set']));
$metadata = Configuration::loadFromArray($metadata, $metadata['entityid']);
$e = new SPSSODescriptor();
$e->setProtocolSupportEnumeration(
array_merge(
$e->getProtocolSupportEnumeration(),
['urn:oasis:names:tc:SAML:1.1:protocol']
)
);
$this->addCertificate($e, $metadata);
$e->setNameIDFormat($metadata->getArrayizeString('NameIDFormat', []));
$endpoints = $metadata->getEndpoints('AssertionConsumerService');
foreach ($metadata->getArrayizeString('AssertionConsumerService.artifact', []) as $acs) {
$endpoints[] = [
'Binding' => 'urn:oasis:names:tc:SAML:1.0:profiles:artifact-01',
'Location' => $acs,
];
}
$e->setAssertionConsumerService(self::createEndpoints($endpoints, true));
$this->addAttributeConsumingService($e, $metadata);
$this->entityDescriptor->addRoleDescriptor($e);
}
/**
* Add metadata of a SAML 1.1 identity provider.
*
* @param array $metadata The metadata.
* @return void
*/
public function addMetadataIdP11($metadata)
{
assert(is_array($metadata));
assert(isset($metadata['entityid']));
assert(isset($metadata['metadata-set']));
$metadata = Configuration::loadFromArray($metadata, $metadata['entityid']);
$e = new IDPSSODescriptor();
$e->setProtocolSupportEnumeration(
array_merge($e->getProtocolSupportEnumeration(), [
'urn:oasis:names:tc:SAML:1.1:protocol',
'urn:mace:shibboleth:1.0'
])
);
$this->addCertificate($e, $metadata);
$e->setNameIDFormat($metadata->getArrayizeString('NameIDFormat', []));
$e->setSingleSignOnService(self::createEndpoints($metadata->getEndpoints('SingleSignOnService'), false));
$this->entityDescriptor->addRoleDescriptor($e);
}
/**
* Add metadata of a SAML attribute authority.
*
* @param array $metadata The AttributeAuthorityDescriptor, in the format returned by
* \SimpleSAML\Metadata\SAMLParser.
* @return void
*/
public function addAttributeAuthority(array $metadata)
{
assert(isset($metadata['entityid']));
assert(isset($metadata['metadata-set']));
$metadata = Configuration::loadFromArray($metadata, $metadata['entityid']);
$e = new AttributeAuthorityDescriptor();
$e->setProtocolSupportEnumeration($metadata->getArray('protocols', [Constants::NS_SAMLP]));
$this->addExtensions($metadata, $e);
$this->addCertificate($e, $metadata);
$e->setAttributeService(self::createEndpoints($metadata->getEndpoints('AttributeService'), false));
$e->setAssertionIDRequestService(self::createEndpoints(
$metadata->getEndpoints('AssertionIDRequestService'),
false
));
$e->setNameIDFormat($metadata->getArrayizeString('NameIDFormat', []));
$this->entityDescriptor->addRoleDescriptor($e);
}
/**
* Add contact information.
*
* Accepts a contact type, and a contact array that must be previously sanitized.
*
* WARNING: This function will change its signature and no longer parse a 'name' element.
*
* @param string $type The type of contact. Deprecated.
* @param array $details The details about the contact.
*
* @return void
* @todo Change the signature to remove $type.
* @todo Remove the capability to pass a name and parse it inside the method.
*/
public function addContact($type, $details)
{
assert(is_string($type));
assert(is_array($details));
assert(in_array($type, ['technical', 'support', 'administrative', 'billing', 'other'], true));
// TODO: remove this check as soon as getContact() is called always before calling this function
$details = Utils\Config\Metadata::getContact($details);
$e = new \SAML2\XML\md\ContactPerson();
$e->setContactType($type);
if (!empty($details['attributes'])) {
$e->setContactPersonAttributes($details['attributes']);
}
if (isset($details['company'])) {
$e->setCompany($details['company']);
}
if (isset($details['givenName'])) {
$e->setGivenName($details['givenName']);
}
if (isset($details['surName'])) {
$e->setSurName($details['surName']);
}
if (isset($details['emailAddress'])) {
$eas = $details['emailAddress'];
if (!is_array($eas)) {
$eas = [$eas];
}
foreach ($eas as $ea) {
$e->addEmailAddress($ea);
}
}
if (isset($details['telephoneNumber'])) {
$tlfNrs = $details['telephoneNumber'];
if (!is_array($tlfNrs)) {
$tlfNrs = [$tlfNrs];
}
foreach ($tlfNrs as $tlfNr) {
$e->addTelephoneNumber($tlfNr);
}
}
$this->entityDescriptor->addContactPerson($e);
}
/**
* Add a KeyDescriptor with an X509 certificate.
*
* @param \SAML2\XML\md\RoleDescriptor $rd The RoleDescriptor the certificate should be added to.
* @param string $use The value of the 'use' attribute.
* @param string $x509data The certificate data.
* @return void
*/
private function addX509KeyDescriptor(RoleDescriptor $rd, string $use, string $x509data): void
{
assert(in_array($use, ['encryption', 'signing'], true));
$keyDescriptor = \SAML2\Utils::createKeyDescriptor($x509data);
$keyDescriptor->setUse($use);
$rd->addKeyDescriptor($keyDescriptor);
}
/**
* Add a certificate.
*
* Helper function for adding a certificate to the metadata.
*
* @param \SAML2\XML\md\RoleDescriptor $rd The RoleDescriptor the certificate should be added to.
* @param \SimpleSAML\Configuration $metadata The metadata of the entity.
* @return void
*/
private function addCertificate(RoleDescriptor $rd, Configuration $metadata): void
{
$keys = $metadata->getPublicKeys();
foreach ($keys as $key) {
if ($key['type'] !== 'X509Certificate') {
continue;
}
if (!isset($key['signing']) || $key['signing'] === true) {
$this->addX509KeyDescriptor($rd, 'signing', $key['X509Certificate']);
}
if (!isset($key['encryption']) || $key['encryption'] === true) {
$this->addX509KeyDescriptor($rd, 'encryption', $key['X509Certificate']);
}
}
if ($metadata->hasValue('https.certData')) {
$this->addX509KeyDescriptor($rd, 'signing', $metadata->getString('https.certData'));
}
}
}
|