KeycloakController.php in Keycloak OpenID Connect 8
File
src/Controller/KeycloakController.php
View source
<?php
namespace Drupal\keycloak\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\keycloak\Service\KeycloakServiceInterface;
use Drupal\openid_connect\OpenIDConnectClaims;
use Drupal\openid_connect\Plugin\OpenIDConnectClientManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class KeycloakController extends ControllerBase {
protected $keycloak;
protected $pluginManager;
protected $claims;
protected $requestStack;
protected $currentUser;
public function __construct(KeycloakServiceInterface $keycloak, AccountProxyInterface $current_user, OpenIDConnectClientManager $plugin_manager, OpenIDConnectClaims $claims, RequestStack $request_stack) {
$this->keycloak = $keycloak;
$this->currentUser = $current_user;
$this->pluginManager = $plugin_manager;
$this->claims = $claims;
$this->requestStack = $request_stack;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('keycloak.keycloak'), $container
->get('current_user'), $container
->get('plugin.manager.openid_connect_client.processor'), $container
->get('openid_connect.claims'), $container
->get('request_stack'));
}
public function login() {
openid_connect_save_destination();
$client_name = 'keycloak';
$configuration = $this
->config('openid_connect.settings.keycloak')
->get('settings');
$client = $this->pluginManager
->createInstance($client_name, $configuration);
$scopes = $this->claims
->getScopes();
$_SESSION['openid_connect_op'] = 'login';
$response = $client
->authorize($scopes);
return $response;
}
public function logout() {
$rp_signout = NULL;
if (!$this->requestStack
->getCurrentRequest()->query
->get('op_initiated') && $this->keycloak
->isEnabled() && $this->keycloak
->isKeycloakUser() && $this->keycloak
->isKeycloakSignOutEnabled()) {
$rp_signout = $this->keycloak
->getSessionInfo([
KeycloakServiceInterface::KEYCLOAK_SESSION_ID_TOKEN,
]);
}
if ($this->currentUser
->isAuthenticated()) {
user_logout();
}
if (!empty($rp_signout[KeycloakServiceInterface::KEYCLOAK_SESSION_ID_TOKEN])) {
$response = $this->keycloak
->getKeycloakSignoutResponse($rp_signout);
return $response;
}
return $this
->redirect('<front>');
}
}