Passed
Branch assertions (18e068)
by Tim
03:23
created

EntityDescriptor::getOrganization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SAML2\XML\md;
6
7
use SAML2\Constants;
8
use SAML2\DOMDocumentFactory;
9
use SAML2\SignedElementHelper;
10
use SAML2\Utils;
11
use SAML2\XML\Chunk;
12
13
/**
14
 * Class representing SAML 2 EntityDescriptor element.
15
 *
16
 * @package SimpleSAMLphp
17
 */
18
class EntityDescriptor extends SignedElementHelper
19
{
20
    /**
21
     * The entityID this EntityDescriptor represents.
22
     *
23
     * @var string
24
     */
25
    public $entityID;
26
27
    /**
28
     * The ID of this element.
29
     *
30
     * @var string|null
31
     */
32
    public $ID = null;
33
34
    /**
35
     * How long this element is valid, as a unix timestamp.
36
     *
37
     * @var int|null
38
     */
39
    public $validUntil = null;
40
41
    /**
42
     * The length of time this element can be cached, as string.
43
     *
44
     * @var string|null
45
     */
46
    public $cacheDuration = null;
47
48
    /**
49
     * Extensions on this element.
50
     *
51
     * Array of extension elements.
52
     *
53
     * @var array
54
     */
55
    public $Extensions = [];
56
57
    /**
58
     * Array with all roles for this entity.
59
     *
60
     * Array of \SAML2\XML\md\RoleDescriptor objects (and subclasses of RoleDescriptor).
61
     *
62
     * @var (\SAML2\XML\md\UnknownRoleDescriptor|\SAML2\XML\md\IDPSSODescriptor|\SAML2\XML\md\SPSSODescriptor|\SAML2\XML\md\AuthnAuthorityDescriptor|\SAML2\XML\md\AttributeAuthorityDescriptor|\SAML2\XML\md\PDPDescriptor)[]
63
     */
64
    public $RoleDescriptor = [];
65
66
    /**
67
     * AffiliationDescriptor of this entity.
68
     *
69
     * @var \SAML2\XML\md\AffiliationDescriptor|null
70
     */
71
    public $AffiliationDescriptor = null;
72
73
    /**
74
     * Organization of this entity.
75
     *
76
     * @var \SAML2\XML\md\Organization|null
77
     */
78
    public $Organization = null;
79
80
    /**
81
     * ContactPerson elements for this entity.
82
     *
83
     * @var \SAML2\XML\md\ContactPerson[]
84
     */
85
    public $ContactPerson = [];
86
87
    /**
88
     * AdditionalMetadataLocation elements for this entity.
89
     *
90
     * @var \SAML2\XML\md\AdditionalMetadataLocation[]
91
     */
92
    public $AdditionalMetadataLocation = [];
93
94
95
    /**
96
     * Initialize an EntitiyDescriptor.
97
     *
98
     * @param \DOMElement|null $xml The XML element we should load.
99
     * @throws \Exception
100
     */
101
    public function __construct(\DOMElement $xml = null)
102
    {
103
        parent::__construct($xml);
104
105
        if ($xml === null) {
106
            return;
107
        }
108
109
        if (!$xml->hasAttribute('entityID')) {
110
            throw new \Exception('Missing required attribute entityID on EntityDescriptor.');
111
        }
112
        $this->setEntityID($xml->getAttribute('entityID'));
113
114
        if ($xml->hasAttribute('ID')) {
115
            $this->setID($xml->getAttribute('ID'));
116
        }
117
        if ($xml->hasAttribute('validUntil')) {
118
            $this->setValidUntil(Utils::xsDateTimeToTimestamp($xml->getAttribute('validUntil')));
119
        }
120
        if ($xml->hasAttribute('cacheDuration')) {
121
            $this->setCacheDuration($xml->getAttribute('cacheDuration'));
122
        }
123
124
        $this->setExtensions(Extensions::getList($xml));
125
126
        for ($node = $xml->firstChild; $node !== null; $node = $node->nextSibling) {
127
            if (!($node instanceof \DOMElement)) {
128
                continue;
129
            }
130
131
            if ($node->namespaceURI !== Constants::NS_MD) {
132
                continue;
133
            }
134
135
            switch ($node->localName) {
136
                case 'RoleDescriptor':
137
                    $this->addRoleDescriptor(new UnknownRoleDescriptor($node));
138
                    break;
139
                case 'IDPSSODescriptor':
140
                    $this->addRoleDescriptor(new IDPSSODescriptor($node));
141
                    break;
142
                case 'SPSSODescriptor':
143
                    $this->addRoleDescriptor(new SPSSODescriptor($node));
144
                    break;
145
                case 'AuthnAuthorityDescriptor':
146
                    $this->addRoleDescriptor(new AuthnAuthorityDescriptor($node));
147
                    break;
148
                case 'AttributeAuthorityDescriptor':
149
                    $this->addRoleDescriptor(new AttributeAuthorityDescriptor($node));
150
                    break;
151
                case 'PDPDescriptor':
152
                    $this->addRoleDescriptor(new PDPDescriptor($node));
153
                    break;
154
            }
155
        }
156
157
        $affiliationDescriptor = Utils::xpQuery($xml, './saml_metadata:AffiliationDescriptor');
158
        if (count($affiliationDescriptor) > 1) {
159
            throw new \Exception('More than one AffiliationDescriptor in the entity.');
160
        } elseif (!empty($affiliationDescriptor)) {
161
            $this->setAffiliationDescriptor(new AffiliationDescriptor($affiliationDescriptor[0]));
162
        }
163
164
        $roleDescriptor = $this->getRoleDescriptor();
165
        if (empty($roleDescriptor) && is_null($this->getAffiliationDescriptor())) {
166
            throw new \Exception('Must have either one of the RoleDescriptors or an AffiliationDescriptor in EntityDescriptor.');
167
        } elseif (!empty($roleDescriptor) && !is_null($this->getAffiliationDescriptor())) {
168
            throw new \Exception('AffiliationDescriptor cannot be combined with other RoleDescriptor elements in EntityDescriptor.');
169
        }
170
171
        $organization = Utils::xpQuery($xml, './saml_metadata:Organization');
172
        if (count($organization) > 1) {
173
            throw new \Exception('More than one Organization in the entity.');
174
        } elseif (!empty($organization)) {
175
            $this->setOrganization(new Organization($organization[0]));
176
        }
177
178
        foreach (Utils::xpQuery($xml, './saml_metadata:ContactPerson') as $cp) {
179
            $this->addContactPerson(new ContactPerson($cp));
180
        }
181
182
        foreach (Utils::xpQuery($xml, './saml_metadata:AdditionalMetadataLocation') as $aml) {
183
            $this->addAdditionalMetadataLocation(new AdditionalMetadataLocation($aml));
184
        }
185
    }
186
187
188
    /**
189
     * Collect the value of the entityID-property
190
     * @return string
191
     */
192
    public function getEntityID() : string
193
    {
194
        return $this->entityID;
195
    }
196
197
198
    /**
199
     * Set the value of the entityID-property
200
     * @param string|null $entityId
201
     * @return void
202
     */
203
    public function setEntityID(string $entityId = null)
204
    {
205
        $this->entityID = $entityId;
206
    }
207
208
209
    /**
210
     * Collect the value of the ID-property
211
     * @return string|null
212
     */
213
    public function getID()
214
    {
215
        return $this->ID;
216
    }
217
218
219
    /**
220
     * Set the value of the ID-property
221
     * @param string|null $Id
222
     * @return void
223
     */
224
    public function setID(string $Id = null)
225
    {
226
        $this->ID = $Id;
227
    }
228
229
230
    /**
231
     * Collect the value of the validUntil-property
232
     * @return int|null
233
     */
234
    public function getValidUntil()
235
    {
236
        return $this->validUntil;
237
    }
238
239
240
    /**
241
     * Set the value of the validUntil-property
242
     * @param int|null $validUntil
243
     * @return void
244
     */
245
    public function setValidUntil(int $validUntil = null)
246
    {
247
        $this->validUntil = $validUntil;
248
    }
249
250
251
    /**
252
     * Collect the value of the cacheDuration-property
253
     * @return string|null
254
     */
255
    public function getCacheDuration()
256
    {
257
        return $this->cacheDuration;
258
    }
259
260
261
    /**
262
     * Set the value of the cacheDuration-property
263
     * @param string|null $cacheDuration
264
     * @return void
265
     */
266
    public function setCacheDuration(string $cacheDuration = null)
267
    {
268
        $this->cacheDuration = $cacheDuration;
269
    }
270
271
272
    /**
273
     * Collect the value of the Extensions-property
274
     * @return \SAML2\XML\Chunk[]
275
     */
276
    public function getExtensions() : array
277
    {
278
        return $this->Extensions;
279
    }
280
281
282
    /**
283
     * Set the value of the Extensions-property
284
     * @param array $extensions
285
     * @return void
286
     */
287
    public function setExtensions(array $extensions)
288
    {
289
        $this->Extensions = $extensions;
290
    }
291
292
293
    /**
294
     * Add an Extension.
295
     *
296
     * @param \SAML2\XML\Chunk $extensions The Extensions
297
     * @return void
298
     */
299
    public function addExtension(Extensions $extension)
300
    {
301
        $this->Extensions[] = $extension;
302
    }
303
304
305
    /**
306
     * Collect the value of the RoleDescriptor-property
307
     * @return \SAML2\XML\md\RoleDescriptor[]
308
     */
309
    public function getRoleDescriptor() : array
310
    {
311
        return $this->RoleDescriptor;
312
    }
313
314
315
    /**
316
     * Set the value of the RoleDescriptor-property
317
     * @param array $roleDescriptor
318
     * @return void
319
     */
320
    public function setRoleDescriptor(array $roleDescriptor)
321
    {
322
        $this->RoleDescriptor = $roleDescriptor;
323
    }
324
325
326
    /**
327
     * Add the value to the RoleDescriptor-property
328
     * @param \SAML2\XML\md\RoleDescriptor $roleDescriptor
329
     * @return void
330
     */
331
    public function addRoleDescriptor(RoleDescriptor $roleDescriptor)
332
    {
333
        $this->RoleDescriptor[] = $roleDescriptor;
334
    }
335
336
337
    /**
338
     * Collect the value of the AffiliationDescriptor-property
339
     * @return \SAML2\XML\md\AffiliationDescriptor|null
340
     */
341
    public function getAffiliationDescriptor()
342
    {
343
        return $this->AffiliationDescriptor;
344
    }
345
346
347
    /**
348
     * Set the value of the AffliationDescriptor-property
349
     * @param \SAML2\XML\md\AffiliationDescriptor|null $affiliationDescriptor
350
     * @return void
351
     */
352
    public function setAffiliationDescriptor(AffiliationDescriptor $affiliationDescriptor = null)
353
    {
354
        $this->AffiliationDescriptor = $affiliationDescriptor;
355
    }
356
357
358
    /**
359
     * Collect the value of the Organization-property
360
     * @return \SAML2\XML\md\Organization|null
361
     */
362
    public function getOrganization()
363
    {
364
        return $this->Organization;
365
    }
366
367
368
    /**
369
     * Set the value of the Organization-property
370
     * @param \SAML2\XML\md\Organization|null $organization
371
     * @return void
372
     */
373
    public function setOrganization(Organization $organization = null)
374
    {
375
        $this->Organization = $organization;
376
    }
377
378
379
    /**
380
     * Collect the value of the ContactPerson-property
381
     * @return \SAML2\XML\md\ContactPerson[]
382
     */
383
    public function getContactPerson() : array
384
    {
385
        return $this->ContactPerson;
386
    }
387
388
389
    /**
390
     * Set the value of the ContactPerson-property
391
     * @param array $contactPerson
392
     * @return void
393
     */
394
    public function setContactPerson(array $contactPerson)
395
    {
396
        $this->ContactPerson = $contactPerson;
397
    }
398
399
400
    /**
401
     * Add the value to the ContactPerson-property
402
     * @param \SAML2\XML\md\ContactPerson $contactPerson
403
     * @return void
404
     */
405
    public function addContactPerson(ContactPerson $contactPerson)
406
    {
407
        $this->ContactPerson[] = $contactPerson;
408
    }
409
410
411
    /**
412
     * Collect the value of the AdditionalMetadataLocation-property
413
     * @return \SAML2\XML\md\AdditionalMetadataLocation[]
414
     */
415
    public function getAdditionalMetadataLocation() : array
416
    {
417
        return $this->AdditionalMetadataLocation;
418
    }
419
420
421
    /**
422
     * Set the value of the AdditionalMetadataLocation-property
423
     * @param array $additionalMetadataLocation
424
     * @return void
425
     */
426
    public function setAdditionalMetadataLocation(array $additionalMetadataLocation)
427
    {
428
        $this->AdditionalMetadataLocation = $additionalMetadataLocation;
429
    }
430
431
432
    /**
433
     * Add the value to the AdditionalMetadataLocation-property
434
     * @param AdditionalMetadataLocation $additionalMetadataLocation
435
     * @return void
436
     */
437
    public function addAdditionalMetadataLocation(AdditionalMetadataLocation $additionalMetadataLocation)
438
    {
439
        $this->AdditionalMetadataLocation[] = $additionalMetadataLocation;
440
    }
441
442
443
    /**
444
     * Create this EntityDescriptor.
445
     *
446
     * @param \DOMElement|null $parent The EntitiesDescriptor we should append this EntityDescriptor to.
447
     * @return \DOMElement
448
     */
449
    public function toXML(\DOMElement $parent = null) : \DOMElement
450
    {
451
        if ($parent === null) {
452
            $doc = DOMDocumentFactory::create();
453
            $e = $doc->createElementNS(Constants::NS_MD, 'md:EntityDescriptor');
454
            $doc->appendChild($e);
455
        } else {
456
            $e = $parent->ownerDocument->createElementNS(Constants::NS_MD, 'md:EntityDescriptor');
457
            $parent->appendChild($e);
458
        }
459
460
        $e->setAttribute('entityID', $this->getEntityID());
461
462
        if ($this->getID() !== null) {
463
            $e->setAttribute('ID', $this->getID());
464
        }
465
466
        if ($this->getValidUntil() !== null) {
467
            $e->setAttribute('validUntil', gmdate('Y-m-d\TH:i:s\Z', $this->getValidUntil()));
468
        }
469
470
        if ($this->getCacheDuration() !== null) {
471
            $e->setAttribute('cacheDuration', $this->getCacheDuration());
472
        }
473
474
        Extensions::addList($e, $this->getExtensions());
475
476
        /** @var \SAML2\XML\md\UnknownRoleDescriptor|\SAML2\XML\md\IDPSSODescriptor|\SAML2\XML\md\SPSSODescriptor|\SAML2\XML\md\AuthnAuthorityDescriptor|\SAML2\XML\md\AttributeAuthorityDescriptor|\SAML2\XML\md\PDPDescriptor $n */
477
        foreach ($this->getRoleDescriptor() as $n) {
478
            $n->toXML($e);
479
        }
480
481
        if ($this->getAffiliationDescriptor() !== null) {
482
            $this->getAffiliationDescriptor()->toXML($e);
483
        }
484
485
        if ($this->getOrganization() !== null) {
486
            $this->getOrganization()->toXML($e);
487
        }
488
489
        foreach ($this->getContactPerson() as $cp) {
490
            $cp->toXML($e);
491
        }
492
493
        foreach ($this->getAdditionalMetadataLocation() as $n) {
494
            $n->toXML($e);
495
        }
496
497
        $this->signElement($e, $e->firstChild);
498
499
        return $e;
500
    }
501
}
502