<?php
namespace App\Controller;
use App\Entity\UserToken;
use App\Repository\UserTokenRepository;
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use Stevenmaguire\OAuth2\Client\Provider\Keycloak;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class KeycloackController extends AbstractController
{
/**
* @Route("/connect", name="connect_start")
*/
public function connectAction(ClientRegistry $clientRegistry)
{
return $clientRegistry
->getClient('keycloak')
->redirect();
}
/**
* @param ClientRegistry $clientRegistry
* @param HttpClientInterface $httpClient
* @param USerTokenRepository $repository
* @return RedirectResponse|void
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
*
* @Route("/connect/check", name="connect_check")
*/
public function connectCheckAction(ClientRegistry $clientRegistry, HttpClientInterface $httpClient, UserTokenRepository $repository, SessionInterface $session)
{
try {
$client = $clientRegistry->getClient('keycloak');
$accessToken = $client->getAccessToken();
if ($accessToken->hasExpired()) {
$accessToken = $client->refreshAccessToken($accessToken->getRefreshToken());
}
$session->set('access_token', $accessToken);
return $this->redirectToRoute('user_information');
} catch (\Exception $exception) {
dd($exception->getMessage());
return $this->redirectToRoute('user_error');
}
// todo add error log,
// return $this->redirectToRoute('home');
return $this->redirectToRoute('user_error');
}
/**
* @Route("/logout", name="logout")
*/
public function logoutAction()
{
$provider = new Keycloak([
'authServerUrl' => 'https://authentication.qotico-telecom.fr/auth',
'realm' => 'qotico-gp-sandbox',
'clientId' => 'sicom-gp-front',
'clientSecret' => '4PEhz0D4NV08PBmUGu34ZKInFkGdgq4f',
'redirectUri' => '/contact',
]);
$url = $provider->getLogoutUrl();
dump($url);
exit();
}
}