SubjectConfirmation::getSubjectConfirmationData()   A
last analyzed

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 SimpleSAML\SAML2\XML\ecp;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Assert\Assert as SAMLAssert;
10
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
11
use SimpleSAML\SAML2\XML\saml\SubjectConfirmationData;
12
use SimpleSAML\SOAP\Constants as C;
13
use SimpleSAML\XML\Exception\InvalidDOMElementException;
14
use SimpleSAML\XML\Exception\MissingAttributeException;
15
use SimpleSAML\XML\Exception\TooManyElementsException;
16
use SimpleSAML\XML\SchemaValidatableElementInterface;
17
use SimpleSAML\XML\SchemaValidatableElementTrait;
18
19
/**
20
 * Class representing the ECP SubjectConfirmation element.
21
 *
22
 * @package simplesamlphp/saml2
23
 */
24
final class SubjectConfirmation extends AbstractEcpElement implements SchemaValidatableElementInterface
25
{
26
    use SchemaValidatableElementTrait;
27
28
29
    /**
30
     * Create a ECP SubjectConfirmation element.
31
     *
32
     * @param string $method
33
     * @param \SimpleSAML\SAML2\XML\saml\SubjectConfirmationData|null $subjectConfirmationData
34
     */
35
    public function __construct(
36
        protected string $method,
37
        protected ?SubjectConfirmationData $subjectConfirmationData = null,
38
    ) {
39
        SAMLAssert::validURI($method);
40
    }
41
42
43
    /**
44
     * Collect the value of the method-property
45
     *
46
     * @return string
47
     */
48
    public function getMethod(): string
49
    {
50
        return $this->method;
51
    }
52
53
54
    /**
55
     * Collect the value of the subjectConfirmationData-property
56
     *
57
     * @return \SimpleSAML\SAML2\XML\saml\SubjectConfirmationData|null
58
     */
59
    public function getSubjectConfirmationData(): ?SubjectConfirmationData
60
    {
61
        return $this->subjectConfirmationData;
62
    }
63
64
65
    /**
66
     * Convert XML into a SubjectConfirmation
67
     *
68
     * @param \DOMElement $xml The XML element we should load
69
     * @return static
70
     *
71
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
72
     *   if the qualified name of the supplied element is wrong
73
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
74
     *   if the supplied element is missing any of the mandatory attributes
75
     */
76
    public static function fromXML(DOMElement $xml): static
77
    {
78
        Assert::same($xml->localName, 'SubjectConfirmation', InvalidDOMElementException::class);
79
        Assert::same($xml->namespaceURI, SubjectConfirmation::NS, InvalidDOMElementException::class);
80
81
        // Assert required attributes
82
        Assert::true(
83
            $xml->hasAttributeNS(C::NS_SOAP_ENV_11, 'actor'),
84
            'Missing env:actor attribute in <ecp:SubjectConfirmation>.',
85
            MissingAttributeException::class,
86
        );
87
        Assert::true(
88
            $xml->hasAttributeNS(C::NS_SOAP_ENV_11, 'mustUnderstand'),
89
            'Missing env:mustUnderstand attribute in <ecp:SubjectConfirmation>.',
90
            MissingAttributeException::class,
91
        );
92
93
        $mustUnderstand = $xml->getAttributeNS(C::NS_SOAP_ENV_11, 'mustUnderstand');
94
        Assert::same(
95
            $mustUnderstand,
96
            '1',
97
            'Invalid value of env:mustUnderstand attribute in <ecp:SubjectConfirmation>.',
98
            ProtocolViolationException::class,
99
        );
100
101
        $actor = $xml->getAttributeNS(C::NS_SOAP_ENV_11, 'actor');
102
        Assert::same(
103
            $actor,
104
            C::SOAP_ACTOR_NEXT,
105
            'Invalid value of env:actor attribute in <ecp:SubjectConfirmation>.',
106
            ProtocolViolationException::class,
107
        );
108
109
        $subjectConfirmationData = SubjectConfirmationData::getChildrenOfClass($xml);
110
        Assert::maxCount(
111
            $subjectConfirmationData,
112
            1,
113
            'More than one <saml:SubjectConfirmationData> in <saml:SubjectConfirmation>.',
114
            TooManyElementsException::class,
115
        );
116
117
        return new static(
118
            self::getAttribute($xml, 'Method'),
119
            array_pop($subjectConfirmationData),
120
        );
121
    }
122
123
124
    /**
125
     * Convert this ECP SubjectConfirmation to XML.
126
     *
127
     * @param \DOMElement|null $parent The element we should append this element to.
128
     * @return \DOMElement
129
     */
130
    public function toXML(?DOMElement $parent = null): DOMElement
131
    {
132
        $e = $this->instantiateParentElement($parent);
133
        $e->setAttributeNS(C::NS_SOAP_ENV_11, 'env:mustUnderstand', '1');
134
        $e->setAttributeNS(C::NS_SOAP_ENV_11, 'env:actor', C::SOAP_ACTOR_NEXT);
135
        $e->setAttribute('Method', $this->getMethod());
136
137
        $this->getSubjectConfirmationData()?->toXML($e);
138
139
        return $e;
140
    }
141
}
142