Skip to content

Commit cc89363

Browse files
committed
Migrate to cas-lib
1 parent 37c4356 commit cc89363

File tree

3 files changed

+39
-29
lines changed

3 files changed

+39
-29
lines changed

.github/workflows/php.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ on: # yamllint disable-line rule:truthy
1111
branches: [master, release-*]
1212
paths-ignore:
1313
- '**.md'
14-
workflow_dispatch
14+
workflow_dispatch:
1515

1616
jobs:
1717
linter:

composer.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,17 @@
3131
}
3232
},
3333
"require": {
34-
"php": "^8.0",
35-
"simplesamlphp/composer-module-installer": "^1.3.2",
36-
"simplesamlphp/simplesamlphp": "^3@dev",
37-
"simplesamlphp/simplesamlphp-module-ldap": "~1.2",
38-
"symfony/http-foundation": "^6.0"
34+
"php": "^7.4 || ^8.0",
35+
"ext-pcre": "*",
36+
37+
"simplesamlphp/assert": "^0.8 || ^1.0",
38+
"simplesamlphp/cas": "^1.0",
39+
"simplesamlphp/composer-module-installer": "^1.3",
40+
"simplesamlphp/simplesamlphp": "^2.0",
41+
"simplesamlphp/simplesamlphp-module-ldap": "^1.2"
3942
},
4043
"require-dev": {
41-
"simplesamlphp/simplesamlphp-test-framework": "^1.5.1"
44+
"simplesamlphp/simplesamlphp-test-framework": "^1.5"
4245
},
4346
"support": {
4447
"issues": "https://github.com/simplesamlphp/simplesamlphp-module-cas/issues",

src/Auth/Source/CAS.php

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

55
use DOMXpath;
66
use Exception;
7-
use SAML2\DOMDocumentFactory;
87
use SimpleSAML\Auth;
8+
use SimpleSAML\CAS\XML\cas\AuthenticationFailure;
9+
use SimpleSAML\CAS\XML\cas\AuthenticationSuccess;
10+
use SimpleSAML\CAS\XML\cas\ServiceResponse;
11+
use SimpleSAML\CAS\Utils\XPath;
912
use SimpleSAML\Configuration;
1013
use SimpleSAML\Module;
1114
use SimpleSAML\Module\ldap\Auth\Ldap;
1215
use SimpleSAML\Utils;
16+
use SimpleSAML\XML\DOMDocumentFactory;
1317

1418
use function array_key_exists;
1519
use function array_merge_recursive;
@@ -147,32 +151,34 @@ private function casServiceValidate(string $ticket, string $service): array
147151

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

168-
$item = $success->item(0);
169-
if (is_null($item)) {
170-
throw new Exception("Error parsing serviceResponse.");
171-
}
172-
$casusername = $item->textContent;
173-
174-
return [$casusername, $attributes];
178+
return [$user, $attributes];
175179
}
180+
181+
throw new Exception("Error parsing serviceResponse.");
176182
}
177183

178184

@@ -206,8 +212,8 @@ public function finalStep(array &$state): void
206212
$ticket = $state['cas:ticket'];
207213
$stateId = Auth\State::saveState($state, self::STAGE_INIT);
208214
$service = Module::getModuleURL('cas/linkback.php', ['stateId' => $stateId]);
209-
list($username, $casattributes) = $this->casValidation($ticket, $service);
210-
$ldapattributes = [];
215+
list($username, $casAttributes) = $this->casValidation($ticket, $service);
216+
$ldapAttributes = [];
211217

212218
$config = Configuration::loadFromArray(
213219
$this->ldapConfig,
@@ -222,12 +228,13 @@ public function finalStep(array &$state): void
222228
$config->getOptionalInteger('port', 389),
223229
$config->getOptionalBoolean('referrals', true)
224230
);
225-
$ldapattributes = $ldap->validate($this->ldapConfig, $username);
226-
if ($ldapattributes === false) {
231+
232+
$ldapAttributes = $ldap->validate($this->ldapConfig, $username);
233+
if ($ldapAttributes === false) {
227234
throw new Exception("Failed to authenticate against LDAP-server.");
228235
}
229236
}
230-
$attributes = array_merge_recursive($casattributes, $ldapattributes);
237+
$attributes = array_merge_recursive($casAttributes, $ldapAttributes);
231238
$state['Attributes'] = $attributes;
232239
}
233240

0 commit comments

Comments
 (0)