-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
debounced
authored
Jul 10, 2024
1 parent
a4692e3
commit 7b97ec2
Showing
14 changed files
with
311 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoctrineMigrations; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\Migrations\AbstractMigration; | ||
|
||
/** | ||
* Auto-generated Migration: Please modify to your needs! | ||
*/ | ||
final class Version20240706005744 extends AbstractMigration | ||
{ | ||
public function getDescription(): string | ||
{ | ||
return 'Add Discord SSO'; | ||
} | ||
|
||
public function up(Schema $schema): void | ||
{ | ||
// this up() migration is auto-generated, please modify it to your needs | ||
$this->addSql('ALTER TABLE "user" ADD oauth_discord_id VARCHAR(255) DEFAULT NULL'); | ||
} | ||
|
||
public function down(Schema $schema): void | ||
{ | ||
// this down() migration is auto-generated, please modify it to your needs | ||
$this->addSql('ALTER TABLE "user" DROP oauth_discord_id'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Controller\Security; | ||
|
||
use App\Controller\AbstractController; | ||
use KnpU\OAuth2ClientBundle\Client\ClientRegistry; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class DiscordController extends AbstractController | ||
{ | ||
public function connect(ClientRegistry $clientRegistry): Response | ||
{ | ||
return $clientRegistry | ||
->getClient('discord') | ||
->redirect(); | ||
} | ||
|
||
public function verify(Request $request, ClientRegistry $client) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Security; | ||
|
||
use App\DTO\UserDto; | ||
use App\Entity\User; | ||
use App\Factory\ImageFactory; | ||
use App\Repository\ImageRepository; | ||
use App\Service\ImageManager; | ||
use App\Service\IpResolver; | ||
use App\Service\SettingsManager; | ||
use App\Service\UserManager; | ||
use App\Utils\Slugger; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use KnpU\OAuth2ClientBundle\Client\ClientRegistry; | ||
use KnpU\OAuth2ClientBundle\Security\Authenticator\OAuth2Authenticator; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\RouterInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Exception\AuthenticationException; | ||
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Passport; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport; | ||
use Wohali\OAuth2\Client\Provider\DiscordResourceOwner; | ||
|
||
class DiscordAuthenticator extends OAuth2Authenticator | ||
{ | ||
public function __construct( | ||
private readonly ClientRegistry $clientRegistry, | ||
private readonly RouterInterface $router, | ||
private readonly EntityManagerInterface $entityManager, | ||
private readonly UserManager $userManager, | ||
private readonly ImageManager $imageManager, | ||
private readonly ImageFactory $imageFactory, | ||
private readonly ImageRepository $imageRepository, | ||
private readonly RequestStack $requestStack, | ||
private readonly IpResolver $ipResolver, | ||
private readonly Slugger $slugger, | ||
private readonly SettingsManager $settingsManager | ||
) { | ||
} | ||
|
||
public function supports(Request $request): ?bool | ||
{ | ||
return 'oauth_discord_verify' === $request->attributes->get('_route'); | ||
} | ||
|
||
public function authenticate(Request $request): Passport | ||
{ | ||
$client = $this->clientRegistry->getClient('discord'); | ||
$slugger = $this->slugger; | ||
$session = $this->requestStack->getSession(); | ||
|
||
$accessToken = $this->fetchAccessToken($client, ['prompt' => 'consent', 'accessType' => 'offline']); | ||
$session->set('access_token', $accessToken); | ||
|
||
$accessToken = $session->get('access_token'); | ||
|
||
if ($accessToken->hasExpired()) { | ||
$accessToken = $client->refreshAccessToken($accessToken->getRefreshToken()); | ||
$session->set('access_token', $accessToken); | ||
} | ||
|
||
$rememberBadge = new RememberMeBadge(); | ||
$rememberBadge = $rememberBadge->enable(); | ||
|
||
return new SelfValidatingPassport( | ||
new UserBadge($accessToken->getToken(), function () use ($accessToken, $client, $slugger) { | ||
/** @var DiscordResourceOwner $discordUser */ | ||
$discordUser = $client->fetchUserFromToken($accessToken); | ||
|
||
$existingUser = $this->entityManager->getRepository(User::class)->findOneBy( | ||
['oauthDiscordId' => $discordUser->getId()] | ||
); | ||
|
||
if ($existingUser) { | ||
return $existingUser; | ||
} | ||
|
||
$user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $discordUser->getEmail()] | ||
); | ||
|
||
if ($user) { | ||
$user->oauthDiscordId = $discordUser->getId(); | ||
|
||
$this->entityManager->persist($user); | ||
$this->entityManager->flush(); | ||
|
||
return $user; | ||
} | ||
|
||
if (false === $this->settingsManager->get('MBIN_SSO_REGISTRATIONS_ENABLED')) { | ||
throw new CustomUserMessageAuthenticationException('MBIN_SSO_REGISTRATIONS_ENABLED'); | ||
} | ||
|
||
$dto = (new UserDto())->create( | ||
$slugger->slug($discordUser->getUsername()).rand(1, 999), | ||
$discordUser->getEmail() | ||
); | ||
|
||
$dto->plainPassword = bin2hex(random_bytes(20)); | ||
$dto->ip = $this->ipResolver->resolve(); | ||
|
||
$user = $this->userManager->create($dto, false); | ||
$user->oauthDiscordId = $discordUser->getId(); | ||
$user->isVerified = true; | ||
|
||
$this->entityManager->persist($user); | ||
$this->entityManager->flush(); | ||
|
||
return $user; | ||
}), | ||
[ | ||
$rememberBadge, | ||
] | ||
); | ||
} | ||
|
||
public function onAuthenticationSuccess( | ||
Request $request, | ||
TokenInterface $token, | ||
string $firewallName | ||
): ?Response { | ||
$targetUrl = $this->router->generate('front'); | ||
|
||
return new RedirectResponse($targetUrl); | ||
} | ||
|
||
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response | ||
{ | ||
$message = strtr($exception->getMessageKey(), $exception->getMessageData()); | ||
|
||
if ('MBIN_SSO_REGISTRATIONS_ENABLED' === $message) { | ||
$session = $request->getSession(); | ||
$session->getFlashBag()->add('error', 'sso_registrations_enabled.error'); | ||
|
||
return new RedirectResponse($this->router->generate('app_login')); | ||
} | ||
|
||
return new Response($message, Response::HTTP_FORBIDDEN); | ||
} | ||
} |
Oops, something went wrong.