Skip to content

Commit bd3b749

Browse files
authored
Give the authentication source Source and Destination before login (#86)
An authentication source that reports failed logins, for example in F-ticks, reads $state['Source']['entityid'] and $state['Destination']['entityid']. casserver set them only after login, for the authproc filters, so a failed CAS login named neither entity. handleAuthenticate() now passes both to Auth\Simple::login(), with the values the filters see after a successful login: Source comes from resolveIdpEntityId(), now shared with runAuthProcs(), and Destination is the requesting service, left out when the request names none. This covers interactive, renewed and passive logins. A session keeps neither key, so login()'s check that a resumed state belongs to the requested service does not bind a session to its first service. The tests pin both the equal values and that. Assisted-by: Claude Code (claude-opus-5-5, implementation & unit tests) Assisted-by: Google Antigravity (gemini-3.8-flash-high, review)
1 parent c1dd40a commit bd3b749

5 files changed

Lines changed: 263 additions & 6 deletions

File tree

‎config/module_casserver.php.dist‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ $config = [
107107
* as $state['Source']['entityid']. Filters that key off the authenticating entity (federation
108108
* logging such as F-ticks, per-entity authorization, audit trails) need it. The requesting CAS
109109
* service URL is made available to the same filters as $state['Destination']['entityid'].
110+
* The authentication source gets both in the login state before the user logs in, so that it can
111+
* report a failed login against them.
110112
*
111113
* NOTE: this is unrelated to the ?entityId= query parameter of the login endpoint, which selects
112114
* which upstream SAML IdP the authsource should authenticate against.

‎docs/ChangeLog.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
## [Unreleased]
44

5+
### Changed
6+
7+
- 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+
510
### Fixed
611

712
- Debug mode (`debugMode=true`) now validates the ticket with the query parameters login used to issue it,
813
so it works for a service given as `TARGET` and with `renew` set, and ignores parameters in the request body (Marko Ivančić)
914
- Debug mode no longer passes the validation XML to the error template for an unsupported debug mode
1015
(the `str_contains()` check for the error template had its arguments reversed)
1116

12-
### Changed
13-
14-
- The test suite now reports deprecations, including those triggered with `@` such as Symfony's `trigger_deprecation()`
15-
1617
## [7.0.6] - 2026-11-10
1718

1819
### Security

‎src/Cas/AttributeExtractor.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ protected function runAuthProcs(array &$state): void
140140

141141

142142
/**
143-
* Resolve the entity ID that casserver presents to the authproc filters as the authenticating IdP.
143+
* Resolve the entity ID that casserver presents as the authenticating IdP, to the authentication
144+
* source before login and to the authproc filters after it.
144145
*
145146
* casserver has no entity ID of its own, so the value is resolved in the following order:
146147
* whatever the state already carries, then the 'idp_entity_id' configuration option, and finally
@@ -152,7 +153,7 @@ protected function runAuthProcs(array &$state): void
152153
* @return string
153154
* @throws \Exception
154155
*/
155-
private function resolveIdpEntityId(mixed $stateEntityId): string
156+
public function resolveIdpEntityId(mixed $stateEntityId = null): string
156157
{
157158
if (\is_string($stateEntityId) && $stateEntityId !== '') {
158159
return $stateEntityId;

‎src/Controller/LoginController.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ public function login(
196196
forceAuthn: $forceAuthn,
197197
returnToUrl: $returnToUrl,
198198
entityId: $entityId,
199+
serviceUrl: $serviceUrl,
199200
);
200201
}
201202

@@ -523,19 +524,22 @@ private function instantiateClassDependencies(): void
523524
* @param bool $forceAuthn
524525
* @param string $returnToUrl
525526
* @param string|null $entityId
527+
* @param string|null $serviceUrl
526528
*
527529
* @return \SimpleSAML\HTTP\RunnableResponse
528530
*/
529531
private function handleInteractiveAuthenticate(
530532
bool $forceAuthn,
531533
string $returnToUrl,
532534
?string $entityId,
535+
?string $serviceUrl,
533536
): RunnableResponse {
534537
return $this->handleAuthenticate(
535538
forceAuthn: $forceAuthn,
536539
gateway: false,
537540
returnToUrl: $returnToUrl,
538541
entityId: $entityId,
542+
serviceUrl: $serviceUrl,
539543
);
540544
}
541545

@@ -570,6 +574,7 @@ private function handleUnauthenticatedGateway(
570574
gateway: true,
571575
returnToUrl: $returnToUrl,
572576
entityId: $entityId,
577+
serviceUrl: $serviceUrl,
573578
);
574579
}
575580

@@ -581,6 +586,7 @@ private function handleUnauthenticatedGateway(
581586
* @param bool $gateway Whether authentication should be passive/non-interactive
582587
* @param string $returnToUrl URL to return to after authentication
583588
* @param string|null $entityId Optional specific IdP entity ID to use
589+
* @param string|null $serviceUrl The requesting CAS service, if the request names one
584590
*
585591
* @return \SimpleSAML\HTTP\RunnableResponse Response containing the login redirect
586592
*/
@@ -589,13 +595,22 @@ private function handleAuthenticate(
589595
bool $gateway,
590596
string $returnToUrl,
591597
?string $entityId,
598+
?string $serviceUrl,
592599
): RunnableResponse {
593600
$params = [
594601
'ForceAuthn' => $forceAuthn,
595602
'isPassive' => $gateway,
596603
'ReturnTo' => $returnToUrl,
604+
// The same entities the authproc filters see after a successful login, so that an authentication
605+
// source can report a failed login against them. They end with the login state: a session does not
606+
// keep them, so they do not bind it to this service.
607+
'Source' => ['entityid' => $this->attributeExtractor->resolveIdpEntityId()],
597608
];
598609

610+
if (!empty($serviceUrl)) {
611+
$params['Destination'] = ['entityid' => $serviceUrl];
612+
}
613+
599614
if (isset($entityId)) {
600615
$params['saml:idp'] = $entityId;
601616
}

0 commit comments

Comments
 (0)