36 lines
1.3 KiB
PHP
36 lines
1.3 KiB
PHP
|
<?php
|
||
|
namespace App\Event;
|
||
|
|
||
|
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
|
||
|
use Psr\Log\LoggerInterface;
|
||
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||
|
use Symfony\Component\HttpKernel\Event\ControllerEvent;
|
||
|
use Symfony\Component\HttpKernel\KernelEvents;
|
||
|
|
||
|
class TrackManiaAuthenticatorSubscriber implements EventSubscriberInterface
|
||
|
{
|
||
|
public function __construct(private ClientRegistry $clientRegistry, private LoggerInterface $logger) {}
|
||
|
|
||
|
public function onKernelController(ControllerEvent $event): void
|
||
|
{
|
||
|
/** @var \League\OAuth2\Client\Token\AccessToken|null */
|
||
|
$accessToken = $event->getRequest()->getSession()->get('oauth2_trackmania_accesstoken');
|
||
|
|
||
|
if ($accessToken !== null && $accessToken->hasExpired()) {
|
||
|
$client = $this->clientRegistry->getClient('TrackMania');
|
||
|
$accessToken = $client->refreshAccessToken($accessToken->getRefreshToken());
|
||
|
|
||
|
// Store the refresh token in the session
|
||
|
if ($accessToken->getRefreshToken() !== null) {
|
||
|
$event->getRequest()->getSession()->set('oauth2_trackmania_accesstoken', $accessToken);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static function getSubscribedEvents(): array
|
||
|
{
|
||
|
return [
|
||
|
KernelEvents::CONTROLLER => 'onKernelController',
|
||
|
];
|
||
|
}
|
||
|
}
|