Skip to content

Commit

Permalink
Move Cas20 validate function to TicketValidatorTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
ioigoume committed Jan 9, 2025
1 parent 3d4d2ef commit b659110
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 176 deletions.
2 changes: 2 additions & 0 deletions src/Controller/Cas20Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use SimpleSAML\Module\casserver\Cas\Factories\TicketFactory;
use SimpleSAML\Module\casserver\Cas\Protocol\Cas20;
use SimpleSAML\Module\casserver\Cas\Ticket\TicketStore;
use SimpleSAML\Module\casserver\Controller\Traits\TicketValidatorTrait;
use SimpleSAML\Module\casserver\Controller\Traits\UrlTrait;
use SimpleSAML\Module\casserver\Http\XmlResponse;
use SimpleSAML\Utils;
Expand All @@ -23,6 +24,7 @@
class Cas20Controller
{
use UrlTrait;
use TicketValidatorTrait;

/** @var Logger */
protected Logger $logger;

Check warning on line 30 in src/Controller/Cas20Controller.php

View workflow job for this annotation

GitHub Actions / Quality control

PropertyNotSetInConstructor

src/Controller/Cas20Controller.php:30:22: PropertyNotSetInConstructor: Property SimpleSAML\Module\casserver\Controller\Cas20Controller::$logger is not defined in constructor of SimpleSAML\Module\casserver\Controller\Cas20Controller or in any methods called in the constructor (see https://psalm.dev/074)
Expand Down
1 change: 0 additions & 1 deletion src/Controller/Cas30Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ public function samlValidate(
Request $request,
#[MapQueryParameter] string $TARGET,
): XmlResponse {
// From SAML2\SOAP::receive()
$postBody = $request->getContent();
if (empty($postBody)) {
throw new \RuntimeException('samlValidate expects a soap body.');
Expand Down
184 changes: 184 additions & 0 deletions src/Controller/Traits/TicketValidatorTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Module\casserver\Controller\Traits;

use SimpleSAML\CAS\Constants as C;
use SimpleSAML\Logger;
use SimpleSAML\Module\casserver\Http\XmlResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

trait TicketValidatorTrait
{
/**
* @param Request $request
* @param string $method
* @param bool $renew
* @param string|null $target
* @param string|null $ticket
* @param string|null $service
* @param string|null $pgtUrl
*
* @return XmlResponse
*/
public function validate(
Request $request,
string $method,
bool $renew = false,
?string $target = null,
?string $ticket = null,
?string $service = null,
?string $pgtUrl = null,
): XmlResponse {
$forceAuthn = $renew;
$serviceUrl = $service ?? $target ?? null;

// Check if any of the required query parameters are missing
if ($serviceUrl === null || $ticket === null) {
$messagePostfix = $serviceUrl === null ? 'service' : 'ticket';
$message = "casserver: Missing {$messagePostfix} parameter: [{$messagePostfix}]";
Logger::debug($message);

return new XmlResponse(
(string)$this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message),
Response::HTTP_BAD_REQUEST,
);
}

try {
// Get the service ticket
// `getTicket` uses the unserializable method and Objects may throw Throwables in their
// un-serialization handlers.
$serviceTicket = $this->ticketStore->getTicket($ticket);
} catch (\Exception $e) {
$messagePostfix = '';
if (!empty($e->getMessage())) {
$messagePostfix = ': ' . var_export($e->getMessage(), true);
}
$message = 'casserver:serviceValidate: internal server error' . $messagePostfix;
Logger::error($message);

return new XmlResponse(
(string)$this->cas20Protocol->getValidateFailureResponse(C::ERR_INTERNAL_ERROR, $message),
Response::HTTP_INTERNAL_SERVER_ERROR,
);
}

$failed = false;
$message = '';
// Below, we do not have a ticket or the ticket does not meet the very basic criteria that allow
// any further handling
if (empty($serviceTicket)) {
// No ticket
$message = 'Ticket ' . var_export($ticket, true) . ' not recognized';
$failed = true;
} elseif ($method === 'proxyValidate' && !$this->ticketFactory->isProxyTicket($serviceTicket)) {
// proxyValidate but not a proxy ticket
$message = 'Ticket ' . var_export($ticket, true) . ' is not a proxy ticket.';
$failed = true;
} elseif ($method === 'serviceValidate' && !$this->ticketFactory->isServiceTicket($serviceTicket)) {
// serviceValidate but not a service ticket
$message = 'Ticket ' . var_export($ticket, true) . ' is not a service ticket.';
$failed = true;
}

if ($failed) {
$finalMessage = 'casserver:validate: ' . $message;
Logger::error($finalMessage);

return new XmlResponse(
(string)$this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message),
Response::HTTP_BAD_REQUEST,
);
}

// Delete the ticket
$this->ticketStore->deleteTicket($ticket);

// Check if the ticket
// - has expired
// - does not pass sanitization
// - forceAutnn criteria are not met
if ($this->ticketFactory->isExpired($serviceTicket)) {
// the ticket has expired
$message = 'Ticket ' . var_export($ticket, true) . ' has expired';
$failed = true;
} elseif ($this->sanitize($serviceTicket['service']) !== $this->sanitize($serviceUrl)) {
// The service url we passed to the query parameters does not match the one in the ticket.
$message = 'Mismatching service parameters: expected ' .
var_export($serviceTicket['service'], true) .
' but was: ' . var_export($serviceUrl, true);
$failed = true;
} elseif ($forceAuthn && !$serviceTicket['forceAuthn']) {
// If `forceAuthn` is required but not set in the ticket
$message = 'Ticket was issued from single sign on session';
$failed = true;
}

if ($failed) {
$finalMessage = 'casserver:validate: ' . $message;
Logger::error($finalMessage);

return new XmlResponse(
(string)$this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message),
Response::HTTP_BAD_REQUEST,
);
}

$attributes = $serviceTicket['attributes'];
$this->cas20Protocol->setAttributes($attributes);

if (isset($pgtUrl)) {
$sessionTicket = $this->ticketStore->getTicket($serviceTicket['sessionId']);
if (
$sessionTicket !== null
&& $this->ticketFactory->isSessionTicket($sessionTicket)
&& !$this->ticketFactory->isExpired($sessionTicket)
) {
$proxyGrantingTicket = $this->ticketFactory->createProxyGrantingTicket(
[
'userName' => $serviceTicket['userName'],
'attributes' => $attributes,
'forceAuthn' => false,
'proxies' => array_merge(
[$serviceUrl],
$serviceTicket['proxies'],
),
'sessionId' => $serviceTicket['sessionId'],
],
);
try {
// Here we assume that the fetch will throw on any error.
// The generation of the proxy-granting-ticket or the corresponding proxy granting ticket IOU may
// fail due to the proxy callback url failing to meet the minimum security requirements such as
// failure to establish trust between peers or unresponsiveness of the endpoint, etc.
// In case of failure, no proxy-granting ticket will be issued and the CAS service response
// as described in Section 2.5.2 MUST NOT contain a <proxyGrantingTicket> block.
// At this point, the issuance of a proxy-granting ticket is halted and service ticket
// validation will fail.
$data = $this->httpUtils->fetch(
$pgtUrl . '?pgtIou=' . $proxyGrantingTicket['iou'] . '&pgtId=' . $proxyGrantingTicket['id'],
);
Logger::debug(__METHOD__ . '::data: ' . var_export($data, true));
$this->cas20Protocol->setProxyGrantingTicketIOU($proxyGrantingTicket['iou']);
$this->ticketStore->addTicket($proxyGrantingTicket);
} catch (\Exception $e) {
return new XmlResponse(
(string)$this->cas20Protocol->getValidateFailureResponse(
C::ERR_INVALID_SERVICE,
'Proxy callback url is failing.',
),
Response::HTTP_BAD_REQUEST,
);
}
}
}

return new XmlResponse(
(string)$this->cas20Protocol->getValidateSuccessResponse($serviceTicket['userName']),
Response::HTTP_OK,
);
}
}
174 changes: 0 additions & 174 deletions src/Controller/Traits/UrlTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@

namespace SimpleSAML\Module\casserver\Controller\Traits;

use SimpleSAML\CAS\Constants as C;
use SimpleSAML\Configuration;
use SimpleSAML\Logger;
use SimpleSAML\Module\casserver\Cas\ServiceValidator;
use SimpleSAML\Module\casserver\Cas\TicketValidator;
use SimpleSAML\Module\casserver\Http\XmlResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

trait UrlTrait
{
Expand Down Expand Up @@ -78,174 +74,4 @@ public function getRequestParam(Request $request, string $paramName): mixed
{
return $request->query->get($paramName) ?? $request->request->get($paramName) ?? null;
}

/**
* @param Request $request
* @param string $method
* @param bool $renew
* @param string|null $target
* @param string|null $ticket
* @param string|null $service
* @param string|null $pgtUrl
*
* @return XmlResponse
*/
public function validate(
Request $request,
string $method,
bool $renew = false,
?string $target = null,
?string $ticket = null,
?string $service = null,
?string $pgtUrl = null,
): XmlResponse {
$forceAuthn = $renew;
$serviceUrl = $service ?? $target ?? null;

// Check if any of the required query parameters are missing
if ($serviceUrl === null || $ticket === null) {
$messagePostfix = $serviceUrl === null ? 'service' : 'ticket';
$message = "casserver: Missing {$messagePostfix} parameter: [{$messagePostfix}]";
Logger::debug($message);

return new XmlResponse(
(string)$this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message),
Response::HTTP_BAD_REQUEST,
);
}

try {
// Get the service ticket
// `getTicket` uses the unserializable method and Objects may throw Throwables in their
// un-serialization handlers.
$serviceTicket = $this->ticketStore->getTicket($ticket);
} catch (\Exception $e) {
$messagePostfix = '';
if (!empty($e->getMessage())) {
$messagePostfix = ': ' . var_export($e->getMessage(), true);
}
$message = 'casserver:serviceValidate: internal server error' . $messagePostfix;
Logger::error($message);

return new XmlResponse(
(string)$this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message),
Response::HTTP_INTERNAL_SERVER_ERROR,
);
}

$failed = false;
$message = '';
// Below, we do not have a ticket or the ticket does not meet the very basic criteria that allow
// any further handling
if (empty($serviceTicket)) {
// No ticket
$message = 'Ticket ' . var_export($ticket, true) . ' not recognized';
$failed = true;
} elseif ($method === 'proxyValidate' && !$this->ticketFactory->isProxyTicket($serviceTicket)) {
// proxyValidate but not a proxy ticket
$message = 'Ticket ' . var_export($ticket, true) . ' is not a proxy ticket.';
$failed = true;
} elseif ($method === 'serviceValidate' && !$this->ticketFactory->isServiceTicket($serviceTicket)) {
// serviceValidate but not a service ticket
$message = 'Ticket ' . var_export($ticket, true) . ' is not a service ticket.';
$failed = true;
}

if ($failed) {
$finalMessage = 'casserver:validate: ' . $message;
Logger::error($finalMessage);

return new XmlResponse(
(string)$this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message),
Response::HTTP_BAD_REQUEST,
);
}

// Delete the ticket
$this->ticketStore->deleteTicket($ticket);

// Check if the ticket
// - has expired
// - does not pass sanitization
// - forceAutnn criteria are not met
if ($this->ticketFactory->isExpired($serviceTicket)) {
// the ticket has expired
$message = 'Ticket ' . var_export($ticket, true) . ' has expired';
$failed = true;
} elseif ($this->sanitize($serviceTicket['service']) !== $this->sanitize($serviceUrl)) {
// The service url we passed to the query parameters does not match the one in the ticket.
$message = 'Mismatching service parameters: expected ' .
var_export($serviceTicket['service'], true) .
' but was: ' . var_export($serviceUrl, true);
$failed = true;
} elseif ($forceAuthn && !$serviceTicket['forceAuthn']) {
// If `forceAuthn` is required but not set in the ticket
$message = 'Ticket was issued from single sign on session';
$failed = true;
}

if ($failed) {
$finalMessage = 'casserver:validate: ' . $message;
Logger::error($finalMessage);

return new XmlResponse(
(string)$this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message),
Response::HTTP_BAD_REQUEST,
);
}

$attributes = $serviceTicket['attributes'];
$this->cas20Protocol->setAttributes($attributes);

if (isset($pgtUrl)) {
$sessionTicket = $this->ticketStore->getTicket($serviceTicket['sessionId']);
if (
$sessionTicket !== null
&& $this->ticketFactory->isSessionTicket($sessionTicket)
&& !$this->ticketFactory->isExpired($sessionTicket)
) {
$proxyGrantingTicket = $this->ticketFactory->createProxyGrantingTicket(
[
'userName' => $serviceTicket['userName'],
'attributes' => $attributes,
'forceAuthn' => false,
'proxies' => array_merge(
[$serviceUrl],
$serviceTicket['proxies'],
),
'sessionId' => $serviceTicket['sessionId'],
],
);
try {
// Here we assume that the fetch will throw on any error.
// The generation of the proxy-granting-ticket or the corresponding proxy granting ticket IOU may
// fail due to the proxy callback url failing to meet the minimum security requirements such as
// failure to establish trust between peers or unresponsiveness of the endpoint, etc.
// In case of failure, no proxy-granting ticket will be issued and the CAS service response
// as described in Section 2.5.2 MUST NOT contain a <proxyGrantingTicket> block.
// At this point, the issuance of a proxy-granting ticket is halted and service ticket
// validation will fail.
$data = $this->httpUtils->fetch(
$pgtUrl . '?pgtIou=' . $proxyGrantingTicket['iou'] . '&pgtId=' . $proxyGrantingTicket['id'],
);
Logger::debug(__METHOD__ . '::data: ' . var_export($data, true));
$this->cas20Protocol->setProxyGrantingTicketIOU($proxyGrantingTicket['iou']);
$this->ticketStore->addTicket($proxyGrantingTicket);
} catch (\Exception $e) {
return new XmlResponse(
(string)$this->cas20Protocol->getValidateFailureResponse(
C::ERR_INVALID_SERVICE,
'Proxy callback url is failing.',
),
Response::HTTP_BAD_REQUEST,
);
}
}
}

return new XmlResponse(
(string)$this->cas20Protocol->getValidateSuccessResponse($serviceTicket['userName']),
Response::HTTP_OK,
);
}
}
Loading

0 comments on commit b659110

Please sign in to comment.