add support of refresh token
This commit is contained in:
parent
eb1ddce377
commit
17d1e1db1e
@ -37,7 +37,7 @@ php bin/console make:user
|
||||
|
||||
Note that you have to answer **no** to the question `Does this app need to hash/check user passwords?`
|
||||
|
||||
Then, you could copy the files `TrackMania.php` in `src/Controller/OAuth2/` and `TrackManiaAuthenticator.php` in `src/Security/`
|
||||
Then, you could copy the files `TrackMania.php` in `src/Controller/OAuth2/`, `TrackManiaAuthenticator.php` in `src/Security/` and `TrackManiaAuthenticatorSubscriber.php` in `src/Event/`.
|
||||
|
||||
Then change the `config/packages/knpu_oauth2_client.yaml` file like this:
|
||||
|
||||
|
@ -42,6 +42,11 @@ class TrackManiaAuthenticator extends OAuth2Authenticator
|
||||
$client = $this->clientRegistry->getClient('TrackMania');
|
||||
$accessToken = $this->fetchAccessToken($client);
|
||||
|
||||
// Store the refresh token in the session
|
||||
if ($accessToken->getRefreshToken() !== null) {
|
||||
$request->getSession()->set('oauth2_trackmania_accesstoken', $accessToken);
|
||||
}
|
||||
|
||||
$selfvalidating = new SelfValidatingPassport(
|
||||
new UserBadge($accessToken->getToken(), function() use ($accessToken, $client) {
|
||||
/** @var Beu\TrackMania\OAuth2\Client\Provider\TrackManiaProviderOwner $user */
|
||||
|
36
examples/TrackManiaAuthenticatorSubscriber.php
Normal file
36
examples/TrackManiaAuthenticatorSubscriber.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?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',
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user