. */ declare(strict_types=1); namespace App\Controller; use App\Services\OAuth\OAuthTokenManager; use KnpU\OAuth2ClientBundle\Client\ClientRegistry; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use function Symfony\Component\Translation\t; #[Route('/oauth/client')] class OAuthClientController extends AbstractController { public function __construct(private readonly ClientRegistry $clientRegistry, private readonly OAuthTokenManager $tokenManager) { } #[Route('/{name}/connect', name: 'oauth_client_connect')] public function connect(string $name): Response { $this->denyAccessUnlessGranted('@system.manage_oauth_tokens'); return $this->clientRegistry ->getClient($name) // key used in config/packages/knpu_oauth2_client.yaml ->redirect([], []); } #[Route('/{name}/check', name: 'oauth_client_check')] public function check(string $name, Request $request): Response { $this->denyAccessUnlessGranted('@system.manage_oauth_tokens'); $client = $this->clientRegistry->getClient($name); $access_token = $client->getAccessToken(); $this->tokenManager->saveToken($name, $access_token); $this->addFlash('success', t('oauth_client.flash.connection_successful')); return $this->redirectToRoute('homepage'); } }