OpenIDConnectSession.php in OpenID Connect / OAuth client 2.x
File
src/OpenIDConnectSession.php
View source
<?php
namespace Drupal\openid_connect;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class OpenIDConnectSession implements OpenIDConnectSessionInterface {
protected $configFactory;
protected $redirectDestination;
protected $session;
protected $languageManager;
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;
}
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'));
}
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;
}
public function saveDestination() {
$destination = ltrim($this->redirectDestination
->get(), '/');
$langcode = $this->languageManager
->getCurrentLanguage()
->getId();
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);
}
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;
}
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);
}
}
public function retrieveIdToken(bool $clear = FALSE) : ?string {
$ret = $this->session
->get('openid_connect_id');
if ($clear) {
$this->session
->remove('openid_connect_id');
}
return $ret;
}
public function saveIdToken(string $token) {
$this->session
->set('openid_connect_id', $token);
}
public function retrieveAccessToken(bool $clear = FALSE) : ?string {
$ret = $this->session
->get('openid_connect_access');
if ($clear) {
$this->session
->remove('openid_connect_access');
}
return $ret;
}
public function saveAccessToken(string $token) {
$this->session
->set('openid_connect_access', $token);
}
public function retrieveStateToken(bool $clear = TRUE) : ?string {
$ret = $this->session
->get('openid_connect_state');
if ($clear) {
$this->session
->remove('openid_connect_state');
}
return $ret;
}
public function saveStateToken(string $token) {
$this->session
->set('openid_connect_state', $token);
}
}