Skip to content

Commit f62d44d

Browse files
committed
Refactor
1 parent f98bed8 commit f62d44d

17 files changed

+369
-244
lines changed

resources/schemas/xenc-schema.xsd

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<!--
4+
#
5+
# Copyright ©[2011] World Wide Web Consortium
6+
# (Massachusetts Institute of Technology,
7+
# European Research Consortium for Informatics and Mathematics,
8+
# Keio University). All Rights Reserved.
9+
# This work is distributed under the W3C® Software License [1] in the
10+
# hope that it will be useful, but WITHOUT ANY WARRANTY; without even
11+
# the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12+
# PURPOSE.
13+
# [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
14+
#
15+
-->
16+
17+
<!DOCTYPE schema PUBLIC "-//W3C//DTD XMLSchema 200102//EN"
18+
"http://www.w3.org/2001/XMLSchema.dtd"
19+
[
20+
<!ATTLIST schema
21+
xmlns:xenc CDATA #FIXED 'http://www.w3.org/2001/04/xmlenc#'
22+
xmlns:ds CDATA #FIXED 'http://www.w3.org/2000/09/xmldsig#'>
23+
<!ENTITY xenc 'http://www.w3.org/2001/04/xmlenc#'>
24+
<!ENTITY % p ''>
25+
<!ENTITY % s ''>
26+
]>
27+
28+
<schema xmlns='http://www.w3.org/2001/XMLSchema' version='1.0'
29+
xmlns:xenc='http://www.w3.org/2001/04/xmlenc#'
30+
xmlns:ds='http://www.w3.org/2000/09/xmldsig#'
31+
targetNamespace='http://www.w3.org/2001/04/xmlenc#'
32+
elementFormDefault='qualified'>
33+
34+
<import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation='xml.xsd' />
35+
<import namespace='http://www.w3.org/2000/09/xmldsig#'
36+
schemaLocation='xmldsig-core-schema.xsd'/>
37+
38+
<complexType name='EncryptedType' abstract='true'>
39+
<sequence>
40+
<element name='EncryptionMethod' type='xenc:EncryptionMethodType'
41+
minOccurs='0'/>
42+
<element ref='ds:KeyInfo' minOccurs='0'/>
43+
<element ref='xenc:CipherData'/>
44+
<element ref='xenc:EncryptionProperties' minOccurs='0'/>
45+
</sequence>
46+
<attribute name='Id' type='ID' use='optional'/>
47+
<attribute name='Type' type='anyURI' use='optional'/>
48+
<attribute name='MimeType' type='string' use='optional'/>
49+
<attribute name='Encoding' type='anyURI' use='optional'/>
50+
</complexType>
51+
52+
<complexType name='EncryptionMethodType' mixed='true'>
53+
<sequence>
54+
<element name='KeySize' minOccurs='0' type='xenc:KeySizeType'/>
55+
<element name='OAEPparams' minOccurs='0' type='base64Binary'/>
56+
<!-- note that optional xenc11:MGF element may be used here for
57+
RSA-OAEP, when appropriate -->
58+
<any namespace='##other' minOccurs='0' maxOccurs='unbounded'/>
59+
</sequence>
60+
<attribute name='Algorithm' type='anyURI' use='required'/>
61+
</complexType>
62+
63+
<simpleType name='KeySizeType'>
64+
<restriction base="integer"/>
65+
</simpleType>
66+
67+
<element name='CipherData' type='xenc:CipherDataType'/>
68+
<complexType name='CipherDataType'>
69+
<choice>
70+
<element name='CipherValue' type='base64Binary'/>
71+
<element ref='xenc:CipherReference'/>
72+
</choice>
73+
</complexType>
74+
75+
<element name='CipherReference' type='xenc:CipherReferenceType'/>
76+
<complexType name='CipherReferenceType'>
77+
<choice>
78+
<element name='Transforms' type='xenc:TransformsType' minOccurs='0'/>
79+
</choice>
80+
<attribute name='URI' type='anyURI' use='required'/>
81+
</complexType>
82+
83+
<complexType name='TransformsType'>
84+
<sequence>
85+
<element ref='ds:Transform' maxOccurs='unbounded'/>
86+
</sequence>
87+
</complexType>
88+
89+
90+
<element name='EncryptedData' type='xenc:EncryptedDataType'/>
91+
<complexType name='EncryptedDataType'>
92+
<complexContent>
93+
<extension base='xenc:EncryptedType'>
94+
</extension>
95+
</complexContent>
96+
</complexType>
97+
98+
<!-- Children of ds:KeyInfo -->
99+
100+
<element name='EncryptedKey' type='xenc:EncryptedKeyType'/>
101+
<complexType name='EncryptedKeyType'>
102+
<complexContent>
103+
<extension base='xenc:EncryptedType'>
104+
<sequence>
105+
<element ref='xenc:ReferenceList' minOccurs='0'/>
106+
<element name='CarriedKeyName' type='string' minOccurs='0'/>
107+
</sequence>
108+
<attribute name='Recipient' type='string'
109+
use='optional'/>
110+
</extension>
111+
</complexContent>
112+
</complexType>
113+
114+
<element name="AgreementMethod" type="xenc:AgreementMethodType"/>
115+
<complexType name="AgreementMethodType" mixed="true">
116+
<sequence>
117+
<element name="KA-Nonce" minOccurs="0" type="base64Binary"/>
118+
<!-- <element ref="ds:DigestMethod" minOccurs="0"/> -->
119+
<any namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
120+
<element name="OriginatorKeyInfo" minOccurs="0" type="ds:KeyInfoType"/>
121+
<element name="RecipientKeyInfo" minOccurs="0" type="ds:KeyInfoType"/>
122+
</sequence>
123+
<attribute name="Algorithm" type="anyURI" use="required"/>
124+
</complexType>
125+
126+
<!-- End Children of ds:KeyInfo -->
127+
128+
<element name='ReferenceList'>
129+
<complexType>
130+
<choice minOccurs='1' maxOccurs='unbounded'>
131+
<element name='DataReference' type='xenc:ReferenceType'/>
132+
<element name='KeyReference' type='xenc:ReferenceType'/>
133+
</choice>
134+
</complexType>
135+
</element>
136+
137+
<complexType name='ReferenceType'>
138+
<sequence>
139+
<any namespace='##other' minOccurs='0' maxOccurs='unbounded'/>
140+
</sequence>
141+
<attribute name='URI' type='anyURI' use='required'/>
142+
</complexType>
143+
144+
145+
<element name='EncryptionProperties' type='xenc:EncryptionPropertiesType'/>
146+
<complexType name='EncryptionPropertiesType'>
147+
<sequence>
148+
<element ref='xenc:EncryptionProperty' maxOccurs='unbounded'/>
149+
</sequence>
150+
<attribute name='Id' type='ID' use='optional'/>
151+
</complexType>
152+
153+
<element name='EncryptionProperty' type='xenc:EncryptionPropertyType'/>
154+
<complexType name='EncryptionPropertyType' mixed='true'>
155+
<choice maxOccurs='unbounded'>
156+
<any namespace='##other' processContents='lax'/>
157+
</choice>
158+
<attribute name='Target' type='anyURI' use='optional'/>
159+
<attribute name='Id' type='ID' use='optional'/>
160+
<anyAttribute namespace="http://www.w3.org/XML/1998/namespace"/>
161+
</complexType>
162+
163+
<!-- Children of ds:KeyValue -->
164+
165+
<element name="DHKeyValue" type="xenc:DHKeyValueType"/>
166+
<complexType name="DHKeyValueType">
167+
<sequence>
168+
<sequence minOccurs="0">
169+
<element name="P" type="ds:CryptoBinary"/>
170+
<element name="Q" type="ds:CryptoBinary"/>
171+
<element name="Generator" type="ds:CryptoBinary"/>
172+
</sequence>
173+
<element name="Public" type="ds:CryptoBinary"/>
174+
<sequence minOccurs="0">
175+
<element name="seed" type="ds:CryptoBinary"/>
176+
<element name="pgenCounter" type="ds:CryptoBinary"/>
177+
</sequence>
178+
</sequence>
179+
</complexType>
180+
181+
<!-- End Children of ds:KeyValue -->
182+
183+
</schema>
184+

src/Type/UsageValue.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\WebServices\Security\Type;
6+
7+
use SimpleSAML\WebServices\Security\Constants as C;
8+
use SimpleSAML\XML\Attribute;
9+
use SimpleSAML\XMLSchema\Type\Helper\AnyURIListValue;
10+
use SimpleSAML\XMLSchema\Type\Interface\AttributeTypeInterface;
11+
12+
/**
13+
* @package simplesaml/xml-wss-core
14+
*/
15+
class UsageValue extends AnyURIListValue implements AttributeTypeInterface
16+
{
17+
public const string SCHEMA_TYPE = 'tUsage';
18+
19+
20+
/**
21+
* Convert this value to an attribute
22+
*
23+
* @return \SimpleSAML\XML\Attribute
24+
*/
25+
public function toAttribute(): Attribute
26+
{
27+
return new Attribute(C::NS_SEC_EXT, 'wsse', 'Usage', $this);
28+
}
29+
}

src/XML/wsse/AbstractAttributedString.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use SimpleSAML\WebServices\Security\Assert\Assert;
99
use SimpleSAML\WebServices\Security\Constants as C;
1010
use SimpleSAML\WebServices\Security\Type\IDValue;
11+
use SimpleSAML\WebServices\Security\XML\wsu\IDTrait;
1112
use SimpleSAML\XML\ExtendableAttributesTrait;
1213
use SimpleSAML\XML\TypedTextContentTrait;
1314
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
@@ -24,6 +25,7 @@
2425
abstract class AbstractAttributedString extends AbstractWsseElement
2526
{
2627
use ExtendableAttributesTrait;
28+
use IDTrait;
2729
use TypedTextContentTrait;
2830

2931

@@ -47,23 +49,15 @@ abstract class AbstractAttributedString extends AbstractWsseElement
4749
*/
4850
public function __construct(
4951
StringValue $content,
50-
protected ?IDValue $Id = null,
52+
?IDValue $Id = null,
5153
array $namespacedAttributes = [],
5254
) {
55+
$this->setId($Id);
5356
$this->setContent($content);
5457
$this->setAttributesNS($namespacedAttributes);
5558
}
5659

5760

58-
/**
59-
* @return \SimpleSAML\WebServices\Security\Type\IDValue|null
60-
*/
61-
public function getId(): ?IDValue
62-
{
63-
return $this->Id;
64-
}
65-
66-
6761
/**
6862
* Create an instance of this object from its XML representation.
6963
*

src/XML/wsse/AbstractBinarySecurityTokenType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use SimpleSAML\WebServices\Security\Assert\Assert;
99
use SimpleSAML\WebServices\Security\Constants as C;
1010
use SimpleSAML\WebServices\Security\Type\IDValue;
11+
use SimpleSAML\WebServices\Security\XML\wsu\IDTrait;
1112
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
1213
use SimpleSAML\XMLSchema\Type\AnyURIValue;
1314
use SimpleSAML\XMLSchema\Type\StringValue;
@@ -19,6 +20,9 @@
1920
*/
2021
abstract class AbstractBinarySecurityTokenType extends AbstractEncodedString
2122
{
23+
use IDTrait;
24+
25+
2226
/**
2327
* AbstractBinarySecurityTokenType constructor
2428
*

src/XML/wsse/AbstractSecurityTokenReferenceType.php

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@
88
use SimpleSAML\WebServices\Security\Assert\Assert;
99
use SimpleSAML\WebServices\Security\Constants as C;
1010
use SimpleSAML\WebServices\Security\Type\IDValue;
11-
use SimpleSAML\XML\Attribute as XMLAttribute;
11+
use SimpleSAML\WebServices\Security\Type\UsageValue;
12+
use SimpleSAML\WebServices\Security\XML\wsu\IDTrait;
1213
use SimpleSAML\XML\ExtendableAttributesTrait;
1314
use SimpleSAML\XML\ExtendableElementTrait;
1415
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
15-
use SimpleSAML\XMLSchema\Type\AnyURIValue;
1616
use SimpleSAML\XMLSchema\XML\Constants\NS;
1717

18-
use function array_unshift;
19-
2018
/**
2119
* Class defining the SecurityTokenReferenceType element
2220
*
@@ -26,6 +24,7 @@ abstract class AbstractSecurityTokenReferenceType extends AbstractWsseElement
2624
{
2725
use ExtendableAttributesTrait;
2826
use ExtendableElementTrait;
27+
use IDTrait;
2928
use UsageTrait;
3029

3130

@@ -46,31 +45,23 @@ abstract class AbstractSecurityTokenReferenceType extends AbstractWsseElement
4645
* AbstractSecurityReferenceType constructor
4746
*
4847
* @param \SimpleSAML\WebServices\Security\Type\IDValue|null $Id
49-
* @param \SimpleSAML\XMLSchema\Type\AnyURIValue|null $Usage
48+
* @param \SimpleSAML\WebServices\Security\Type\UsageValue|null $Usage
5049
* @param array<\SimpleSAML\XML\SerializableElementInterface> $children
5150
* @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
5251
*/
5352
final public function __construct(
54-
protected ?IDValue $Id = null,
55-
?AnyURIValue $Usage = null,
53+
?IDValue $Id = null,
54+
?UsageValue $Usage = null,
5655
array $children = [],
5756
array $namespacedAttributes = [],
5857
) {
58+
$this->setId($Id);
5959
$this->setUsage($Usage);
6060
$this->setElements($children);
6161
$this->setAttributesNS($namespacedAttributes);
6262
}
6363

6464

65-
/**
66-
* @return \SimpleSAML\WebServices\Security\Type\IDValue|null
67-
*/
68-
public function getId(): ?IDValue
69-
{
70-
return $this->Id;
71-
}
72-
73-
7465
/**
7566
* Test if an object, at the state it's in, would produce an empty XML-element
7667
*/
@@ -102,7 +93,7 @@ public static function fromXML(DOMElement $xml): static
10293

10394
$Usage = null;
10495
if ($xml->hasAttributeNS(C::NS_SEC_EXT, 'Usage')) {
105-
$Usage = AnyURIValue::fromString($xml->getAttributeNS(C::NS_SEC_EXT, 'Usage'));
96+
$Usage = UsageValue::fromString($xml->getAttributeNS(C::NS_SEC_EXT, 'Usage'));
10697
}
10798

10899
return new static(
@@ -123,18 +114,10 @@ public function toXML(?DOMElement $parent = null): DOMElement
123114
{
124115
$e = parent::instantiateParentElement($parent);
125116

126-
$attributes = $this->getAttributesNS();
127-
if ($this->getId() !== null) {
128-
$idAttr = new XMLAttribute(C::NS_SEC_UTIL, 'wsu', 'Id', $this->getId());
129-
array_unshift($attributes, $idAttr);
130-
}
131-
132-
if ($this->getUsage() !== null) {
133-
$UsageAttr = new XMLAttribute(C::NS_SEC_EXT, 'wsse', 'Usage', $this->getUsage());
134-
array_unshift($attributes, $UsageAttr);
135-
}
117+
$this->getId()?->toAttribute()->toXML($e);
118+
$this->getUsage()?->toAttribute()->toXML($e);
136119

137-
foreach ($attributes as $attr) {
120+
foreach ($this->getAttributesNS() as $attr) {
138121
$attr->toXML($e);
139122
}
140123

0 commit comments

Comments
 (0)