Skip to content

Commit 50c2959

Browse files
committed
Merge branch 'master' into feat-fticks-before-login
Bring in the debug-mode fixes (#87, #88). The only conflict was in docs/ChangeLog.md, where both sides added [Unreleased] entries; they are now combined into one Changed and one Fixed section. Assisted-by: Claude Code (claude-opus-5-5, conflict resolution)
2 parents abefa7f + c1dd40a commit 50c2959

4 files changed

Lines changed: 181 additions & 7 deletions

File tree

‎docs/ChangeLog.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
### Changed
66

77
- Give the authentication source Source and Destination before login, so that it can report a failed login against them (Marko Ivančić)
8+
- The test suite now reports deprecations, including those triggered with `@` such as Symfony's `trigger_deprecation()`
9+
10+
### Fixed
11+
12+
- Debug mode (`debugMode=true`) now validates the ticket with the query parameters login used to issue it,
13+
so it works for a service given as `TARGET` and with `renew` set, and ignores parameters in the request body (Marko Ivančić)
14+
- Debug mode no longer passes the validation XML to the error template for an unsupported debug mode
15+
(the `str_contains()` check for the error template had its arguments reversed)
816

917
## [7.0.6] - 2026-11-10
1018

‎phpunit.xml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/12.5/phpunit.xsd"
66
backupGlobals="false"
77
displayDetailsOnTestsThatTriggerWarnings="true"
8+
displayDetailsOnTestsThatTriggerDeprecations="true"
89
cacheDirectory=".phpunit.cache">
910
<coverage>
1011
<report>
@@ -19,7 +20,7 @@
1920
</testsuite>
2021
</testsuites>
2122
<logging/>
22-
<source>
23+
<source ignoreSuppressionOfDeprecations="true">
2324
<include>
2425
<directory suffix=".php">./src</directory>
2526
</include>

‎src/Controller/LoginController.php‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,18 @@ public function __construct(
115115
* @param bool $renew
116116
* @param bool $gateway
117117
* @param string|null $service
118-
* @param string|null $TARGET Query parameter name for "service" used by older CAS clients'
118+
* @param string|null $TARGET Query parameter name for "service" used by older CAS clients'
119119
* @param string|null $scope
120120
* @param string|null $language
121121
* @param string|null $entityId
122122
* @param string|null $debugMode
123123
* @param string|null $method
124+
* @param string|null $pgtUrl Only used by debug mode, which passes it on to the ticket validation
124125
*
125126
* @return \SimpleSAML\HTTP\RunnableResponse|\SimpleSAML\XHTML\Template
126127
* @throws \SimpleSAML\Error\ConfigurationError
127128
* @throws \SimpleSAML\Error\NoState
129+
* @throws \Exception
128130
*/
129131
public function login(
130132
Request $request,
@@ -137,6 +139,7 @@ public function login(
137139
#[MapQueryParameter] ?string $entityId = null,
138140
#[MapQueryParameter] ?string $debugMode = null,
139141
#[MapQueryParameter] ?string $method = null,
142+
#[MapQueryParameter] ?string $pgtUrl = null,
140143
): RunnableResponse|Template {
141144
$forceAuthn = $renew;
142145
$serviceUrl = $service ?? $TARGET ?? null;
@@ -255,14 +258,19 @@ public function login(
255258

256259
// Check if we are in debug mode.
257260
if ($debugMode !== null && $this->casConfig->getOptionalBoolean('debugMode', false)) {
261+
// Validate with the parameters the ticket was issued with, not with whatever the request holds
258262
[$templateName, $statusCode, $DebugModeXmlString] = $this->handleDebugMode(
259263
$request,
260264
$debugMode,
261265
$serviceTicket,
266+
$renew,
267+
$TARGET,
268+
$service,
269+
$pgtUrl,
262270
);
263271
$t = new Template($this->sspConfig, (string)$templateName);
264272
$t->data['debugMode'] = $debugMode === 'true' ? 'Default' : $debugMode;
265-
if (!str_contains('error', (string)$templateName)) {
273+
if (!str_contains((string)$templateName, 'error')) {
266274
$t->data['DebugModeXml'] = $DebugModeXmlString;
267275
}
268276
$t->data['statusCode'] = $statusCode;
@@ -291,16 +299,29 @@ public function login(
291299

292300

293301
/**
302+
* Validate the ticket login() has just issued, for debug mode to show the response.
303+
*
304+
* $renew, $target, $service and $pgtUrl are login()'s query parameters as it mapped them, so the
305+
* ticket is validated under the conditions it was issued with.
306+
*
294307
* @param \Symfony\Component\HttpFoundation\Request $request
295308
* @param string|null $debugMode
296309
* @param array $serviceTicket
310+
* @param bool $renew
311+
* @param string|null $target
312+
* @param string|null $service
313+
* @param string|null $pgtUrl
297314
*
298315
* @return array []
299316
*/
300317
public function handleDebugMode(
301318
Request $request,
302319
?string $debugMode,
303320
array $serviceTicket,
321+
bool $renew = false,
322+
?string $target = null,
323+
?string $service = null,
324+
?string $pgtUrl = null,
304325
): array {
305326
// Check if the debugMode is supported
306327
if (!in_array($debugMode, self::DEBUG_MODES, true)) {
@@ -312,11 +333,11 @@ public function handleDebugMode(
312333
$xmlResponse = $this->validate(
313334
request: $request,
314335
method: 'serviceValidate',
315-
renew: $request->get('renew', false),
316-
target: $request->get('target'),
336+
renew: $renew,
337+
target: $target,
317338
ticket: $serviceTicket['id'],
318-
service: $request->get('service'),
319-
pgtUrl: $request->get('pgtUrl'),
339+
service: $service,
340+
pgtUrl: $pgtUrl,
320341
);
321342
return ['casserver:validate.twig', $xmlResponse->getStatusCode(), $xmlResponse->getContent()];
322343
}

‎tests/src/Controller/LoginControllerTest.php‎

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use SimpleSAML\Module\casserver\Controller\LoginController;
1818
use SimpleSAML\Session;
1919
use SimpleSAML\Utils;
20+
use SimpleSAML\XHTML\Template;
2021
use Symfony\Component\HttpFoundation\Request;
2122
use Symfony\Component\HttpFoundation\Response;
2223

@@ -106,6 +107,7 @@ private function callLogin(LoginController $controller, Request $request, array
106107
entityId: isset($params['entityId']) ? (string)$params['entityId'] : null,
107108
debugMode: isset($params['debugMode']) ? (string)$params['debugMode'] : null,
108109
method: isset($params['method']) ? (string)$params['method'] : null,
110+
pgtUrl: isset($params['pgtUrl']) ? (string)$params['pgtUrl'] : null,
109111
);
110112
}
111113

@@ -1086,4 +1088,146 @@ public function testAuthenticatedPostSubmitsViaPostWithTicket(): void
10861088
$this->assertArrayHasKey('ticket', $params);
10871089
$this->assertStringStartsWith('ST-', $params['ticket']);
10881090
}
1091+
1092+
1093+
/**
1094+
* @return array<string, array{array<string, string>}>
1095+
*/
1096+
public static function debugModeServiceValidateProvider(): array
1097+
{
1098+
$service = 'https://example.com/ssp/module.php/cas/linkback.php';
1099+
return [
1100+
'service' => [['service' => $service, 'debugMode' => 'true']],
1101+
'TARGET' => [['TARGET' => $service, 'debugMode' => 'true']],
1102+
'service with renew' => [['service' => $service, 'debugMode' => 'true', 'renew' => 'true']],
1103+
'TARGET with renew' => [['TARGET' => $service, 'debugMode' => 'true', 'renew' => 'true']],
1104+
];
1105+
}
1106+
1107+
1108+
/**
1109+
* Debug mode validates the ticket it has just issued against the service it was issued for, whichever query
1110+
* parameter named the service, and whether or not renew was requested.
1111+
*
1112+
* @param array<string, string> $requestParams
1113+
*
1114+
* @throws \Exception
1115+
*/
1116+
#[DataProvider('debugModeServiceValidateProvider')]
1117+
public function testDebugModeValidatesTheIssuedTicket(array $requestParams): void
1118+
{
1119+
$loginRequest = Request::create(
1120+
uri: Module::getModuleURL('casserver/login'),
1121+
parameters: $requestParams,
1122+
);
1123+
$response = $this->callLogin($this->createDebugModeController(), $loginRequest, $requestParams);
1124+
1125+
$this->assertDebugModeValidated($response);
1126+
}
1127+
1128+
1129+
/**
1130+
* @return array<string, array{array<string, string>, array<string, mixed>}>
1131+
*/
1132+
public static function debugModeRequestBodyProvider(): array
1133+
{
1134+
$service = 'https://example.com/ssp/module.php/cas/linkback.php';
1135+
return [
1136+
// Would ask for a ticket from a fresh login, which this one, issued from the SSO session, is not
1137+
'renew' => [['service' => $service, 'debugMode' => 'true'], ['renew' => 'true']],
1138+
// Would name a service the ticket was not issued for
1139+
'service' => [['TARGET' => $service, 'debugMode' => 'true'], ['service' => 'https://example.com/other']],
1140+
// Would not be a string
1141+
'TARGET array' => [['service' => $service, 'debugMode' => 'true'], ['TARGET' => ['x']]],
1142+
];
1143+
}
1144+
1145+
1146+
/**
1147+
* Debug mode validates the ticket with the query parameters login() issued it with, so parameters
1148+
* in the POST body cannot change the result.
1149+
*
1150+
* @param array<string, string> $queryParams
1151+
* @param array<string, mixed> $bodyParams
1152+
*
1153+
* @throws \Exception
1154+
*/
1155+
#[DataProvider('debugModeRequestBodyProvider')]
1156+
public function testDebugModeIgnoresTheRequestBody(array $queryParams, array $bodyParams): void
1157+
{
1158+
$loginRequest = Request::create(
1159+
uri: Module::getModuleURL('casserver/login') . '?' . http_build_query($queryParams),
1160+
method: 'POST',
1161+
parameters: $bodyParams,
1162+
);
1163+
$response = $this->callLogin($this->createDebugModeController(), $loginRequest, $queryParams);
1164+
1165+
$this->assertDebugModeValidated($response);
1166+
}
1167+
1168+
1169+
/**
1170+
* An unsupported debug mode renders the error template, without any XML to show.
1171+
*
1172+
* @throws \Exception
1173+
*/
1174+
public function testUnsupportedDebugModeRendersTheErrorWithoutXml(): void
1175+
{
1176+
$requestParams = [
1177+
'service' => 'https://example.com/ssp/module.php/cas/linkback.php',
1178+
'debugMode' => 'unsupported',
1179+
];
1180+
$loginRequest = Request::create(
1181+
uri: Module::getModuleURL('casserver/login'),
1182+
parameters: $requestParams,
1183+
);
1184+
$response = $this->callLogin($this->createDebugModeController(), $loginRequest, $requestParams);
1185+
1186+
$this->assertInstanceOf(Template::class, $response);
1187+
$this->assertSame(Response::HTTP_BAD_REQUEST, $response->data['statusCode']);
1188+
$this->assertSame('unsupported', $response->data['debugMode']);
1189+
$this->assertArrayNotHasKey('DebugModeXml', $response->data);
1190+
}
1191+
1192+
1193+
/**
1194+
* A controller with debug mode enabled, for a user with an SSO session.
1195+
*/
1196+
private function createDebugModeController(): LoginController
1197+
{
1198+
$moduleConfig = $this->moduleConfig;
1199+
$moduleConfig['debugMode'] = true;
1200+
$casconfig = Configuration::loadFromArray($moduleConfig);
1201+
1202+
$controllerMock = $this->getMockBuilder(LoginController::class)
1203+
->setConstructorArgs([$this->sspConfig, $casconfig, $this->authSimpleMock, $this->httpUtils])
1204+
->onlyMethods(['getSession'])
1205+
->getMock();
1206+
$controllerMock->expects($this->once())->method('getSession')->willReturn($this->sessionMock);
1207+
$this->sessionMock->expects($this->exactly(2))->method('getSessionId')->willReturn(session_create_id());
1208+
$this->authSimpleMock->expects($this->atLeastOnce())->method('isAuthenticated')->willReturn(true);
1209+
$this->authSimpleMock->expects($this->once())->method('getAuthData')->with('Expire')->willReturn(9999999999);
1210+
$this->authSimpleMock->expects($this->once())->method('getAuthDataArray')->willReturn([
1211+
'Attributes' => ['eduPersonPrincipalName' => ['testuser@example.com']],
1212+
'Expire' => 9999999999,
1213+
]);
1214+
1215+
return $controllerMock;
1216+
}
1217+
1218+
1219+
/**
1220+
* Debug mode rendered a successful serviceValidate response for the test user.
1221+
*/
1222+
private function assertDebugModeValidated(mixed $response): void
1223+
{
1224+
$this->assertInstanceOf(Template::class, $response);
1225+
$this->assertSame(Response::HTTP_OK, $response->data['statusCode']);
1226+
$xml = simplexml_load_string((string)$response->data['DebugModeXml']);
1227+
$xml->registerXPathNamespace('cas', 'serviceResponse');
1228+
$this->assertSame(
1229+
'testuser@example.com',
1230+
(string)$xml->xpath('//cas:authenticationSuccess/cas:user')[0],
1231+
);
1232+
}
10891233
}

0 commit comments

Comments
 (0)