src/Controller/SecurityController.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Role;
  4. use App\Entity\User;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. class SecurityController extends AbstractController
  14. {
  15.     private const DISCORD_ENDPOINT 'https://discord.com/api/oauth2/authorize';
  16.     private const STEAM_ENDPOINT 'https://steamcommunity.com/openid/login';
  17.     /**
  18.      * @Route("/login/failed", name="failed")
  19.      * @param Request $request
  20.      * @return Response
  21.      */
  22.     public function failed(Request $request): Response
  23.     {
  24.         return $this->render('security/failed.html.twig', ['countryGroup' => $request->getLocale()]);
  25.     }
  26.     /**
  27.      * @Route("/login/discord", name="login_discord", methods={"GET"})
  28.      * @param SessionInterface $session
  29.      * @param UrlGeneratorInterface $urlGenerator
  30.      * @return Response
  31.      */
  32.     public function loginWithDiscord(SessionInterface $sessionUrlGeneratorInterface $urlGenerator): Response
  33.     {
  34.         $redirectURL $urlGenerator->generate('home', [
  35.             'discord-oauth-provider' => true
  36.         ], UrlGeneratorInterface::ABSOLUTE_URL);
  37.         $queryParams http_build_query([
  38.             'client_id'     => getenv("DISCORD_ID"),
  39.             'prompt'        => 'consent',
  40.             'redirect_url'  => $redirectURL,
  41.             'response_type' => 'code',
  42.             'scope'         => 'guilds.join identify email'
  43.         ]);
  44.         return new RedirectResponse(self::DISCORD_ENDPOINT '?' $queryParams);
  45.     }
  46.     /**
  47.      * @Route("/login/steam", name="login_steam", methods={"GET"})
  48.      * @param SessionInterface $session
  49.      * @param UrlGeneratorInterface $urlGenerator
  50.      * @return Response
  51.      */
  52.     public function loginWithSteam(SessionInterface $sessionUrlGeneratorInterface $urlGenerator): Response
  53.     {
  54.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  55.         if (!$this->getUser()) {
  56.             return $this->redirectToRoute('home');
  57.         }
  58.         $redirectURL $urlGenerator->generate('login_steam_response', [], UrlGeneratorInterface::ABSOLUTE_URL);
  59.         $queryParams http_build_query([
  60.             'openid.return_to' => $redirectURL,
  61.             'openid.mode' => 'checkid_setup',
  62.             'openid.ns' => 'http://specs.openid.net/auth/2.0',
  63.             'openid.realm' => $redirectURL,
  64.             'openid.ns.sreg' => 'http://openid.net/extensions/sreg/1.1',
  65.             'openid.claimed_id' => 'http://specs.openid.net/auth/2.0/identifier_select',
  66.             'openid.identity' => 'http://specs.openid.net/auth/2.0/identifier_select',
  67.         ]);
  68.         return new RedirectResponse(self::STEAM_ENDPOINT '?' $queryParams);
  69.     }
  70.     /**
  71.      * @Route("/login/steam/response", name="login_steam_response", methods={"GET"})
  72.      * @param SessionInterface $session
  73.      * @param EntityManagerInterface $entityManager
  74.      * @param Request $request
  75.      * @return Response
  76.      */
  77.     public function loginWithSteamResponse(
  78.         SessionInterface $session,
  79.         EntityManagerInterface $entityManager,
  80.         Request $request
  81.     ): Response {
  82.         $urlSteamId $request->query->get('openid_identity');
  83.         $tmp explode("/"$urlSteamId);
  84.         $steamId end($tmp);
  85.         /** @var User $user */
  86.         $user $this->getUser();
  87.         if ($user->getSteamId() !== $steamId) {
  88.             $user->setSteamId($steamId);
  89.             $entityManager->persist($user);
  90.             $entityManager->flush();
  91.         }
  92.         return $this->redirectToRoute('home');
  93.     }
  94. }