Skip to content

Commit 2cfc5f5

Browse files
committed
Migrate calls to xml-libraries to use the new interface
1 parent 60faa1d commit 2cfc5f5

File tree

5 files changed

+48
-53
lines changed

5 files changed

+48
-53
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"ext-SimpleXML": "*",
3838
"ext-session": "*",
3939

40+
"beste/clock": "~3.0",
4041
"simplesamlphp/assert": "~1.9",
4142
"simplesamlphp/composer-module-installer": "~1.6",
4243
"simplesamlphp/simplesamlphp": "~2.5@dev",

src/Cas/Protocol/Cas20.php

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,30 @@
2525

2626
namespace SimpleSAML\Module\casserver\Cas\Protocol;
2727

28-
use DateTimeImmutable;
29-
use SimpleSAML\CAS\XML\cas\Attributes;
30-
use SimpleSAML\CAS\XML\cas\AuthenticationDate;
31-
use SimpleSAML\CAS\XML\cas\AuthenticationFailure;
32-
use SimpleSAML\CAS\XML\cas\AuthenticationSuccess;
33-
use SimpleSAML\CAS\XML\cas\IsFromNewLogin;
34-
use SimpleSAML\CAS\XML\cas\LongTermAuthenticationRequestTokenUsed;
35-
use SimpleSAML\CAS\XML\cas\ProxyFailure;
36-
use SimpleSAML\CAS\XML\cas\ProxyGrantingTicket;
37-
use SimpleSAML\CAS\XML\cas\ProxySuccess;
38-
use SimpleSAML\CAS\XML\cas\ProxyTicket;
39-
use SimpleSAML\CAS\XML\cas\ServiceResponse;
40-
use SimpleSAML\CAS\XML\cas\User;
28+
use Beste\Clock\LocalizedClock;
29+
use DateTimeZone;
30+
use InvalidArgumentException;
31+
use SimpleSAML\CAS\Type\CodeValue;
32+
use SimpleSAML\CAS\XML\Attributes;
33+
use SimpleSAML\CAS\XML\AuthenticationDate;
34+
use SimpleSAML\CAS\XML\AuthenticationFailure;
35+
use SimpleSAML\CAS\XML\AuthenticationSuccess;
36+
use SimpleSAML\CAS\XML\IsFromNewLogin;
37+
use SimpleSAML\CAS\XML\LongTermAuthenticationRequestTokenUsed;
38+
use SimpleSAML\CAS\XML\ProxyFailure;
39+
use SimpleSAML\CAS\XML\ProxyGrantingTicket;
40+
use SimpleSAML\CAS\XML\ProxySuccess;
41+
use SimpleSAML\CAS\XML\ProxyTicket;
42+
use SimpleSAML\CAS\XML\ServiceResponse;
43+
use SimpleSAML\CAS\XML\User;
4144
use SimpleSAML\Configuration;
4245
use SimpleSAML\Logger;
46+
use SimpleSAML\XML\Assert\Assert;
4347
use SimpleSAML\XML\Chunk;
4448
use SimpleSAML\XML\DOMDocumentFactory;
49+
use SimpleSAML\XMLSchema\Type\BooleanValue;
50+
use SimpleSAML\XMLSchema\Type\DateTimeValue;
51+
use SimpleSAML\XMLSchema\Type\StringValue;
4552

4653
use function base64_encode;
4754
use function count;
@@ -121,23 +128,24 @@ public function getProxyGrantingTicketIOU(): ?string
121128
*/
122129
public function getValidateSuccessResponse(string $username): ServiceResponse
123130
{
124-
$user = new User($username);
131+
$user = new User(StringValue::fromString($username));
125132

126133
$proxyGrantingTicket = null;
127134
if (is_string($this->proxyGrantingTicketIOU)) {
128-
$proxyGrantingTicket = new ProxyGrantingTicket($this->proxyGrantingTicketIOU);
135+
$proxyGrantingTicket = new ProxyGrantingTicket(StringValue::fromString($this->proxyGrantingTicketIOU));
129136
}
130137

131138
$attr = [];
132139
if ($this->sendAttributes && count($this->attributes) > 0) {
133140
foreach ($this->attributes as $name => $values) {
134141
// Fix the most common cause of invalid XML elements
135142
$_name = str_replace(':', '_', $name);
136-
if ($this->isValidXmlName($_name) === true) {
143+
try {
144+
Assert::validNCName($_name);
137145
foreach ($values as $value) {
138146
$attr[] = $this->generateCas20Attribute($_name, $value);
139147
}
140-
} else {
148+
} catch (InvalidArgumentException) {
141149
Logger::warning("DOMException creating attribute '$_name'. Continuing without attribute'");
142150
}
143151
}
@@ -150,10 +158,11 @@ public function getValidateSuccessResponse(string $username): ServiceResponse
150158
}
151159
}
152160

161+
$systemClock = LocalizedClock::in(new DateTimeZone('Z'));
153162
$attributes = new Attributes(
154-
new AuthenticationDate(new DateTimeImmutable('now')),
155-
new LongTermAuthenticationRequestTokenUsed('true'),
156-
new IsFromNewLogin('true'),
163+
new AuthenticationDate(DateTimeValue::now($systemClock)),
164+
new LongTermAuthenticationRequestTokenUsed(BooleanValue::fromBoolean(true)),
165+
new IsFromNewLogin(BooleanValue::fromBoolean(true)),
157166
$attr,
158167
);
159168

@@ -171,7 +180,10 @@ public function getValidateSuccessResponse(string $username): ServiceResponse
171180
*/
172181
public function getValidateFailureResponse(string $errorCode, string $explanation): ServiceResponse
173182
{
174-
$authenticationFailure = new AuthenticationFailure($explanation, $errorCode);
183+
$authenticationFailure = new AuthenticationFailure(
184+
StringValue::fromString($explanation),
185+
CodeValue::fromString($errorCode),
186+
);
175187
$serviceResponse = new ServiceResponse($authenticationFailure);
176188

177189
return $serviceResponse;
@@ -184,7 +196,7 @@ public function getValidateFailureResponse(string $errorCode, string $explanatio
184196
*/
185197
public function getProxySuccessResponse(string $proxyTicketId): ServiceResponse
186198
{
187-
$proxyTicket = new ProxyTicket($proxyTicketId);
199+
$proxyTicket = new ProxyTicket(StringValue::fromString($proxyTicketId));
188200
$proxySuccess = new ProxySuccess($proxyTicket);
189201
$serviceResponse = new ServiceResponse($proxySuccess);
190202

@@ -199,7 +211,10 @@ public function getProxySuccessResponse(string $proxyTicketId): ServiceResponse
199211
*/
200212
public function getProxyFailureResponse(string $errorCode, string $explanation): ServiceResponse
201213
{
202-
$proxyFailure = new ProxyFailure($explanation, $errorCode);
214+
$proxyFailure = new ProxyFailure(
215+
StringValue::fromString($explanation),
216+
CodeValue::fromString($errorCode),
217+
);
203218
$serviceResponse = new ServiceResponse($proxyFailure);
204219

205220
return $serviceResponse;
@@ -222,26 +237,4 @@ private function generateCas20Attribute(
222237

223238
return new Chunk($attributeElement);
224239
}
225-
226-
227-
/**
228-
* XML element names have a lot of rules and not every SAML attribute name can be converted.
229-
* Ref: https://www.w3.org/TR/REC-xml/#NT-NameChar
230-
* https://stackoverflow.com/q/2519845/54396
231-
* must only start with letter or underscore
232-
* cannot start with 'xml' (or maybe it can - stackoverflow commenters don't agree)
233-
* cannot contain a ':' since those are for namespaces
234-
* cannot contains space
235-
* can only contain letters, digits, hyphens, underscores, and periods
236-
* @param string $name The attribute name to be used as an element
237-
* @return bool true if $name would make a valid xml element.
238-
*/
239-
private function isValidXmlName(string $name): bool
240-
{
241-
return filter_var(
242-
$name,
243-
FILTER_VALIDATE_REGEXP,
244-
['options' => ['regexp' => '/^[a-zA-Z_][\w.-]*$/']],
245-
) !== false;
246-
}
247240
}

src/Cas/Protocol/SamlValidateResponder.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
use SimpleSAML\Configuration;
88
use SimpleSAML\Module\casserver\Shib13\AuthnResponse;
9-
use SimpleSAML\SOAP\XML\env_200106\Body;
10-
use SimpleSAML\SOAP\XML\env_200106\Envelope;
9+
use SimpleSAML\SAML11\Constants as C;
10+
use SimpleSAML\SOAP11\XML\Body;
11+
use SimpleSAML\SOAP11\XML\Envelope;
1112
use SimpleSAML\XML\Chunk;
1213
use SimpleSAML\XML\DOMDocumentFactory;
1314
use SimpleSAML\XML\SerializableElementInterface;
@@ -48,8 +49,8 @@ public function convertToSaml(array $ticket): Chunk
4849
'<NameIdentifier$1>' . htmlspecialchars($user) . '</NameIdentifier>',
4950
$authnResponseXML,
5051
);
51-
// CAS seems to prefer this type of assertiond
52-
$ret = str_replace('urn:oasis:names:tc:SAML:1.0:cm:bearer', 'urn:oasis:names:tc:SAML:1.0:cm:artifact', $ret);
52+
// CAS seems to prefer this type of assertion
53+
$ret = str_replace(C::CM_BEARER, C::CM_ARTIFACT, $ret);
5354
// CAS uses a different namespace for attributes
5455
$ret = str_replace(
5556
'urn:mace:shibboleth:1.0:attributeNamespace:uri',
@@ -64,7 +65,7 @@ public function convertToSaml(array $ticket): Chunk
6465

6566
/**
6667
* @param \SimpleSAML\XML\SerializableElementInterface $samlResponse
67-
* @return \SimpleSAML\SOAP\XML\env_200106\Envelope
68+
* @return \SimpleSAML\SOAP11\XML\Envelope
6869
*/
6970
public function wrapInSoap(SerializableElementInterface $samlResponse): Envelope
7071
{

src/Controller/Cas30Controller.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use SimpleSAML\Module\casserver\Http\XmlResponse;
1616
use SimpleSAML\SAML11\Exception\ProtocolViolationException;
1717
use SimpleSAML\SAML11\XML\samlp\Request as SamlRequest;
18-
use SimpleSAML\SOAP\XML\env_200106\Envelope;
18+
use SimpleSAML\SOAP11\XML\Envelope;
1919
use SimpleSAML\XML\DOMDocumentFactory;
2020
use Symfony\Component\HttpFoundation\Request;
2121
use Symfony\Component\HttpFoundation\Response;
@@ -112,7 +112,7 @@ public function samlValidate(
112112
// Assertion Artifact Element
113113
$assertionArtifactParsed = $samlpRequestParsed->getRequest()[0];
114114

115-
$ticketId = $assertionArtifactParsed->getContent();
115+
$ticketId = $assertionArtifactParsed->getContent()->getValue();
116116
Logger::debug('samlvalidate: Checking ticket ' . $ticketId);
117117

118118
try {

tests/src/Cas/Protocol/SamlValidateTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use PHPUnit\Framework\TestCase;
88
use SimpleSAML\Module\casserver\Cas\Protocol\SamlValidateResponder;
9-
use SimpleSAML\SOAP\XML\env_200106\Envelope;
9+
use SimpleSAML\SOAP11\XML\Envelope;
1010

1111
class SamlValidateTest extends TestCase
1212
{

0 commit comments

Comments
 (0)