<?php
namespace App\Controller;
use Gesdinet\JWTRefreshTokenBundle\Entity\RefreshToken;
use Hslavich\OneloginSamlBundle\Security\Authentication\Token\SamlToken;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Events;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
class IndexController extends AbstractController
{
/**
* @var EventDispatcherInterface
*/
private $dispatcher;
/**
* IndexController constructor.
* @param EventDispatcherInterface $dispatcher
*/
public function __construct(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
}
/**
* @Route("/check-auth", name="check_auth")
* @param JWTTokenManagerInterface $JWTManager
* @return JsonResponse
*/
public function checkAuth(JWTTokenManagerInterface $JWTManager): JsonResponse
{
// get logged in user from token storage
$tokenStorage = $this->get('security.token_storage')->getToken();
if(!$tokenStorage instanceof SamlToken) return new JsonResponse(['status' => false]);
$user = $tokenStorage->getUser();
if ($user) {
$jwt = $JWTManager->create($user);
$oldRefreshToken = $this->getDoctrine()->getRepository(RefreshToken::class)->findOneBy(['username' => $user->getUsername()]);
if ( $oldRefreshToken instanceof RefreshToken) {
$payload = [
'token' => $jwt,
'refresh_token' => $oldRefreshToken->getRefreshToken()
];
} else {
$response = new JsonResponse();
$event = new AuthenticationSuccessEvent(['token' => $jwt], $user, $response);
$this->dispatcher->dispatch($event, Events::AUTHENTICATION_SUCCESS);
$payload = $event->getData();
}
return new JsonResponse($payload);
}
return new JsonResponse(['status' => false]);
}
/**
* @Route("/{reactRouting}", name="home", defaults={"reactRouting": null}, methods={"GET"})
* @Route("/{path}", name="all_router", requirements={"path" = ".+"}, priority=-1, methods={"GET"})
*/
public function index()
{
return $this->render('index/index.html.twig', []);
}
/**
* @Route("/admin", name="app_dashboard")
*/
public function admin(): Response
{
return $this->render('index/dashboard.html.twig', []);
}
}