Skip to content

Commit bd853cd

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

File tree

4 files changed

+32
-38
lines changed

4 files changed

+32
-38
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: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525

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

28-
use DateTimeImmutable;
28+
use Beste\Clock\LocalizedClock;
29+
use DateTimeZone;
30+
use InvalidArgumentException;
31+
use SimpleSAML\CAS\Type\CodeValue;
2932
use SimpleSAML\CAS\XML\cas\Attributes;
3033
use SimpleSAML\CAS\XML\cas\AuthenticationDate;
3134
use SimpleSAML\CAS\XML\cas\AuthenticationFailure;
@@ -42,6 +45,9 @@
4245
use SimpleSAML\Logger;
4346
use SimpleSAML\XML\Chunk;
4447
use SimpleSAML\XML\DOMDocumentFactory;
48+
use SimpleSAML\XMLSchema\Type\BooleanValue;
49+
use SimpleSAML\XMLSchema\Type\DateTimeValue;
50+
use SimpleSAML\XMLSchema\Type\StringValue;
4551

4652
use function base64_encode;
4753
use function count;
@@ -121,23 +127,24 @@ public function getProxyGrantingTicketIOU(): ?string
121127
*/
122128
public function getValidateSuccessResponse(string $username): ServiceResponse
123129
{
124-
$user = new User($username);
130+
$user = new User(StringValue::fromString($username));
125131

126132
$proxyGrantingTicket = null;
127133
if (is_string($this->proxyGrantingTicketIOU)) {
128-
$proxyGrantingTicket = new ProxyGrantingTicket($this->proxyGrantingTicketIOU);
134+
$proxyGrantingTicket = new ProxyGrantingTicket(StringValue::fromString($this->proxyGrantingTicketIOU));
129135
}
130136

131137
$attr = [];
132138
if ($this->sendAttributes && count($this->attributes) > 0) {
133139
foreach ($this->attributes as $name => $values) {
134140
// Fix the most common cause of invalid XML elements
135141
$_name = str_replace(':', '_', $name);
136-
if ($this->isValidXmlName($_name) === true) {
142+
try {
143+
Assert::validNCName($_name);
137144
foreach ($values as $value) {
138145
$attr[] = $this->generateCas20Attribute($_name, $value);
139146
}
140-
} else {
147+
} catch (InvalidArgumentException) {
141148
Logger::warning("DOMException creating attribute '$_name'. Continuing without attribute'");
142149
}
143150
}
@@ -150,10 +157,11 @@ public function getValidateSuccessResponse(string $username): ServiceResponse
150157
}
151158
}
152159

160+
$systemClock = LocalizedClock::in(new DateTimeZone('Z'));
153161
$attributes = new Attributes(
154-
new AuthenticationDate(new DateTimeImmutable('now')),
155-
new LongTermAuthenticationRequestTokenUsed('true'),
156-
new IsFromNewLogin('true'),
162+
new AuthenticationDate(DateTimeValue::now($systemClock)),
163+
new LongTermAuthenticationRequestTokenUsed(BooleanValue::fromBoolean(true)),
164+
new IsFromNewLogin(BooleanValue::fromBoolean(true)),
157165
$attr,
158166
);
159167

@@ -171,7 +179,10 @@ public function getValidateSuccessResponse(string $username): ServiceResponse
171179
*/
172180
public function getValidateFailureResponse(string $errorCode, string $explanation): ServiceResponse
173181
{
174-
$authenticationFailure = new AuthenticationFailure($explanation, $errorCode);
182+
$authenticationFailure = new AuthenticationFailure(
183+
StringValue::fromString($explanation),
184+
CodeValue::fromString($errorCode),
185+
);
175186
$serviceResponse = new ServiceResponse($authenticationFailure);
176187

177188
return $serviceResponse;
@@ -184,7 +195,7 @@ public function getValidateFailureResponse(string $errorCode, string $explanatio
184195
*/
185196
public function getProxySuccessResponse(string $proxyTicketId): ServiceResponse
186197
{
187-
$proxyTicket = new ProxyTicket($proxyTicketId);
198+
$proxyTicket = new ProxyTicket(StringValue::fromString($proxyTicketId));
188199
$proxySuccess = new ProxySuccess($proxyTicket);
189200
$serviceResponse = new ServiceResponse($proxySuccess);
190201

@@ -199,7 +210,10 @@ public function getProxySuccessResponse(string $proxyTicketId): ServiceResponse
199210
*/
200211
public function getProxyFailureResponse(string $errorCode, string $explanation): ServiceResponse
201212
{
202-
$proxyFailure = new ProxyFailure($explanation, $errorCode);
213+
$proxyFailure = new ProxyFailure(
214+
StringValue::fromString($explanation),
215+
CodeValue::fromString($errorCode),
216+
);
203217
$serviceResponse = new ServiceResponse($proxyFailure);
204218

205219
return $serviceResponse;
@@ -222,26 +236,4 @@ private function generateCas20Attribute(
222236

223237
return new Chunk($attributeElement);
224238
}
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-
}
247239
}

src/Cas/Protocol/SamlValidateResponder.php

Lines changed: 5 additions & 4 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',

src/Controller/Cas30Controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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 {

0 commit comments

Comments
 (0)