You are here

public function CasValidator::validateTicket in CAS 8

Same name and namespace in other branches
  1. 2.x src/Service/CasValidator.php \Drupal\cas\Service\CasValidator::validateTicket()

Validate the service ticket parameter present in the request.

This method will return the username of the user if valid, and raise an exception if the ticket is not found or not valid.

Parameters

string $ticket: The CAS authentication ticket to validate.

array $service_params: An array of query string parameters to add to the service URL.

Return value

\Drupal\cas\CasPropertyBag Contains user info from the CAS server.

Throws

\Drupal\cas\Exception\CasValidateException Thrown if there was a problem making the validation request or if there was a local configuration issue.

File

src/Service/CasValidator.php, line 99

Class

CasValidator
Class CasValidator.

Namespace

Drupal\cas\Service

Code

public function validateTicket($ticket, array $service_params = []) {
  $casServerConfig = CasServerConfig::createFromModuleConfig($this->settings);

  // Allow modules to modify the server config before it's used to validate
  // the login ticket.
  $event = new CasPreValidateServerConfigEvent($casServerConfig);
  $this->eventDispatcher
    ->dispatch(CasHelper::EVENT_PRE_VALIDATE_SERVER_CONFIG, $event);

  // Determine the path to send the validation request to on the CAS server.
  $path = '';
  switch ($casServerConfig
    ->getProtocolVerison()) {
    case "1.0":
      $path = 'validate';
      break;
    case "2.0":
      if ($this->settings
        ->get('proxy.can_be_proxied')) {
        $path = 'proxyValidate';
      }
      else {
        $path = 'serviceValidate';
      }
      break;
    case "3.0":
      if ($this->settings
        ->get('proxy.can_be_proxied')) {
        $path = 'p3/proxyValidate';
      }
      else {
        $path = 'p3/serviceValidate';
      }
      break;
  }
  $params = [];
  $params['service'] = $this->urlGenerator
    ->generate('cas.service', $service_params, UrlGeneratorInterface::ABSOLUTE_URL);
  $params['ticket'] = $ticket;
  if ($this->settings
    ->get('proxy.initialize')) {
    $params['pgtUrl'] = $this
      ->formatProxyCallbackUrl();
  }

  // Dispatch an event that allows modules to alter the validation path or
  // URL parameters.
  $pre_validate_event = new CasPreValidateEvent($path, $params);
  $this->eventDispatcher
    ->dispatch(CasHelper::EVENT_PRE_VALIDATE, $pre_validate_event);
  $validate_url = $casServerConfig
    ->getServerBaseUrl() . $pre_validate_event
    ->getValidationPath();
  if (!empty($pre_validate_event
    ->getParameters())) {
    $validate_url .= '?' . UrlHelper::buildQuery($pre_validate_event
      ->getParameters());
  }
  $this->casHelper
    ->log(LogLevel::DEBUG, 'Attempting to validate service ticket %ticket by making request to URL %url', [
    '%ticket' => $ticket,
    '%url' => $validate_url,
  ]);
  try {
    $response = $this->httpClient
      ->get($validate_url, $casServerConfig
      ->getCasServerGuzzleConnectionOptions());
    $response_data = $response
      ->getBody()
      ->__toString();
    $this->casHelper
      ->log(LogLevel::DEBUG, "Validation response received from CAS server: %data", [
      '%data' => $response_data,
    ]);
  } catch (RequestException $e) {
    throw new CasValidateException("Error with request to validate ticket: " . $e
      ->getMessage());
  }
  $protocol_version = $casServerConfig
    ->getProtocolVerison();
  switch ($protocol_version) {
    case "1.0":
      $cas_property_bag = $this
        ->validateVersion1($response_data);
      break;
    case "2.0":
    case "3.0":
      $cas_property_bag = $this
        ->validateVersion2($response_data);
      break;
  }
  if (empty($cas_property_bag)) {
    throw new CasValidateException('Unknown CAS protocol version specified: ' . $protocol_version);
  }

  // Dispatch an event that allows modules to alter the CAS property bag.
  $event = new CasPostValidateEvent($response_data, $cas_property_bag);
  $this->eventDispatcher
    ->dispatch(CasHelper::EVENT_POST_VALIDATE, $event);
  return $event
    ->getCasPropertyBag();
}