Passed
Push — master ( 8cd786...a11f07 )
by Tim
04:16 queued 02:07
created

Request   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 47
c 1
b 0
f 0
dl 0
loc 148
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getIssuer() 0 3 1
A toXML() 0 18 3
A getProviderName() 0 3 1
A getIDPList() 0 3 1
A fromXML() 0 48 1
A getIsPassive() 0 3 1
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\Exception\ProtocolViolationException;
10
use SimpleSAML\SAML2\XML\saml\Issuer;
11
use SimpleSAML\SAML2\XML\samlp\IDPList;
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\Exception\SchemaViolationException;
17
18
use function boolval;
19
use function intval;
20
use function is_null;
21
use function is_numeric;
22
use function strval;
23
24
/**
25
 * Class representing the ECP Request element.
26
 *
27
 * @package simplesamlphp/saml2
28
 */
29
final class Request extends AbstractEcpElement
30
{
31
    /**
32
     * Create a ECP Request element.
33
     *
34
     * @param \SimpleSAML\SAML2\XML\saml\Issuer $issuer
35
     * @param \SimpleSAML\SAML2\XML\samlp\IDPList|null $idpList
36
     * @param string|null $providerName
37
     * @param bool|null $isPassive
38
     */
39
    public function __construct(
40
        protected Issuer $issuer,
41
        protected ?IDPList $idpList = null,
42
        protected ?string $providerName = null,
43
        protected ?bool $isPassive = null,
44
    ) {
45
    }
46
47
48
    /**
49
     * Collect the value of the isPassive-property
50
     *
51
     * @return bool|null
52
     */
53
    public function getIsPassive(): ?bool
54
    {
55
        return $this->isPassive;
56
    }
57
58
59
    /**
60
     * Collect the value of the providerName-property
61
     *
62
     * @return string|null
63
     */
64
    public function getProviderName(): ?string
65
    {
66
        return $this->providerName;
67
    }
68
69
70
    /**
71
     * Collect the value of the issuer-property
72
     *
73
     * @return \SimpleSAML\SAML2\XML\saml\Issuer
74
     */
75
    public function getIssuer(): Issuer
76
    {
77
        return $this->issuer;
78
    }
79
    /**
80
     * Collect the value of the idpList-property
81
     *
82
     * @return \SimpleSAML\SAML2\XML\samlp\IDPList|null
83
     */
84
    public function getIDPList(): ?IDPList
85
    {
86
        return $this->idpList;
87
    }
88
89
90
    /**
91
     * Convert XML into a Request
92
     *
93
     * @param \DOMElement $xml The XML element we should load
94
     * @return static
95
     *
96
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
97
     *   if the qualified name of the supplied element is wrong
98
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
99
     *   if the supplied element is missing any of the mandatory attributes
100
     */
101
    public static function fromXML(DOMElement $xml): static
102
    {
103
        Assert::same($xml->localName, 'Request', InvalidDOMElementException::class);
104
        Assert::same($xml->namespaceURI, Request::NS, InvalidDOMElementException::class);
105
106
        // Assert required attributes
107
        Assert::true(
108
            $xml->hasAttributeNS(C::NS_SOAP_ENV_11, 'actor'),
109
            'Missing env:actor attribute in <ecp:Request>.',
110
            MissingAttributeException::class,
111
        );
112
        Assert::true(
113
            $xml->hasAttributeNS(C::NS_SOAP_ENV_11, 'mustUnderstand'),
114
            'Missing env:mustUnderstand attribute in <ecp:Request>.',
115
            MissingAttributeException::class,
116
        );
117
118
        $mustUnderstand = $xml->getAttributeNS(C::NS_SOAP_ENV_11, 'mustUnderstand');
119
        Assert::same(
120
            $mustUnderstand,
121
            '1',
122
            'Invalid value of env:mustUnderstand attribute in <ecp:Request>.',
123
            ProtocolViolationException::class,
124
        );
125
126
        $actor = $xml->getAttributeNS(C::NS_SOAP_ENV_11, 'actor');
127
        Assert::same(
128
            $actor,
129
            C::SOAP_ACTOR_NEXT,
130
            'Invalid value of env:actor attribute in <ecp:Request>.',
131
            ProtocolViolationException::class,
132
        );
133
134
        $issuer = Issuer::getChildrenOfClass($xml);
135
        Assert::count(
136
            $issuer,
137
            1,
138
            'More than one <saml:Issuer> in <ecp:Request>.',
139
            TooManyElementsException::class,
140
        );
141
142
        $idpList = IDPList::getChildrenOfClass($xml);
143
144
        return new static(
145
            array_pop($issuer),
146
            array_pop($idpList),
147
            self::getOptionalAttribute($xml, 'ProviderName', null),
148
            self::getOptionalBooleanAttribute($xml, 'IsPassive', null),
149
        );
150
    }
151
152
153
    /**
154
     * Convert this ECP SubjectConfirmation to XML.
155
     *
156
     * @param \DOMElement|null $parent The element we should append this element to.
157
     * @return \DOMElement
158
     */
159
    public function toXML(DOMElement $parent = null): DOMElement
160
    {
161
        $e = $this->instantiateParentElement($parent);
162
        $e->setAttributeNS(C::NS_SOAP_ENV_11, 'env:mustUnderstand', '1');
163
        $e->setAttributeNS(C::NS_SOAP_ENV_11, 'env:actor', C::SOAP_ACTOR_NEXT);
164
165
        if ($this->getProviderName() !== null) {
166
            $e->setAttribute('ProviderName', $this->getProviderName());
167
        }
168
169
        if ($this->getIsPassive() !== null) {
170
            $e->setAttribute('IsPassive', strval(intval($this->getIsPassive())));
171
        }
172
173
        $this->getIssuer()->toXML($e);
174
        $this->getIDPList()?->toXML($e);
175
176
        return $e;
177
    }
178
}
179