summaryrefslogtreecommitdiffstats
path: root/lib/SAML2/XML/md/RoleDescriptor.php
blob: 346d34cc1376419d54f29c1c775dc212e24b93d0 (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
<?php

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

	/**
	 * The name of this descriptor element.
	 *
	 * @var string
	 */
	private $elementName;


	/**
	 * 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;


	/**
	 * List of supported protocols.
	 *
	 * @var array
	 */
	public $protocolSupportEnumeration = array();


	/**
	 * Error URL for this role.
	 *
	 * @var string|NULL
	 */
	public $errorURL;


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


	/**
	 * KeyDescriptor elements.
	 *
	 * Array of SAML2_XML_md_KeyDescriptor elements.
	 *
	 * @var array
	 */
	public $KeyDescriptor = array();


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


	/**
	 * ContactPerson elements for this role.
	 *
	 * Array of SAML2_XML_md_ContactPerson objects.
	 *
	 * @var array
	 */
	public $ContactPerson = array();


	/**
	 * Initialize a RoleDescriptor.
	 *
	 * @param string $elementName  The name of this element.
	 * @param DOMElement|NULL $xml  The XML element we should load.
	 */
	protected function __construct($elementName, DOMElement $xml = NULL) {
		assert('is_string($elementName)');

		parent::__construct($xml);
		$this->elementName = $elementName;

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

		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');
		}

		if (!$xml->hasAttribute('protocolSupportEnumeration')) {
			throw new Exception('Missing protocolSupportEnumeration attribute on ' . $xml->localName);
		}
		$this->protocolSupportEnumeration = preg_split('/[\s]+/', $xml->getAttribute('protocolSupportEnumeration'));

		if ($xml->hasAttribute('errorURL')) {
			$this->errorURL = $xml->getAttribute('errorURL');
		}


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

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

		$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->contactPersons[] = new SAML2_XML_md_ContactPerson($cp);
		}
	}


	/**
	 * Add this RoleDescriptor to an EntityDescriptor.
	 *
	 * @param DOMElement $parent  The EntityDescriptor we should append this endpoint to.
	 * @param string $name  The name of the element we should create.
	 */
	protected function toXML(DOMElement $parent) {
		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->protocolSupportEnumeration)');
		assert('is_null($this->errorURL) || is_string($this->errorURL)');
		assert('is_array($this->Extensions)');
		assert('is_array($this->KeyDescriptor)');
		assert('is_null($this->Organization) || $this->Organization instanceof SAML2_XML_md_Organization');
		assert('is_array($this->ContactPerson)');

		$e = $parent->ownerDocument->createElementNS(SAML2_Const::NS_MD, $this->elementName);
		$parent->appendChild($e);

		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);
		}

		$e->setAttribute('protocolSupportEnumeration', implode(' ', $this->protocolSupportEnumeration));

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


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

		foreach ($this->KeyDescriptor as $kd) {
			$kd->toXML($e);
		}

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

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

		return $e;
	}

}