<?php
namespace App\Controller;
use App\Entity\Role;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class SecurityController extends AbstractController
{
private const DISCORD_ENDPOINT = 'https://discord.com/api/oauth2/authorize';
private const STEAM_ENDPOINT = 'https://steamcommunity.com/openid/login';
/**
* @Route("/login/failed", name="failed")
* @param Request $request
* @return Response
*/
public function failed(Request $request): Response
{
return $this->render('security/failed.html.twig', ['countryGroup' => $request->getLocale()]);
}
/**
* @Route("/login/discord", name="login_discord", methods={"GET"})
* @param SessionInterface $session
* @param UrlGeneratorInterface $urlGenerator
* @return Response
*/
public function loginWithDiscord(SessionInterface $session, UrlGeneratorInterface $urlGenerator): Response
{
$redirectURL = $urlGenerator->generate('home', [
'discord-oauth-provider' => true
], UrlGeneratorInterface::ABSOLUTE_URL);
$queryParams = http_build_query([
'client_id' => getenv("DISCORD_ID"),
'prompt' => 'consent',
'redirect_url' => $redirectURL,
'response_type' => 'code',
'scope' => 'guilds.join identify email'
]);
return new RedirectResponse(self::DISCORD_ENDPOINT . '?' . $queryParams);
}
/**
* @Route("/login/steam", name="login_steam", methods={"GET"})
* @param SessionInterface $session
* @param UrlGeneratorInterface $urlGenerator
* @return Response
*/
public function loginWithSteam(SessionInterface $session, UrlGeneratorInterface $urlGenerator): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
if (!$this->getUser()) {
return $this->redirectToRoute('home');
}
$redirectURL = $urlGenerator->generate('login_steam_response', [], UrlGeneratorInterface::ABSOLUTE_URL);
$queryParams = http_build_query([
'openid.return_to' => $redirectURL,
'openid.mode' => 'checkid_setup',
'openid.ns' => 'http://specs.openid.net/auth/2.0',
'openid.realm' => $redirectURL,
'openid.ns.sreg' => 'http://openid.net/extensions/sreg/1.1',
'openid.claimed_id' => 'http://specs.openid.net/auth/2.0/identifier_select',
'openid.identity' => 'http://specs.openid.net/auth/2.0/identifier_select',
]);
return new RedirectResponse(self::STEAM_ENDPOINT . '?' . $queryParams);
}
/**
* @Route("/login/steam/response", name="login_steam_response", methods={"GET"})
* @param SessionInterface $session
* @param EntityManagerInterface $entityManager
* @param Request $request
* @return Response
*/
public function loginWithSteamResponse(
SessionInterface $session,
EntityManagerInterface $entityManager,
Request $request
): Response {
$urlSteamId = $request->query->get('openid_identity');
$tmp = explode("/", $urlSteamId);
$steamId = end($tmp);
/** @var User $user */
$user = $this->getUser();
if ($user->getSteamId() !== $steamId) {
$user->setSteamId($steamId);
$entityManager->persist($user);
$entityManager->flush();
}
return $this->redirectToRoute('home');
}
}