From 41a12d9a823103d6bcf13d133891393b5a2e1bc9 Mon Sep 17 00:00:00 2001 From: Michel Date: Thu, 7 Nov 2024 21:42:28 +0100 Subject: [PATCH] Adds SSL support for Discord login --- src/php/Environment/EnvironmentHandler.php | 5 +++++ src/php/Login/DiscordLoginProvider.php | 11 +++++++++-- src/php/index.php | 2 -- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/php/Environment/EnvironmentHandler.php b/src/php/Environment/EnvironmentHandler.php index 95dd8c4..279e36b 100644 --- a/src/php/Environment/EnvironmentHandler.php +++ b/src/php/Environment/EnvironmentHandler.php @@ -37,4 +37,9 @@ final class EnvironmentHandler public function isProduction(): bool { return $_SERVER['PRODUCTION'] === 'true'; } + + public function useSSL(): bool + { + return $_SERVER['USE_SSL'] === 'true'; + } } \ No newline at end of file diff --git a/src/php/Login/DiscordLoginProvider.php b/src/php/Login/DiscordLoginProvider.php index 6e38991..cba3235 100644 --- a/src/php/Login/DiscordLoginProvider.php +++ b/src/php/Login/DiscordLoginProvider.php @@ -7,12 +7,14 @@ use Doctrine\ORM\EntityManager; use GamesShop\Api\DiscordAPI; use GamesShop\ContainerHandler; use GamesShop\Entities\Account\User; +use GamesShop\Environment\EnvironmentHandler; use Psr\Http\Message\ServerRequestInterface; final class DiscordLoginProvider implements LoginProvider { public function __construct( - private readonly EntityManager $entityManager + private readonly EntityManager $entityManager, + private readonly EnvironmentHandler $env ) { } @@ -20,7 +22,12 @@ final class DiscordLoginProvider implements LoginProvider public function getUser(ServerRequestInterface $request): User { $discordApiHandler = ContainerHandler::get(DiscordAPI::class); - $result = $discordApiHandler->getUserFromCode($request->getQueryParams()['code'], (string)$request->getUri()->withQuery('')); + $result = $discordApiHandler->getUserFromCode( + $request->getQueryParams()['code'], + (string)$request->getUri() + ->withScheme($this->env->useSSL() ? 'https' : 'http') + ->withQuery('') + ); $repo = $this->entityManager->getRepository(User::class); $users = $repo->findBy(['loginMethod' => LoginMethod::DISCORD, 'foreignLoginId' => $result['id']]); diff --git a/src/php/index.php b/src/php/index.php index 9419574..4857464 100644 --- a/src/php/index.php +++ b/src/php/index.php @@ -15,8 +15,6 @@ require_once __DIR__ . '/../src/php/bootstrap.php'; ContainerHandler::get(CrashHandler::class)->register(); -throw new Exception(); - $router = ContainerHandler::getInstance()->get(Router::class); $result = $router->route();