You are here

class OpenIDConnectSession in OpenID Connect / OAuth client 2.x

Same name and namespace in other branches
  1. 8 src/OpenIDConnectSession.php \Drupal\openid_connect\OpenIDConnectSession

Session service of the OpenID Connect module.

Hierarchy

Expanded class hierarchy of OpenIDConnectSession

1 file declares its use of OpenIDConnectSession
OpenIdConnectSessionTest.php in tests/src/Unit/OpenIdConnectSessionTest.php
1 string reference to 'OpenIDConnectSession'
openid_connect.services.yml in ./openid_connect.services.yml
openid_connect.services.yml
1 service uses OpenIDConnectSession
openid_connect.session in ./openid_connect.services.yml
Drupal\openid_connect\OpenIDConnectSession

File

src/OpenIDConnectSession.php, line 15

Namespace

Drupal\openid_connect
View source
class OpenIDConnectSession implements OpenIDConnectSessionInterface {

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The redirect destination service.
   *
   * @var \Drupal\Core\Routing\RedirectDestinationInterface
   */
  protected $redirectDestination;

  /**
   * The session object.
   *
   * @var \Symfony\Component\HttpFoundation\Session\SessionInterface
   */
  protected $session;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * Construct an instance of the OpenID Connect session service.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
   *   The redirect destination service.
   * @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
   *   The session object.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, RedirectDestinationInterface $redirect_destination, SessionInterface $session, LanguageManagerInterface $language_manager) {
    $this->configFactory = $config_factory;
    $this->redirectDestination = $redirect_destination;
    $this->session = $session;
    $this->languageManager = $language_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) : OpenIDConnectSession {
    return new static($container
      ->get('config.factory'), $container
      ->get('redirect.destination'), $container
      ->get('session'), $container
      ->get('language_manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function retrieveDestination(bool $clear = TRUE) : array {
    $ret = [
      'destination' => $this->session
        ->get('openid_connect_destination'),
      'langcode' => $this->session
        ->get('openid_connect_langcode'),
    ];
    if ($clear) {
      $this->session
        ->remove('openid_connect_destination');
      $this->session
        ->remove('openid_connect_langcode');
    }
    return $ret;
  }

  /**
   * {@inheritdoc}
   */
  public function saveDestination() {

    // If the current request includes a 'destination' query parameter we'll use
    // that in the redirection. Otherwise use the current request path and
    // query.
    $destination = ltrim($this->redirectDestination
      ->get(), '/');
    $langcode = $this->languageManager
      ->getCurrentLanguage()
      ->getId();

    // Don't redirect to user/login. In this case redirect to the user profile.
    if (strpos($destination, ltrim(Url::fromRoute('user.login')
      ->toString(), '/')) === 0) {
      $redirect_login = $this->configFactory
        ->get('openid_connect.settings')
        ->get('redirect_login');
      $destination = $redirect_login ?: 'user';
    }
    $this->session
      ->set('openid_connect_destination', $destination);
    $this->session
      ->set('openid_connect_langcode', $langcode);
  }

  /**
   * {@inheritdoc}
   */
  public function retrieveOp(bool $clear = TRUE) : array {
    $ret = [
      'op' => $this->session
        ->get('openid_connect_op'),
      'uid' => $this->session
        ->get('openid_connect_uid'),
    ];
    if ($clear) {
      $this->session
        ->remove('openid_connect_op');
      $this->session
        ->remove('openid_connect_uid');
    }
    return $ret;
  }

  /**
   * {@inheritdoc}
   */
  public function saveOp(string $op, int $uid = NULL) {
    $this->session
      ->set('openid_connect_op', $op);
    if (isset($uid)) {
      $this->session
        ->set('openid_connect_uid', $uid);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function retrieveIdToken(bool $clear = FALSE) : ?string {
    $ret = $this->session
      ->get('openid_connect_id');
    if ($clear) {
      $this->session
        ->remove('openid_connect_id');
    }
    return $ret;
  }

  /**
   * {@inheritdoc}
   */
  public function saveIdToken(string $token) {
    $this->session
      ->set('openid_connect_id', $token);
  }

  /**
   * {@inheritdoc}
   */
  public function retrieveAccessToken(bool $clear = FALSE) : ?string {
    $ret = $this->session
      ->get('openid_connect_access');
    if ($clear) {
      $this->session
        ->remove('openid_connect_access');
    }
    return $ret;
  }

  /**
   * {@inheritdoc}
   */
  public function saveAccessToken(string $token) {
    $this->session
      ->set('openid_connect_access', $token);
  }

  /**
   * {@inheritdoc}
   */
  public function retrieveStateToken(bool $clear = TRUE) : ?string {
    $ret = $this->session
      ->get('openid_connect_state');
    if ($clear) {
      $this->session
        ->remove('openid_connect_state');
    }
    return $ret;
  }

  /**
   * {@inheritdoc}
   */
  public function saveStateToken(string $token) {
    $this->session
      ->set('openid_connect_state', $token);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OpenIDConnectSession::$configFactory protected property The config factory.
OpenIDConnectSession::$languageManager protected property The language manager.
OpenIDConnectSession::$redirectDestination protected property The redirect destination service.
OpenIDConnectSession::$session protected property The session object.
OpenIDConnectSession::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
OpenIDConnectSession::retrieveAccessToken public function Get the access token from the session. Overrides OpenIDConnectSessionInterface::retrieveAccessToken
OpenIDConnectSession::retrieveDestination public function Get the destination redirect path and langcode from the session. Overrides OpenIDConnectSessionInterface::retrieveDestination
OpenIDConnectSession::retrieveIdToken public function Get the id token from the session. Overrides OpenIDConnectSessionInterface::retrieveIdToken
OpenIDConnectSession::retrieveOp public function Get the operation details from the session. Overrides OpenIDConnectSessionInterface::retrieveOp
OpenIDConnectSession::retrieveStateToken public function Get the state token from the session. Overrides OpenIDConnectSessionInterface::retrieveStateToken
OpenIDConnectSession::saveAccessToken public function Save the access token in the session. Overrides OpenIDConnectSessionInterface::saveAccessToken
OpenIDConnectSession::saveDestination public function Save the current path and langcode, for redirecting after authorization. Overrides OpenIDConnectSessionInterface::saveDestination
OpenIDConnectSession::saveIdToken public function Save the id token in the session. Overrides OpenIDConnectSessionInterface::saveIdToken
OpenIDConnectSession::saveOp public function Save the operation details in the session. Overrides OpenIDConnectSessionInterface::saveOp
OpenIDConnectSession::saveStateToken public function Save the state token in the session. Overrides OpenIDConnectSessionInterface::saveStateToken
OpenIDConnectSession::__construct public function Construct an instance of the OpenID Connect session service.