ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
EntityDescriptor.php
Go to the documentation of this file.
1 <?php
2 
3 namespace SAML2\XML\md;
4 
8 use SAML2\Utils;
9 
16 {
22  public $entityID;
23 
29  public $ID;
30 
36  public $validUntil;
37 
44 
52  public $Extensions = array();
53 
61  public $RoleDescriptor = array();
62 
68  public $AffiliationDescriptor = null;
69 
75  public $Organization = null;
76 
82  public $ContactPerson = array();
83 
89  public $AdditionalMetadataLocation = array();
90 
97  public function __construct(\DOMElement $xml = null)
98  {
99  parent::__construct($xml);
100 
101  if ($xml === null) {
102  return;
103  }
104 
105  if (!$xml->hasAttribute('entityID')) {
106  throw new \Exception('Missing required attribute entityID on EntityDescriptor.');
107  }
108  $this->entityID = $xml->getAttribute('entityID');
109 
110  if ($xml->hasAttribute('ID')) {
111  $this->ID = $xml->getAttribute('ID');
112  }
113  if ($xml->hasAttribute('validUntil')) {
114  $this->validUntil = Utils::xsDateTimeToTimestamp($xml->getAttribute('validUntil'));
115  }
116  if ($xml->hasAttribute('cacheDuration')) {
117  $this->cacheDuration = $xml->getAttribute('cacheDuration');
118  }
119 
121 
122  for ($node = $xml->firstChild; $node !== null; $node = $node->nextSibling) {
123  if (!($node instanceof \DOMElement)) {
124  continue;
125  }
126 
127  if ($node->namespaceURI !== Constants::NS_MD) {
128  continue;
129  }
130 
131  switch ($node->localName) {
132  case 'RoleDescriptor':
133  $this->RoleDescriptor[] = new UnknownRoleDescriptor($node);
134  break;
135  case 'IDPSSODescriptor':
136  $this->RoleDescriptor[] = new IDPSSODescriptor($node);
137  break;
138  case 'SPSSODescriptor':
139  $this->RoleDescriptor[] = new SPSSODescriptor($node);
140  break;
141  case 'AuthnAuthorityDescriptor':
142  $this->RoleDescriptor[] = new AuthnAuthorityDescriptor($node);
143  break;
144  case 'AttributeAuthorityDescriptor':
145  $this->RoleDescriptor[] = new AttributeAuthorityDescriptor($node);
146  break;
147  case 'PDPDescriptor':
148  $this->RoleDescriptor[] = new PDPDescriptor($node);
149  break;
150  }
151  }
152 
153  $affiliationDescriptor = Utils::xpQuery($xml, './saml_metadata:AffiliationDescriptor');
154  if (count($affiliationDescriptor) > 1) {
155  throw new \Exception('More than one AffiliationDescriptor in the entity.');
156  } elseif (!empty($affiliationDescriptor)) {
157  $this->AffiliationDescriptor = new AffiliationDescriptor($affiliationDescriptor[0]);
158  }
159 
160  if (empty($this->RoleDescriptor) && is_null($this->AffiliationDescriptor)) {
161  throw new \Exception('Must have either one of the RoleDescriptors or an AffiliationDescriptor in EntityDescriptor.');
162  } elseif (!empty($this->RoleDescriptor) && !is_null($this->AffiliationDescriptor)) {
163  throw new \Exception('AffiliationDescriptor cannot be combined with other RoleDescriptor elements in EntityDescriptor.');
164  }
165 
166  $organization = Utils::xpQuery($xml, './saml_metadata:Organization');
167  if (count($organization) > 1) {
168  throw new \Exception('More than one Organization in the entity.');
169  } elseif (!empty($organization)) {
170  $this->Organization = new Organization($organization[0]);
171  }
172 
173  foreach (Utils::xpQuery($xml, './saml_metadata:ContactPerson') as $cp) {
174  $this->ContactPerson[] = new ContactPerson($cp);
175  }
176 
177  foreach (Utils::xpQuery($xml, './saml_metadata:AdditionalMetadataLocation') as $aml) {
179  }
180  }
181 
188  public function toXML(\DOMElement $parent = null)
189  {
190  assert(is_string($this->entityID));
191  assert(is_null($this->ID) || is_string($this->ID));
192  assert(is_null($this->validUntil) || is_int($this->validUntil));
193  assert(is_null($this->cacheDuration) || is_string($this->cacheDuration));
194  assert(is_array($this->Extensions));
195  assert(is_array($this->RoleDescriptor));
196  assert(is_null($this->AffiliationDescriptor) || $this->AffiliationDescriptor instanceof AffiliationDescriptor);
197  assert(is_null($this->Organization) || $this->Organization instanceof Organization);
198  assert(is_array($this->ContactPerson));
199  assert(is_array($this->AdditionalMetadataLocation));
200 
201  if ($parent === null) {
203  $e = $doc->createElementNS(Constants::NS_MD, 'md:EntityDescriptor');
204  $doc->appendChild($e);
205  } else {
206  $e = $parent->ownerDocument->createElementNS(Constants::NS_MD, 'md:EntityDescriptor');
207  $parent->appendChild($e);
208  }
209 
210  $e->setAttribute('entityID', $this->entityID);
211 
212  if (isset($this->ID)) {
213  $e->setAttribute('ID', $this->ID);
214  }
215 
216  if (isset($this->validUntil)) {
217  $e->setAttribute('validUntil', gmdate('Y-m-d\TH:i:s\Z', $this->validUntil));
218  }
219 
220  if (isset($this->cacheDuration)) {
221  $e->setAttribute('cacheDuration', $this->cacheDuration);
222  }
223 
224  Extensions::addList($e, $this->Extensions);
225 
227  foreach ($this->RoleDescriptor as $n) {
228  $n->toXML($e);
229  }
230 
231  if (isset($this->AffiliationDescriptor)) {
232  $this->AffiliationDescriptor->toXML($e);
233  }
234 
235  if (isset($this->Organization)) {
236  $this->Organization->toXML($e);
237  }
238 
239  foreach ($this->ContactPerson as $cp) {
240  $cp->toXML($e);
241  }
242 
243  foreach ($this->AdditionalMetadataLocation as $n) {
244  $n->toXML($e);
245  }
246 
247  $this->signElement($e, $e->firstChild);
248 
249  return $e;
250  }
251 }
__construct(\DOMElement $xml=null)
Initialize an EntitiyDescriptor.
$n
Definition: RandomTest.php:85
const NS_MD
The namespace for the SAML 2 metadata.
Definition: Constants.php:230
static xpQuery(\DOMNode $node, $query)
Do an XPath query on an XML node.
Definition: Utils.php:191
signElement(\DOMElement $root, \DOMElement $insertBefore=null)
Sign the given XML element.
static xsDateTimeToTimestamp($time)
This function converts a SAML2 timestamp on the form yyyy-mm-ddThh:mm:ss(.s+)?Z to a UNIX timestamp...
Definition: Utils.php:721
static addList(\DOMElement $parent, array $extensions)
Add a list of Extensions to the given element.
Definition: Extensions.php:70
static getList(\DOMElement $parent)
Get a list of Extensions in the given element.
Definition: Extensions.php:27