Skip to content

Commit 72618dc

Browse files
committed
Migrate to cas-lib
1 parent 85dafed commit 72618dc

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
},
3535
"require": {
3636
"php": "^8.1",
37+
"ext-pcre": "*",
38+
39+
"simplesamlphp/assert": "^0.8 || ^1.0",
40+
"simplesamlphp/cas": "^1.0",
3741
"simplesamlphp/composer-module-installer": "^1.3.4",
3842
"simplesamlphp/simplesamlphp": "^3@dev",
3943
"simplesamlphp/simplesamlphp-module-ldap": "~1.2",

src/Auth/Source/CAS.php

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66

77
use DOMXpath;
88
use Exception;
9-
use SAML2\DOMDocumentFactory;
109
use SimpleSAML\Auth;
10+
use SimpleSAML\CAS\XML\cas\AuthenticationFailure;
11+
use SimpleSAML\CAS\XML\cas\AuthenticationSuccess;
12+
use SimpleSAML\CAS\XML\cas\ServiceResponse;
13+
use SimpleSAML\CAS\Utils\XPath;
1114
use SimpleSAML\Configuration;
1215
use SimpleSAML\Module;
1316
use SimpleSAML\Module\ldap\Auth\Ldap;
1417
use SimpleSAML\Utils;
18+
use SimpleSAML\XML\DOMDocumentFactory;
1519

1620
use function array_key_exists;
1721
use function array_merge_recursive;
@@ -149,32 +153,34 @@ private function casServiceValidate(string $ticket, string $service): array
149153

150154
/** @var string $result */
151155
$dom = DOMDocumentFactory::fromString($result);
152-
$xPath = new DOMXpath($dom);
153-
$xPath->registerNamespace("cas", 'http://www.yale.edu/tp/cas');
154-
$success = $xPath->query("/cas:serviceResponse/cas:authenticationSuccess/cas:user");
155-
if ($success->length == 0) {
156-
$failure = $xPath->evaluate("/cas:serviceResponse/cas:authenticationFailure");
157-
throw new Exception("Error when validating CAS service ticket: " . $failure->item(0)->textContent);
158-
} else {
156+
157+
$serviceResponse = ServiceResponse::fromXML($dom->documentElement);
158+
$message = $serviceResponse->getResponse();
159+
if ($message instanceof AuthenticationFailure) {
160+
throw new Exception(sprintf(
161+
"Error when validating CAS service ticket: %s (%s)",
162+
$message->getContent(),
163+
$message->getCode(),
164+
));
165+
} elseif ($message instanceof AuthenticationSuccess) {
166+
$user = $message->getUser()->getContent();
167+
$xPath = XPath::getXPath();
168+
159169
$attributes = [];
160170
if ($casattributes = $this->casConfig['attributes']) {
161-
// Some has attributes in the xml - attributes is a list of XPath expressions to get them
171+
// Some have attributes in the xml - attributes is a list of XPath expressions to get them
162172
foreach ($casattributes as $name => $query) {
163-
$attrs = $xPath->query($query);
173+
$attrs = $xPath->xpQuery($query, $xPath);
164174
foreach ($attrs as $attrvalue) {
165175
$attributes[$name][] = $attrvalue->textContent;
166176
}
167177
}
168178
}
169179

170-
$item = $success->item(0);
171-
if (is_null($item)) {
172-
throw new Exception("Error parsing serviceResponse.");
173-
}
174-
$casusername = $item->textContent;
175-
176-
return [$casusername, $attributes];
180+
return [$user, $attributes];
177181
}
182+
183+
throw new Exception("Error parsing serviceResponse.");
178184
}
179185

180186

@@ -208,8 +214,8 @@ public function finalStep(array &$state): void
208214
$ticket = $state['cas:ticket'];
209215
$stateId = Auth\State::saveState($state, self::STAGE_INIT);
210216
$service = Module::getModuleURL('cas/linkback.php', ['stateId' => $stateId]);
211-
list($username, $casattributes) = $this->casValidation($ticket, $service);
212-
$ldapattributes = [];
217+
list($username, $casAttributes) = $this->casValidation($ticket, $service);
218+
$ldapAttributes = [];
213219

214220
$config = Configuration::loadFromArray(
215221
$this->ldapConfig,
@@ -224,12 +230,13 @@ public function finalStep(array &$state): void
224230
$config->getOptionalInteger('port', 389),
225231
$config->getOptionalBoolean('referrals', true),
226232
);
227-
$ldapattributes = $ldap->validate($this->ldapConfig, $username);
228-
if ($ldapattributes === false) {
233+
234+
$ldapAttributes = $ldap->validate($this->ldapConfig, $username);
235+
if ($ldapAttributes === false) {
229236
throw new Exception("Failed to authenticate against LDAP-server.");
230237
}
231238
}
232-
$attributes = array_merge_recursive($casattributes, $ldapattributes);
239+
$attributes = array_merge_recursive($casAttributes, $ldapAttributes);
233240
$state['Attributes'] = $attributes;
234241
}
235242

0 commit comments

Comments
 (0)