src/Controller/Gate3Controller.php line 17

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Cookie;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\Uid\Uuid;
  11. class Gate3Controller extends AbstractController
  12. {
  13.     #[Route('/gate3'name'app_gate3'methods: ['GET'])]
  14.     public function index(Request $request): Response
  15.     {
  16.         $firstIdCookie $request->cookies->get('firstid');
  17.         return $this->render('gate/gate3.html.twig', [
  18.             'hasFirstId' => !empty($firstIdCookie),
  19.             'site_name' => 'First ID Demo'
  20.         ]);
  21.     }
  22.     #[Route('/gate3/redirect'name'app_gate3_redirect'methods: ['GET'])]
  23.     public function initiateFirstIdRedirect(Request $request): RedirectResponse
  24.     {
  25.         $host $request->getSchemeAndHttpHost();
  26.         $randomUuid Uuid::v4()->toRfc4122();
  27.         $redirectHost urlencode($host '/random-redirect/' $randomUuid);
  28.         $redirectUri urlencode($host '/gate3');
  29.         return new RedirectResponse(
  30.             "https://gate.preprod.first-id.fr?redirectHost={$redirectHost}&redirectUri={$redirectUri}",
  31.             Response::HTTP_FOUND
  32.         );
  33.     }
  34.     #[Route('/random-redirect/{uuid}'name'app_random_redirect'methods: ['GET'])]
  35.     public function handleRandomRedirect(Request $requeststring $uuid): RedirectResponse
  36.     {
  37.         if ($request->query->has('firstId')) {
  38.             $firstId = (string) $request->query->get('firstId');
  39.             $redirectUri $request->query->get('redirectUri');
  40.             $redirectHost $request->query->get('redirectHost');
  41.             $targetUrl $this->generateUrl('app_gate3');
  42.             if (is_string($redirectUri) && $redirectUri !== '') {
  43.                 $targetUrl urldecode($redirectUri);
  44.             } elseif (is_string($redirectHost) && $redirectHost !== '') {
  45.                 $targetUrl urldecode($redirectHost);
  46.             }
  47.             $response $this->redirect($targetUrl);
  48.             $response->headers->setCookie(
  49.                 new Cookie(
  50.                     'firstid',
  51.                     $firstId,
  52.                     time() + (60 60 24 180),
  53.                     '/',
  54.                     null,
  55.                     true,
  56.                     false,
  57.                     false,
  58.                     Cookie::SAMESITE_NONE
  59.                 )
  60.             );
  61.             return $response;
  62.         }
  63.         return $this->redirectToRoute('app_gate3');
  64.     }
  65. }