/lib/simplesamlphp-1.10.0/lib/SAML2/XML/md/EntityDescriptor.php

https://bitbucket.org/sahkoinenaanestys/sahkoinenaanestys · PHP · 252 lines · 132 code · 50 blank · 70 comment · 21 complexity · 5c9612ca7ee4372980135fd6ced729aa MD5 · raw file

  1. <?php
  2. /**
  3. * Class representing SAML 2 EntityDescriptor element.
  4. *
  5. * @package simpleSAMLphp
  6. * @version $Id$
  7. */
  8. class SAML2_XML_md_EntityDescriptor extends SAML2_SignedElementHelper {
  9. /**
  10. * The entityID this EntityDescriptor represents.
  11. *
  12. * @var string
  13. */
  14. public $entityID;
  15. /**
  16. * The ID of this element.
  17. *
  18. * @var string|NULL
  19. */
  20. public $ID;
  21. /**
  22. * How long this element is valid, as a unix timestamp.
  23. *
  24. * @var int|NULL
  25. */
  26. public $validUntil;
  27. /**
  28. * The length of time this element can be cached, as string.
  29. *
  30. * @var string|NULL
  31. */
  32. public $cacheDuration;
  33. /**
  34. * Extensions on this element.
  35. *
  36. * Array of extension elements.
  37. *
  38. * @var array
  39. */
  40. public $Extensions = array();
  41. /**
  42. * Array with all roles for this entity.
  43. *
  44. * Array of SAML2_XML_md_RoleDescriptor objects (and subclasses of RoleDescriptor).
  45. *
  46. * @var array
  47. */
  48. public $RoleDescriptor = array();
  49. /**
  50. * AffiliationDescriptor of this entity.
  51. *
  52. * @var SAML2_XML_md_AffiliationDescriptor|NULL
  53. */
  54. public $AffiliationDescriptor = NULL;
  55. /**
  56. * Organization of this entity.
  57. *
  58. * @var SAML2_XML_md_Organization|NULL
  59. */
  60. public $Organization = NULL;
  61. /**
  62. * ContactPerson elements for this entity.
  63. *
  64. * @var array
  65. */
  66. public $ContactPerson = array();
  67. /**
  68. * AdditionalMetadataLocation elements for this entity.
  69. *
  70. * @var array
  71. */
  72. public $AdditionalMetadataLocation = array();
  73. /**
  74. * Initialize an EntitiyDescriptor.
  75. *
  76. * @param DOMElement|NULL $xml The XML element we should load.
  77. */
  78. public function __construct(DOMElement $xml = NULL) {
  79. parent::__construct($xml);
  80. if ($xml === NULL) {
  81. return;
  82. }
  83. if (!$xml->hasAttribute('entityID')) {
  84. throw new Exception('Missing required attribute entityID on EntityDescriptor.');
  85. }
  86. $this->entityID = $xml->getAttribute('entityID');
  87. if ($xml->hasAttribute('ID')) {
  88. $this->ID = $xml->getAttribute('ID');
  89. }
  90. if ($xml->hasAttribute('validUntil')) {
  91. $this->validUntil = SimpleSAML_Utilities::parseSAML2Time($xml->getAttribute('validUntil'));
  92. }
  93. if ($xml->hasAttribute('cacheDuration')) {
  94. $this->cacheDuration = $xml->getAttribute('cacheDuration');
  95. }
  96. $this->Extensions = SAML2_XML_md_Extensions::getList($xml);
  97. for ($node = $xml->firstChild; $node !== NULL; $node = $node->nextSibling) {
  98. if (!($node instanceof DOMElement)) {
  99. continue;
  100. }
  101. if ($node->namespaceURI !== SAML2_Const::NS_MD) {
  102. continue;
  103. }
  104. switch ($node->localName) {
  105. case 'RoleDescriptor':
  106. $this->RoleDescriptor[] = new SAML2_XML_md_UnknownRoleDescriptor($node);
  107. break;
  108. case 'IDPSSODescriptor':
  109. $this->RoleDescriptor[] = new SAML2_XML_md_IDPSSODescriptor($node);
  110. break;
  111. case 'SPSSODescriptor':
  112. $this->RoleDescriptor[] = new SAML2_XML_md_SPSSODescriptor($node);
  113. break;
  114. case 'AuthnAuthorityDescriptor':
  115. $this->RoleDescriptor[] = new SAML2_XML_md_AuthnAuthorityDescriptor($node);
  116. break;
  117. case 'AttributeAuthorityDescriptor':
  118. $this->RoleDescriptor[] = new SAML2_XML_md_AttributeAuthorityDescriptor($node);
  119. break;
  120. case 'PDPDescriptor':
  121. $this->RoleDescriptor[] = new SAML2_XML_md_PDPDescriptor($node);
  122. break;
  123. }
  124. }
  125. $affiliationDescriptor = SAML2_Utils::xpQuery($xml, './saml_metadata:AffiliationDescriptor');
  126. if (count($affiliationDescriptor) > 1) {
  127. throw new Exception('More than one AffiliationDescriptor in the entity.');
  128. } elseif (!empty($affiliationDescriptor)) {
  129. $this->AffiliationDescriptor = new SAML2_XML_md_AffiliationDescriptor($affiliationDescriptor[0]);
  130. }
  131. if (empty($this->RoleDescriptor) && is_null($this->AffiliationDescriptor)) {
  132. throw new Exception('Must have either one of the RoleDescriptors or an AffiliationDescriptor in EntityDescriptor.');
  133. } elseif (!empty($this->RoleDescriptor) && !is_null($this->AffiliationDescriptor)) {
  134. throw new Exception('AffiliationDescriptor cannot be combined with other RoleDescriptor elements in EntityDescriptor.');
  135. }
  136. $organization = SAML2_Utils::xpQuery($xml, './saml_metadata:Organization');
  137. if (count($organization) > 1) {
  138. throw new Exception('More than one Organization in the entity.');
  139. } elseif (!empty($organization)) {
  140. $this->Organization = new SAML2_XML_md_Organization($organization[0]);
  141. }
  142. foreach (SAML2_Utils::xpQuery($xml, './saml_metadata:ContactPerson') as $cp) {
  143. $this->ContactPerson[] = new SAML2_XML_md_ContactPerson($cp);
  144. }
  145. foreach (SAML2_Utils::xpQuery($xml, './saml_metadata:AdditionalMetadataLocation') as $aml) {
  146. $this->AdditionalMetadataLocation[] = new SAML2_XML_md_AdditionalMetadataLocation($aml);
  147. }
  148. }
  149. /**
  150. * Create this EntityDescriptor.
  151. *
  152. * @param DOMElement|NULL $parent The EntitiesDescriptor we should append this EntityDescriptor to.
  153. */
  154. public function toXML(DOMElement $parent = NULL) {
  155. assert('is_string($this->entityID)');
  156. assert('is_null($this->ID) || is_string($this->ID)');
  157. assert('is_null($this->validUntil) || is_int($this->validUntil)');
  158. assert('is_null($this->cacheDuration) || is_string($this->cacheDuration)');
  159. assert('is_array($this->Extensions)');
  160. assert('is_array($this->RoleDescriptor)');
  161. assert('is_null($this->AffiliationDescriptor) || $this->AffiliationDescriptor instanceof SAML2_XML_md_AffiliationDescriptor');
  162. assert('is_null($this->Organization) || $this->Organization instanceof SAML2_XML_md_Organization');
  163. assert('is_array($this->ContactPerson)');
  164. assert('is_array($this->AdditionalMetadataLocation)');
  165. if ($parent === NULL) {
  166. $doc = new DOMDocument();
  167. $e = $doc->createElementNS(SAML2_Const::NS_MD, 'md:EntityDescriptor');
  168. $doc->appendChild($e);
  169. } else {
  170. $e = $parent->ownerDocument->createElementNS(SAML2_Const::NS_MD, 'md:EntityDescriptor');
  171. $parent->appendChild($e);
  172. }
  173. $e->setAttribute('entityID', $this->entityID);
  174. if (isset($this->ID)) {
  175. $e->setAttribute('ID', $this->ID);
  176. }
  177. if (isset($this->validUntil)) {
  178. $e->setAttribute('validUntil', gmdate('Y-m-d\TH:i:s\Z', $this->validUntil));
  179. }
  180. if (isset($this->cacheDuration)) {
  181. $e->setAttribute('cacheDuration', $this->cacheDuration);
  182. }
  183. SAML2_XML_md_Extensions::addList($e, $this->Extensions);
  184. foreach ($this->RoleDescriptor as $n) {
  185. $n->toXML($e);
  186. }
  187. if (isset($this->AffiliationDescriptor)) {
  188. $this->AffiliationDescriptor->toXML($e);
  189. }
  190. if (isset($this->Organization)) {
  191. $this->Organization->toXML($e);
  192. }
  193. foreach ($this->ContactPerson as $cp) {
  194. $cp->toXML($e);
  195. }
  196. foreach ($this->AdditionalMetadataLocation as $n) {
  197. $n->toXML($e);
  198. }
  199. $this->signElement($e, $e->firstChild);
  200. return $e;
  201. }
  202. }