Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,20 @@ public function login(
// This will be used to come back from the AuthSource login or from the Processing Chain
$returnToUrl = $this->getReturnUrl($request, $sessionTicket);

/*
* CAS gateway behavior:
* If gateway=true, service is valid, and the user is not authenticated,
* redirect immediately to the service URL with NO query parameters or fragment.
*/
if ($gateway === true && $serviceUrl !== null && !$this->authSource->isAuthenticated()) {
$cleanServiceUrl = $this->stripQueryParameters($serviceUrl);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't line up with my reading of the spec. I don't think you need to manipulate the service url. The service url is allowed to have query parameters. I think the spec just says "do not add a ticket query parameter" and redirect the user to that serviceUrl.

From https://apereo.github.io/cas/7.3.x/protocol/CAS-Protocol-Specification.html

If the client does not have a single sign-on session with CAS, and a non-interactive authentication cannot be established, CAS MUST redirect the client to the URL specified by the service parameter with no “ticket” parameter appended to the URL.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read it the same way.. Just the ticket-param should be stripped

return new RunnableResponse(
[$this->httpUtils, 'redirectTrustedURL'],
[$cleanServiceUrl]
);
}


// Authenticate
if (
$requestForceAuthenticate || !$this->authSource->isAuthenticated()
Expand Down Expand Up @@ -464,4 +478,32 @@ private function instantiateClassDependencies(): void
// Attribute Extractor
$this->attributeExtractor = new AttributeExtractor($this->casConfig, $processingChainFactory);
}

/**
* Remove query string from a URL while preserving scheme, userinfo, host, port, path and fragment.
*
* @param string $url
* @return string
*/
private function stripQueryParameters(string $url): string
{
$parts = parse_url($url);

$scheme = $parts['scheme'] ?? '';
$host = $parts['host'] ?? '';
$port = isset($parts['port']) ? ':' . $parts['port'] : '';
$user = $parts['user'] ?? null;
$pass = $parts['pass'] ?? null;
$userInfo = $user ? $user . ($pass ? ':' . $pass : '') . '@' : '';
$path = $parts['path'] ?? '';
$fragment = isset($parts['fragment']) ? '#' . $parts['fragment'] : '';

// Ensure root path is preserved if it was "/"
if ($path === '' && (($parts['path'] ?? '') === '/')) {
$path = '/';
}

return sprintf('%s://%s%s%s%s%s', $scheme, $userInfo, $host, $port, $path, $fragment);
}

}
Loading