summaryrefslogtreecommitdiffstats
path: root/lib/SAML2/XML/md/EntityDescriptor.php
blob: 89c7dcefc8ab082c24248195a46ced036b70681e (plain)
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
<?php

/**
 * Class representing SAML 2 EntityDescriptor element.
 *
 * @package simpleSAMLphp
 * @version $Id$
 */
class SAML2_XML_md_EntityDescriptor extends SAML2_SignedElementHelper {

	/**
	 * The entityID this EntityDescriptor represents.
	 *
	 * @var string
	 */
	public $entityID;


	/**
	 * The ID of this element.
	 *
	 * @var string|NULL
	 */
	public $ID;


	/**
	 * How long this element is valid, as a unix timestamp.
	 *
	 * @var int|NULL
	 */
	public $validUntil;


	/**
	 * The length of time this element can be cached, as string.
	 *
	 * @var string|NULL
	 */
	public $cacheDuration;


	/**
	 * Extensions on this element.
	 *
	 * Array of extension elements.
	 *
	 * @var array
	 */
	public $Extensions = array();


	/**
	 * Array with all roles for this entity.
	 *
	 * Array of SAML2_XML_md_RoleDescriptor objects (and subclasses of RoleDescriptor).
	 *
	 * @var array
	 */
	public $RoleDescriptor = array();


	/**
	 * AffiliationDescriptor of this entity.
	 *
	 * @var SAML2_XML_md_AffiliationDescriptor|NULL
	 */
	public $AffiliationDescriptor = NULL;


	/**
	 * Organization of this entity.
	 *
	 * @var SAML2_XML_md_Organization|NULL
	 */
	public $Organization = NULL;


	/**
	 * ContactPerson elements for this entity.
	 *
	 * @var array
	 */
	public $ContactPerson = array();


	/**
	 * AdditionalMetadataLocation elements for this entity.
	 *
	 * @var array
	 */
	public $AdditionalMetadataLocation = array();


	/**
	 * Initialize an EntitiyDescriptor.
	 *
	 * @param DOMElement|NULL $xml  The XML element we should load.
	 */
	public function __construct(DOMElement $xml = NULL) {
		parent::__construct($xml);

		if ($xml === NULL) {
			return;
		}

		if (!$xml->hasAttribute('entityID')) {
			throw new Exception('Missing required attribute entityID on EntityDescriptor.');
		}
		$this->entityID = $xml->getAttribute('entityID');

		if ($xml->hasAttribute('ID')) {
			$this->ID = $xml->getAttribute('ID');
		}
		if ($xml->hasAttribute('validUntil')) {
			$this->validUntil = SimpleSAML_Utilities::parseSAML2Time($xml->getAttribute('validUntil'));
		}
		if ($xml->hasAttribute('cacheDuration')) {
			$this->cacheDuration = $xml->getAttribute('cacheDuration');
		}

		$this->Extensions = SAML2_XML_md_Extensions::getList($xml);

		for ($node = $xml->firstChild; $node !== NULL; $node = $node->nextSibling) {
			if (!($node instanceof DOMElement)) {
				continue;
			}

			if ($node->namespaceURI !== SAML2_Const::NS_MD) {
				continue;
			}

			switch ($node->localName) {
			case 'RoleDescriptor':
				$this->RoleDescriptor[] = new SAML2_XML_md_UnknownRoleDescriptor($node);
				break;
			case 'IDPSSODescriptor':
				$this->RoleDescriptor[] = new SAML2_XML_md_IDPSSODescriptor($node);
				break;
			case 'SPSSODescriptor':
				$this->RoleDescriptor[] = new SAML2_XML_md_SPSSODescriptor($node);
				break;
			case 'AuthnAuthorityDescriptor':
				$this->RoleDescriptor[] = new SAML2_XML_md_AuthnAuthorityDescriptor($node);
				break;
			case 'AttributeAuthorityDescriptor':
				$this->RoleDescriptor[] = new SAML2_XML_md_AttributeAuthorityDescriptor($node);
				break;
			case 'PDPDescriptor':
				$this->RoleDescriptor[] = new SAML2_XML_md_PDPDescriptor($node);
				break;
			}
		}

		$affiliationDescriptor = SAML2_Utils::xpQuery($xml, './saml_metadata:AffiliationDescriptor');
		if (count($affiliationDescriptor) > 1) {
			throw new Exception('More than one AffiliationDescriptor in the entity.');
		} elseif (!empty($affiliationDescriptor)) {
			$this->AffiliationDescriptor = new SAML2_XML_md_AffiliationDescriptor($affiliationDescriptor[0]);
		}

		if (empty($this->RoleDescriptor) && is_null($this->AffiliationDescriptor)) {
			throw new Exception('Must have either one of the RoleDescriptors or an AffiliationDescriptor in EntityDescriptor.');
		} elseif (!empty($this->RoleDescriptor) && !is_null($this->AffiliationDescriptor)) {
			throw new Exception('AffiliationDescriptor cannot be combined with other RoleDescriptor elements in EntityDescriptor.');
		}

		$organization = SAML2_Utils::xpQuery($xml, './saml_metadata:Organization');
		if (count($organization) > 1) {
			throw new Exception('More than one Organization in the entity.');
		} elseif (!empty($organization)) {
			$this->Organization = new SAML2_XML_md_Organization($organization[0]);
		}

		foreach (SAML2_Utils::xpQuery($xml, './saml_metadata:ContactPerson') as $cp) {
			$this->ContactPerson[] = new SAML2_XML_md_ContactPerson($cp);
		}

		foreach (SAML2_Utils::xpQuery($xml, './saml_metadata:AdditionalMetadataLocation') as $aml) {
			$this->AdditionalMetadataLocation[] = new SAML2_XML_md_AdditionalMetadataLocation($aml);
		}
	}


	/**
	 * Create this EntityDescriptor.
	 *
	 * @param DOMElement|NULL $parent  The EntitiesDescriptor we should append this EntityDescriptor to.
	 */
	public function toXML(DOMElement $parent = NULL) {
		assert('is_string($this->entityID)');
		assert('is_null($this->ID) || is_string($this->ID)');
		assert('is_null($this->validUntil) || is_int($this->validUntil)');
		assert('is_null($this->cacheDuration) || is_string($this->cacheDuration)');
		assert('is_array($this->Extensions)');
		assert('is_array($this->RoleDescriptor)');
		assert('is_null($this->AffiliationDescriptor) || $this->AffiliationDescriptor instanceof SAML2_XML_md_AffiliationDescriptor');
		assert('is_null($this->Organization) || $this->Organization instanceof SAML2_XML_md_Organization');
		assert('is_array($this->ContactPerson)');
		assert('is_array($this->AdditionalMetadataLocation)');

		if ($parent === NULL) {
			$doc = new DOMDocument();
			$e = $doc->createElementNS(SAML2_Const::NS_MD, 'md:EntityDescriptor');
			$doc->appendChild($e);
		} else {
			$e = $parent->ownerDocument->createElementNS(SAML2_Const::NS_MD, 'md:EntityDescriptor');
			$parent->appendChild($e);
		}

		$e->setAttribute('entityID', $this->entityID);

		if (isset($this->ID)) {
			$e->setAttribute('ID', $this->ID);
		}

		if (isset($this->validUntil)) {
			$e->setAttribute('validUntil', gmdate('Y-m-d\TH:i:s\Z', $this->validUntil));
		}

		if (isset($this->cacheDuration)) {
			$e->setAttribute('cacheDuration', $this->cacheDuration);
		}

		SAML2_XML_md_Extensions::addList($e, $this->Extensions);

		foreach ($this->RoleDescriptor as $n) {
			$n->toXML($e);
		}

		if (isset($this->AffiliationDescriptor)) {
			$this->AffiliationDescriptor->toXML($e);
		}

		if (isset($this->Organization)) {
			$this->Organization->toXML($e);
		}

		foreach ($this->ContactPerson as $cp) {
			$cp->toXML($e);
		}

		foreach ($this->AdditionalMetadataLocation as $n) {
			$n->toXML($e);
		}

		$this->signElement($e, $e->firstChild);

		return $e;
	}

}