src/Controller/KeycloackController.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
  4. use Stevenmaguire\OAuth2\Client\Provider\Keycloak;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. class KeycloackController extends AbstractController
  10. {
  11.     /**
  12.      * @Route("/connect", name="connect_start")
  13.      */
  14.     public function connectAction(ClientRegistry $clientRegistry)
  15.     {
  16.         return $clientRegistry
  17.             ->getClient('keycloak')
  18.             ->redirect();
  19.     }
  20.     /**
  21.      * @Route("/connect/check", name="connect_check")
  22.      */
  23.     public function connectCheckAction(Request $requestSessionInterface $session)
  24.     {
  25.         $authServerUrl $this->getParameter('AUTH_SERVER_URL');
  26.         $realm $this->getParameter('REALM');
  27.         $clientId $this->getParameter('CLIENT_ID');
  28.         $clientSecret $this->getParameter('CLIENT_SECRET');
  29.         $redirectUri 'connect_check';
  30.         $provider = new Keycloak([
  31.             'authServerUrl' => $authServerUrl,
  32.             'realm' => $realm,
  33.             'clientId' => $clientId,
  34.             'clientSecret' => $clientSecret,
  35.             'redirectUri' => $redirectUri
  36.         ]);
  37.         /*try {
  38.             $token = $provider->getAccessToken('authorization_code', [
  39.                 'code' => $_GET['code']
  40.             ]);
  41.             dd($token);
  42.         } catch (\Exception $e) {
  43.             exit('Failed to get access token: ' . $e->getMessage());
  44.         }*/
  45.     // Try to get an access token (using the authorization coe grant)
  46.     session_start();
  47.         if (!isset($_GET['code'])) {
  48.     // If we don't have an authorization code then get one
  49.     $authUrl $provider->getAuthorizationUrl();
  50.     $_SESSION['oauth2state'] = $provider->getState();
  51.     header('Location: '.$authUrl);
  52.     exit;
  53. // Check given state against previously stored one to mitigate CSRF attack
  54. } elseif (empty($_GET['state']) || ($_SESSION['oauth2state'] && ($_GET['state'] !== $_SESSION['oauth2state']))) {
  55.     unset($_SESSION['oauth2state']);
  56.     exit('Invalid state, make sure HTTP sessions are enabled.');
  57. } else {
  58.     // Try to get an access token (using the authorization coe grant)
  59.     try {
  60.         $token $provider->getAccessToken('authorization_code', [
  61.             'code' => $_GET['code']
  62.         ]);
  63.     } catch (Exception $e) {
  64.         exit('Failed to get access token: '.$e->getMessage());
  65.     }
  66.     // Optional: Now you have a token you can look up a users profile data
  67.     try {
  68.         // We got an access token, let's now get the user's details
  69.         $user $provider->getResourceOwner($token);
  70.         // Use these details to create a new profile
  71.         printf('Hello %s!'$user->getName());
  72.     } catch (Exception $e) {
  73.         exit('Failed to get resource owner: '.$e->getMessage());
  74.     }
  75.     // Use this to interact with an API on the users behalf
  76.     echo $token->getToken();
  77. }
  78.     }
  79.     /**
  80.      * @Route("/logout", name="logout")
  81.      */
  82.     public function logoutAction()
  83.     {
  84.         $provider = new Keycloak([
  85.             'authServerUrl' => 'https://authentication.qotico-telecom.fr/auth',
  86.             'realm' => 'qotico-gp-sandbox',
  87.             'clientId' => 'sicom-gp-front',
  88.             'clientSecret' => '4PEhz0D4NV08PBmUGu34ZKInFkGdgq4f',
  89.             'redirectUri' => 'connect_check',
  90.         ]);
  91.        $url $provider->getLogoutUrl();
  92.        dump($url);
  93.        exit();
  94.     }
  95. }