You are here

public function CasProxyHelper::proxyAuthenticate in CAS 8

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

Proxy authenticates to a target service.

Returns cookies from the proxied service in a CookieJar object for use when later accessing resources.

Parameters

string $target_service: The service to be proxied.

Return value

\GuzzleHttp\Cookie\CookieJar A CookieJar object (array storage) containing cookies from the proxied service.

Throws

\Drupal\cas\Exception\CasProxyException Thrown if there was a problem communicating with the CAS server or if there was is invalid use rsession data.

File

src/Service/CasProxyHelper.php, line 150

Class

CasProxyHelper
Class CasProxyHelper.

Namespace

Drupal\cas\Service

Code

public function proxyAuthenticate($target_service) {
  $cas_proxy_helper = $this->session
    ->get('cas_proxy_helper');

  // Check to see if we have proxied this application already.
  if (isset($cas_proxy_helper[$target_service])) {
    $cookies = [];
    foreach ($cas_proxy_helper[$target_service] as $cookie) {
      $cookies[$cookie['Name']] = $cookie['Value'];
    }
    $domain = $cookie['Domain'];
    $jar = CookieJar::fromArray($cookies, $domain);
    $this->casHelper
      ->log(LogLevel::DEBUG, "%target_service already proxied. Returning information from session.", [
      '%target_service' => $target_service,
    ]);
    return $jar;
  }

  // Get proxy ticket and use it to initialize params.
  $params = [
    'ticket' => $this
      ->getProxyTicket($target_service),
  ];

  // Make request to target service with our new proxy ticket.
  // The target service will validate this ticket against the CAS server
  // and set a cookie that grants authentication for further resource calls.
  $service_url = $target_service . "?" . UrlHelper::buildQuery($params);
  $cookie_jar = new CookieJar();
  try {
    $this->casHelper
      ->log(LogLevel::DEBUG, "Contacting service: %service", [
      '%service' => $service_url,
    ]);
    $this->httpClient
      ->get($service_url, [
      'cookies' => $cookie_jar,
      'timeout' => $this->settings
        ->get('advanced.connection_timeout'),
    ]);
  } catch (ClientException $e) {
    throw new CasProxyException($e
      ->getMessage());
  }

  // Store in session storage for later reuse.
  $cas_proxy_helper[$target_service] = $cookie_jar
    ->toArray();
  $this->session
    ->set('cas_proxy_helper', $cas_proxy_helper);
  $this->casHelper
    ->log(LogLevel::DEBUG, "Stored cookies from %service in session.", [
    '%service' => $target_service,
  ]);
  return $cookie_jar;
}