Skip to content

Commit c36ad98

Browse files
committed
Refactor saml:SubjectConfirmationData
1 parent e8d72a4 commit c36ad98

File tree

2 files changed

+193
-167
lines changed

2 files changed

+193
-167
lines changed
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\SAML2\XML\saml;
6+
7+
use DOMElement;
8+
use SimpleSAML\Assert\AssertionFailedException;
9+
use SimpleSAML\SAML2\Assert\Assert;
10+
use SimpleSAML\SAML2\Constants as C;
11+
use SimpleSAML\SAML2\Type\EntityIDValue;
12+
use SimpleSAML\SAML2\Type\SAMLDateTimeValue;
13+
use SimpleSAML\SAML2\Type\SAMLStringValue;
14+
use SimpleSAML\SAML2\Utils;
15+
use SimpleSAML\XMLSchema\Type\NCNameValue;
16+
use SimpleSAML\XMLSchema\XML\AbstractAnyType;
17+
use SimpleSAML\XMLSchema\XML\Constants\NS;
18+
19+
use function strval;
20+
21+
/**
22+
* Abstract class representing SAML 2 SubjectConfirmationData element.
23+
*
24+
* @package simplesamlphp/saml2
25+
*/
26+
abstract class AbstractSubjectConfirmationData extends AbstractAnyType
27+
{
28+
/** @var string */
29+
public const NS = C::NS_SAML;
30+
31+
/** @var string */
32+
public const NS_PREFIX = 'saml';
33+
34+
/** @var string */
35+
public const SCHEMA = 'resources/schemas/saml-schema-assertion-2.0.xsd';
36+
37+
/** The namespace-attribute for the xs:any element */
38+
public const XS_ANY_ELT_NAMESPACE = NS::ANY;
39+
40+
/** The namespace-attribute for the xs:anyAttribute element */
41+
public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
42+
43+
44+
/**
45+
* Initialize (and parse) a SubjectConfirmationData element.
46+
*
47+
* @param \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null $notBefore
48+
* @param \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null $notOnOrAfter
49+
* @param \SimpleSAML\SAML2\Type\EntityIDValue|null $recipient
50+
* @param \SimpleSAML\XMLSchema\Type\NCNameValue|null $inResponseTo
51+
* @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $address
52+
* @param \SimpleSAML\XML\SerializableElementInterface[] $children
53+
* @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
54+
*/
55+
public function __construct(
56+
protected ?SAMLDateTimeValue $notBefore = null,
57+
protected ?SAMLDateTimeValue $notOnOrAfter = null,
58+
protected ?EntityIDValue $recipient = null,
59+
protected ?NCNameValue $inResponseTo = null,
60+
protected ?SAMLStringValue $address = null,
61+
array $children = [],
62+
array $namespacedAttributes = [],
63+
) {
64+
if ($address !== null) {
65+
try {
66+
/**
67+
* IPv4 addresses SHOULD be represented in the usual dotted-decimal format (e.g., "1.2.3.4").
68+
* IPv6 addresses SHOULD be represented as defined by Section 2.2 of IETF RFC 3513 [RFC 3513]
69+
* (e.g., "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210").
70+
*/
71+
Assert::ip($address->getValue());
72+
} catch (AssertionFailedException) {
73+
Utils::getContainer()->getLogger()->warning(
74+
sprintf('Provided address (%s) is not a valid IPv4 or IPv6 address.', $address->getValue()),
75+
);
76+
}
77+
}
78+
79+
$this->setElements($children);
80+
$this->setAttributesNS($namespacedAttributes);
81+
}
82+
83+
84+
/**
85+
* Collect the value of the NotBefore-property
86+
*
87+
* @return \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null
88+
*/
89+
public function getNotBefore(): ?SAMLDateTimeValue
90+
{
91+
return $this->notBefore;
92+
}
93+
94+
95+
/**
96+
* Collect the value of the NotOnOrAfter-property
97+
*
98+
* @return \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null
99+
*/
100+
public function getNotOnOrAfter(): ?SAMLDateTimeValue
101+
{
102+
return $this->notOnOrAfter;
103+
}
104+
105+
106+
/**
107+
* Collect the value of the Recipient-property
108+
*
109+
* @return \SimpleSAML\SAML2\Type\EntityIDValue|null
110+
*/
111+
public function getRecipient(): ?EntityIDValue
112+
{
113+
return $this->recipient;
114+
}
115+
116+
117+
/**
118+
* Collect the value of the InResponseTo-property
119+
*
120+
* @return \SimpleSAML\XMLSchema\Type\NCNameValue|null
121+
*/
122+
public function getInResponseTo(): ?NCNameValue
123+
{
124+
return $this->inResponseTo;
125+
}
126+
127+
128+
/**
129+
* Collect the value of the Address-property
130+
*
131+
* @return \SimpleSAML\SAML2\Type\SAMLStringValue|null
132+
*/
133+
public function getAddress(): ?SAMLStringValue
134+
{
135+
return $this->address;
136+
}
137+
138+
139+
/**
140+
* Test if an object, at the state it's in, would produce an empty XML-element
141+
*
142+
* @return bool
143+
*/
144+
public function isEmptyElement(): bool
145+
{
146+
return empty($this->getNotBefore())
147+
&& empty($this->getNotOnOrAfter())
148+
&& empty($this->getRecipient())
149+
&& empty($this->getInResponseTo())
150+
&& empty($this->getAddress())
151+
&& empty($this->getElements())
152+
&& empty($this->getAttributesNS());
153+
}
154+
155+
156+
/**
157+
* Convert this element to XML.
158+
*
159+
* @param \DOMElement|null $parent The parent element we should append this element to.
160+
* @return \DOMElement This element, as XML.
161+
*/
162+
public function toXML(?DOMElement $parent = null): DOMElement
163+
{
164+
$e = $this->instantiateParentElement($parent);
165+
166+
if ($this->getNotBefore() !== null) {
167+
$e->setAttribute('NotBefore', strval($this->getNotBefore()));
168+
}
169+
if ($this->getNotOnOrAfter() !== null) {
170+
$e->setAttribute('NotOnOrAfter', strval($this->getNotOnOrAfter()));
171+
}
172+
if ($this->getRecipient() !== null) {
173+
$e->setAttribute('Recipient', strval($this->getRecipient()));
174+
}
175+
if ($this->getInResponseTo() !== null) {
176+
$e->setAttribute('InResponseTo', strval($this->getInResponseTo()));
177+
}
178+
if ($this->getAddress() !== null) {
179+
$e->setAttribute('Address', strval($this->getAddress()));
180+
}
181+
182+
foreach ($this->getAttributesNS() as $attr) {
183+
$attr->toXML($e);
184+
}
185+
186+
foreach ($this->getElements() as $n) {
187+
$n->toXML($e);
188+
}
189+
190+
return $e;
191+
}
192+
}

src/XML/saml/SubjectConfirmationData.php

Lines changed: 1 addition & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -5,153 +5,25 @@
55
namespace SimpleSAML\SAML2\XML\saml;
66

77
use DOMElement;
8-
use SimpleSAML\Assert\AssertionFailedException;
98
use SimpleSAML\SAML2\Assert\Assert;
109
use SimpleSAML\SAML2\Type\EntityIDValue;
1110
use SimpleSAML\SAML2\Type\SAMLDateTimeValue;
1211
use SimpleSAML\SAML2\Type\SAMLStringValue;
13-
use SimpleSAML\SAML2\Utils;
14-
use SimpleSAML\XML\ExtendableAttributesTrait;
15-
use SimpleSAML\XML\ExtendableElementTrait;
1612
use SimpleSAML\XML\SchemaValidatableElementInterface;
1713
use SimpleSAML\XML\SchemaValidatableElementTrait;
1814
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
1915
use SimpleSAML\XMLSchema\Type\NCNameValue;
20-
use SimpleSAML\XMLSchema\XML\Constants\NS;
21-
22-
use function strval;
2316

2417
/**
2518
* Class representing SAML 2 SubjectConfirmationData element.
2619
*
2720
* @package simplesamlphp/saml2
2821
*/
29-
final class SubjectConfirmationData extends AbstractSamlElement implements SchemaValidatableElementInterface
22+
final class SubjectConfirmationData extends AbstractSubjectConfirmationData implements SchemaValidatableElementInterface
3023
{
31-
use ExtendableAttributesTrait;
32-
use ExtendableElementTrait;
3324
use SchemaValidatableElementTrait;
3425

3526

36-
/** The namespace-attribute for the xs:any element */
37-
public const XS_ANY_ELT_NAMESPACE = NS::ANY;
38-
39-
/** The namespace-attribute for the xs:anyAttribute element */
40-
public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
41-
42-
43-
/**
44-
* Initialize (and parse) a SubjectConfirmationData element.
45-
*
46-
* @param \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null $notBefore
47-
* @param \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null $notOnOrAfter
48-
* @param \SimpleSAML\SAML2\Type\EntityIDValue|null $recipient
49-
* @param \SimpleSAML\XMLSchema\Type\NCNameValue|null $inResponseTo
50-
* @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $address
51-
* @param \SimpleSAML\XML\SerializableElementInterface[] $children
52-
* @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
53-
*/
54-
public function __construct(
55-
protected ?SAMLDateTimeValue $notBefore = null,
56-
protected ?SAMLDateTimeValue $notOnOrAfter = null,
57-
protected ?EntityIDValue $recipient = null,
58-
protected ?NCNameValue $inResponseTo = null,
59-
protected ?SAMLStringValue $address = null,
60-
array $children = [],
61-
array $namespacedAttributes = [],
62-
) {
63-
if ($address !== null) {
64-
try {
65-
/**
66-
* IPv4 addresses SHOULD be represented in the usual dotted-decimal format (e.g., "1.2.3.4").
67-
* IPv6 addresses SHOULD be represented as defined by Section 2.2 of IETF RFC 3513 [RFC 3513]
68-
* (e.g., "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210").
69-
*/
70-
Assert::ip($address->getValue());
71-
} catch (AssertionFailedException) {
72-
Utils::getContainer()->getLogger()->warning(
73-
sprintf('Provided address (%s) is not a valid IPv4 or IPv6 address.', $address->getValue()),
74-
);
75-
}
76-
}
77-
78-
$this->setElements($children);
79-
$this->setAttributesNS($namespacedAttributes);
80-
}
81-
82-
83-
/**
84-
* Collect the value of the NotBefore-property
85-
*
86-
* @return \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null
87-
*/
88-
public function getNotBefore(): ?SAMLDateTimeValue
89-
{
90-
return $this->notBefore;
91-
}
92-
93-
94-
/**
95-
* Collect the value of the NotOnOrAfter-property
96-
*
97-
* @return \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null
98-
*/
99-
public function getNotOnOrAfter(): ?SAMLDateTimeValue
100-
{
101-
return $this->notOnOrAfter;
102-
}
103-
104-
105-
/**
106-
* Collect the value of the Recipient-property
107-
*
108-
* @return \SimpleSAML\SAML2\Type\EntityIDValue|null
109-
*/
110-
public function getRecipient(): ?EntityIDValue
111-
{
112-
return $this->recipient;
113-
}
114-
115-
116-
/**
117-
* Collect the value of the InResponseTo-property
118-
*
119-
* @return \SimpleSAML\XMLSchema\Type\NCNameValue|null
120-
*/
121-
public function getInResponseTo(): ?NCNameValue
122-
{
123-
return $this->inResponseTo;
124-
}
125-
126-
127-
/**
128-
* Collect the value of the Address-property
129-
*
130-
* @return \SimpleSAML\SAML2\Type\SAMLStringValue|null
131-
*/
132-
public function getAddress(): ?SAMLStringValue
133-
{
134-
return $this->address;
135-
}
136-
137-
138-
/**
139-
* Test if an object, at the state it's in, would produce an empty XML-element
140-
*
141-
* @return bool
142-
*/
143-
public function isEmptyElement(): bool
144-
{
145-
return empty($this->getNotBefore())
146-
&& empty($this->getNotOnOrAfter())
147-
&& empty($this->getRecipient())
148-
&& empty($this->getInResponseTo())
149-
&& empty($this->getAddress())
150-
&& empty($this->getElements())
151-
&& empty($this->getAttributesNS());
152-
}
153-
154-
15527
/**
15628
* Convert XML into a SubjectConfirmationData
15729
*
@@ -180,42 +52,4 @@ public static function fromXML(DOMElement $xml): static
18052
self::getAttributesNSFromXML($xml),
18153
);
18254
}
183-
184-
185-
/**
186-
* Convert this element to XML.
187-
*
188-
* @param \DOMElement|null $parent The parent element we should append this element to.
189-
* @return \DOMElement This element, as XML.
190-
*/
191-
public function toXML(?DOMElement $parent = null): DOMElement
192-
{
193-
$e = $this->instantiateParentElement($parent);
194-
195-
if ($this->getNotBefore() !== null) {
196-
$e->setAttribute('NotBefore', strval($this->getNotBefore()));
197-
}
198-
if ($this->getNotOnOrAfter() !== null) {
199-
$e->setAttribute('NotOnOrAfter', strval($this->getNotOnOrAfter()));
200-
}
201-
if ($this->getRecipient() !== null) {
202-
$e->setAttribute('Recipient', strval($this->getRecipient()));
203-
}
204-
if ($this->getInResponseTo() !== null) {
205-
$e->setAttribute('InResponseTo', strval($this->getInResponseTo()));
206-
}
207-
if ($this->getAddress() !== null) {
208-
$e->setAttribute('Address', strval($this->getAddress()));
209-
}
210-
211-
foreach ($this->getAttributesNS() as $attr) {
212-
$attr->toXML($e);
213-
}
214-
215-
foreach ($this->getElements() as $n) {
216-
$n->toXML($e);
217-
}
218-
219-
return $e;
220-
}
22155
}

0 commit comments

Comments
 (0)