src/Controller/KeycloackController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\UserToken;
  4. use App\Repository\UserTokenRepository;
  5. use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
  6. use Stevenmaguire\OAuth2\Client\Provider\Keycloak;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Contracts\HttpClient\HttpClientInterface;
  12. class KeycloackController extends AbstractController
  13. {
  14.     /**
  15.      * @Route("/connect", name="connect_start")
  16.      */
  17.     public function connectAction(ClientRegistry $clientRegistry)
  18.     {
  19.         return $clientRegistry
  20.             ->getClient('keycloak')
  21.             ->redirect();
  22.     }
  23.     /**
  24.      * @param ClientRegistry $clientRegistry
  25.      * @param HttpClientInterface $httpClient
  26.      * @param USerTokenRepository $repository
  27.      * @return RedirectResponse|void
  28.      * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  29.      * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  30.      * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  31.      * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  32.      *
  33.      * @Route("/connect/check", name="connect_check")
  34.      */
  35.     public function connectCheckAction(ClientRegistry $clientRegistryHttpClientInterface $httpClientUserTokenRepository $repositorySessionInterface $session)
  36.     {
  37.         try {
  38.             $client $clientRegistry->getClient('keycloak');
  39.             $accessToken $client->getAccessToken();
  40.             if ($accessToken->hasExpired()) {
  41.                 $accessToken $client->refreshAccessToken($accessToken->getRefreshToken());
  42.             }
  43.             $session->set('access_token'$accessToken);
  44.             return $this->redirectToRoute('user_information');
  45.         } catch (\Exception $exception) {
  46.             dd($exception->getMessage());
  47.             return $this->redirectToRoute('user_error');
  48.         }
  49.         // todo add error log,
  50. //       return $this->redirectToRoute('home');
  51.         return $this->redirectToRoute('user_error');
  52.     }
  53.     /**
  54.      * @Route("/logout", name="logout")
  55.      */
  56.     public function logoutAction()
  57.     {
  58.         $provider = new Keycloak([
  59.             'authServerUrl' => 'https://authentication.qotico-telecom.fr/auth',
  60.             'realm' => 'qotico-gp-sandbox',
  61.             'clientId' => 'sicom-gp-front',
  62.             'clientSecret' => '4PEhz0D4NV08PBmUGu34ZKInFkGdgq4f',
  63.             'redirectUri' => '/contact',
  64.         ]);
  65.         $url $provider->getLogoutUrl();
  66.         dump($url);
  67.         exit();
  68.     }
  69. }