Passed
Push — release-4-alpha ( 53e910...362819 )
by Tim
02:18
created

SubjectConfirmationData::setAddress()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SAML2\XML\saml;
6
7
use Webmozart\Assert\Assert;
8
use RobRichards\XMLSecLibs\XMLSecurityDSig;
9
10
use SAML2\Constants;
11
use SAML2\Utils;
12
use SAML2\XML\Chunk;
13
use SAML2\XML\ds\KeyInfo;
14
15
/**
16
 * Class representing SAML 2 SubjectConfirmationData element.
17
 *
18
 * @package SimpleSAMLphp
19
 */
20
class SubjectConfirmationData
21
{
22
    /**
23
     * The time before this element is valid, as an unix timestamp.
24
     *
25
     * @var int|null
26
     */
27
    public $NotBefore = null;
28
29
    /**
30
     * The time after which this element is invalid, as an unix timestamp.
31
     *
32
     * @var int|null
33
     */
34
    public $NotOnOrAfter = null;
35
36
    /**
37
     * The Recipient this Subject is valid for. Either an entity or a location.
38
     *
39
     * @var string|null
40
     */
41
    public $Recipient = null;
42
43
    /**
44
     * The ID of the AuthnRequest this is a response to.
45
     *
46
     * @var string|null
47
     */
48
    public $InResponseTo = null;
49
50
    /**
51
     * The IP(v6) address of the user.
52
     *
53
     * @var string|null
54
     */
55
    public $Address = null;
56
57
    /**
58
     * The various key information elements.
59
     *
60
     * Array with various elements describing this key.
61
     * Unknown elements will be represented by \SAML2\XML\Chunk.
62
     *
63
     * @var (\SAML2\XML\ds\KeyInfo|\SAML2\XML\Chunk)[]
64
     */
65
    public $info = [];
66
67
68
    /**
69
     * Collect the value of the NotBefore-property
70
     *
71
     * @return int|null
72
     */
73
    public function getNotBefore()
74
    {
75
        return $this->NotBefore;
76
    }
77
78
79
    /**
80
     * Set the value of the NotBefore-property
81
     *
82
     * @param int|null $notBefore
83
     * @return void
84
     */
85
    public function setNotBefore(int $notBefore = null)
86
    {
87
        $this->NotBefore = $notBefore;
88
    }
89
90
91
    /**
92
     * Collect the value of the NotOnOrAfter-property
93
     *
94
     * @return int|null
95
     */
96
    public function getNotOnOrAfter()
97
    {
98
        return $this->NotOnOrAfter;
99
    }
100
101
102
    /**
103
     * Set the value of the NotOnOrAfter-property
104
     *
105
     * @param int|null $notOnOrAfter
106
     * @return void
107
     */
108
    public function setNotOnOrAfter(int $notOnOrAfter = null)
109
    {
110
        $this->NotOnOrAfter = $notOnOrAfter;
111
    }
112
113
114
    /**
115
     * Collect the value of the Recipient-property
116
     *
117
     * @return string|null
118
     */
119
    public function getRecipient()
120
    {
121
        return $this->Recipient;
122
    }
123
124
125
    /**
126
     * Set the value of the Recipient-property
127
     *
128
     * @param string|null $recipient
129
     * @return void
130
     */
131
    public function setRecipient(string $recipient = null)
132
    {
133
        $this->Recipient = $recipient;
134
    }
135
136
137
    /**
138
     * Collect the value of the InResponseTo-property
139
     *
140
     * @return string|null
141
     */
142
    public function getInResponseTo()
143
    {
144
        return $this->InResponseTo;
145
    }
146
147
148
    /**
149
     * Set the value of the InResponseTo-property
150
     *
151
     * @param string|null $inResponseTo
152
     * @return void
153
     */
154
    public function setInResponseTo(string $inResponseTo = null)
155
    {
156
        $this->InResponseTo = $inResponseTo;
157
    }
158
159
160
    /**
161
     * Collect the value of the Address-property
162
     *
163
     * @return string|null
164
     */
165
    public function getAddress()
166
    {
167
        return $this->Address;
168
    }
169
170
171
    /**
172
     * Set the value of the Address-property
173
     *
174
     * @param string|null $address
175
     * @return void
176
     */
177
    public function setAddress(string $address = null)
178
    {
179
        if (!is_null($address) && !filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
180
            throw new \InvalidArgumentException('Provided argument is not a valid IP address.');
181
        }
182
        $this->Address = $address;
183
    }
184
185
186
    /**
187
     * Collect the value of the info-property
188
     *
189
     * @return (\SAML2\XML\ds\KeyInfo|\SAML2\XML\Chunk)[]
190
     */
191
    public function getInfo() : array
192
    {
193
        return $this->info;
194
    }
195
196
197
    /**
198
     * Set the value of the info-property
199
     *
200
     * @param (\SAML2\XML\ds\KeyInfo|\SAML2\XML\Chunk)[] $info
201
     * @return void
202
     */
203
    public function setInfo(array $info)
204
    {
205
        $this->info = $info;
206
    }
207
208
209
    /**
210
     * Add the value to the info-property
211
     *
212
     * @param \SAML2\XML\Chunk|\SAML2\XML\ds\KeyInfo $info
213
     * @return void
214
     */
215
    public function addInfo($info)
216
    {
217
        Assert::isInstanceOfAny($info, [Chunk::class, KeyInfo::class]);
218
        $this->info[] = $info;
219
    }
220
221
222
    /**
223
     * Initialize (and parse) a SubjectConfirmationData element.
224
     *
225
     * @param \DOMElement|null $xml The XML element we should load.
226
     */
227
    public function __construct(\DOMElement $xml = null)
228
    {
229
        if ($xml === null) {
230
            return;
231
        }
232
233
        if ($xml->hasAttribute('NotBefore')) {
234
            $this->setNotBefore(Utils::xsDateTimeToTimestamp($xml->getAttribute('NotBefore')));
235
        }
236
        if ($xml->hasAttribute('NotOnOrAfter')) {
237
            $this->setNotOnOrAfter(Utils::xsDateTimeToTimestamp($xml->getAttribute('NotOnOrAfter')));
238
        }
239
        if ($xml->hasAttribute('Recipient')) {
240
            $this->setRecipient($xml->getAttribute('Recipient'));
241
        }
242
        if ($xml->hasAttribute('InResponseTo')) {
243
            $this->setInResponseTo($xml->getAttribute('InResponseTo'));
244
        }
245
        if ($xml->hasAttribute('Address')) {
246
            $this->setAddress($xml->getAttribute('Address'));
247
        }
248
        foreach ($xml->childNodes as $n) {
249
            if (!($n instanceof \DOMElement)) {
250
                continue;
251
            }
252
            if ($n->namespaceURI !== XMLSecurityDSig::XMLDSIGNS) {
253
                $this->addInfo(new Chunk($n));
254
                continue;
255
            }
256
            switch ($n->localName) {
257
                case 'KeyInfo':
258
                    $this->addInfo(new KeyInfo($n));
259
                    break;
260
                default:
261
                    $this->addInfo(new Chunk($n));
262
                    break;
263
            }
264
        }
265
    }
266
267
268
    /**
269
     * Convert this element to XML.
270
     *
271
     * @param  \DOMElement $parent The parent element we should append this element to.
272
     * @return \DOMElement This element, as XML.
273
     */
274
    public function toXML(\DOMElement $parent)
275
    {
276
        $e = $parent->ownerDocument->createElementNS(Constants::NS_SAML, 'saml:SubjectConfirmationData');
277
        $parent->appendChild($e);
278
279
        if ($this->NotBefore !== null) {
280
            $e->setAttribute('NotBefore', gmdate('Y-m-d\TH:i:s\Z', $this->NotBefore));
281
        }
282
        if ($this->NotOnOrAfter !== null) {
283
            $e->setAttribute('NotOnOrAfter', gmdate('Y-m-d\TH:i:s\Z', $this->NotOnOrAfter));
284
        }
285
        if ($this->Recipient !== null) {
286
            $e->setAttribute('Recipient', $this->Recipient);
287
        }
288
        if ($this->InResponseTo !== null) {
289
            $e->setAttribute('InResponseTo', $this->InResponseTo);
290
        }
291
        if ($this->Address !== null) {
292
            $e->setAttribute('Address', $this->Address);
293
        }
294
        /** @var \SAML2\XML\ds\KeyInfo|\SAML2\XML\Chunk $n */
295
        foreach ($this->getInfo() as $n) {
296
            $n->toXML($e);
297
        }
298
299
        return $e;
300
    }
301
}
302