You are here

class ProxyCallbackController in CAS 8

Same name and namespace in other branches
  1. 2.x src/Controller/ProxyCallbackController.php \Drupal\cas\Controller\ProxyCallbackController

Class ProxyCallbackController.

Hierarchy

Expanded class hierarchy of ProxyCallbackController

File

src/Controller/ProxyCallbackController.php, line 16

Namespace

Drupal\cas\Controller
View source
class ProxyCallbackController implements ContainerInjectionInterface {

  /**
   * Used when inserting the CAS PGT into the database.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * Used to get params from the current request object.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * Used for logging.
   *
   * @var \Drupal\cas\Service\CasHelper
   */
  protected $casHelper;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Database\Connection $database_connection
   *   The database service.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The Symfony request stack.
   * @param \Drupal\cas\Service\CasHelper $cas_helper
   *   The CasHelper.
   */
  public function __construct(Connection $database_connection, RequestStack $request_stack, CasHelper $cas_helper) {
    $this->connection = $database_connection;
    $this->requestStack = $request_stack;
    $this->casHelper = $cas_helper;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('database'), $container
      ->get('request_stack'), $container
      ->get('cas.helper'));
  }

  /**
   * Route callback for the ProxyGrantingTicket information.
   *
   * This function stores the incoming PGTIOU and pgtId parameters so that
   * the incoming response from the CAS Server can be looked up.
   */
  public function callback() {
    $this->casHelper
      ->log(LogLevel::DEBUG, 'Proxy callback processing started.');

    // @TODO: Check that request is coming from configured CAS server to avoid
    // filling up the table with bogus pgt values.
    $request = $this->requestStack
      ->getCurrentRequest();

    // Check for both a pgtIou and pgtId parameter. If either is not present,
    // inform CAS Server of an error.
    if (!($request->query
      ->get('pgtId') && $request->query
      ->get('pgtIou'))) {
      $this->casHelper
        ->log(LogLevel::ERROR, "Either pgtId or pgtIou parameters are missing from the request.");
      return Response::create('Missing necessary parameters', 400);
    }
    else {

      // Store the pgtIou and pgtId in the database for later use.
      $pgt_id = $request->query
        ->get('pgtId');
      $pgt_iou = $request->query
        ->get('pgtIou');
      $this
        ->storePgtMapping($pgt_iou, $pgt_id);
      $this->casHelper
        ->log(LogLevel::DEBUG, "Storing pgtId %pgt_id with pgtIou %pgt_iou", [
        '%pgt_id' => $pgt_id,
        '%pgt_iou' => $pgt_iou,
      ]);

      // PGT stored properly, tell CAS Server to proceed.
      return Response::create('OK', 200);
    }
  }

  /**
   * Store the pgtIou to pgtId mapping in the database.
   *
   * @param string $pgt_iou
   *   The pgtIou from CAS Server.
   * @param string $pgt_id
   *   The pgtId from the CAS server.
   *
   * @codeCoverageIgnore
   */
  protected function storePgtMapping($pgt_iou, $pgt_id) {
    $this->connection
      ->insert('cas_pgt_storage')
      ->fields([
      'pgt_iou',
      'pgt',
      'timestamp',
    ], [
      $pgt_iou,
      $pgt_id,
      time(),
    ])
      ->execute();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ProxyCallbackController::$casHelper protected property Used for logging.
ProxyCallbackController::$connection protected property Used when inserting the CAS PGT into the database.
ProxyCallbackController::$requestStack protected property Used to get params from the current request object.
ProxyCallbackController::callback public function Route callback for the ProxyGrantingTicket information.
ProxyCallbackController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
ProxyCallbackController::storePgtMapping protected function Store the pgtIou to pgtId mapping in the database.
ProxyCallbackController::__construct public function Constructor.