Last active
October 23, 2025 12:55
-
-
Save ajgarlag/1f84d29ee0e1a92c8878f44a902338cd to your computer and use it in GitHub Desktop.
Simple league/oauth2-bundle decision flow
This file contains hidden or 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
| <?php | |
| //src/Controller/DecisionController.php | |
| namespace App\Controller; | |
| use App\EventSubscriber\SignedAuthorizationRequestSubscriber; | |
| use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface; | |
| use League\Bundle\OAuth2ServerBundle\Manager\Doctrine\ClientManager; | |
| use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | |
| use Symfony\Component\HttpFoundation\Request; | |
| use Symfony\Component\HttpFoundation\Response; | |
| use Symfony\Component\HttpFoundation\UriSigner; | |
| use Symfony\Component\HttpKernel\Attribute\MapQueryParameter; | |
| use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | |
| use Symfony\Component\Routing\Attribute\Route; | |
| use Symfony\Component\Security\Http\Attribute\IsGranted; | |
| class DecisionController extends AbstractController | |
| { | |
| public function __construct( | |
| private readonly UriSigner $uriSigner, | |
| private readonly ClientManagerInterface $clientManager, | |
| private readonly string $authorizationRoute, | |
| ) { | |
| } | |
| #[Route('/oauth2/authorize/decision', name: 'oauth2_authorize_decision')] | |
| #[IsGranted('ROLE_USER')] | |
| public function __invoke(Request $request) | |
| { | |
| Request $request, | |
| #[MapQueryParameter('client_id')] string $clientId, | |
| #[MapQueryParameter('redirect_uri')] string $redirectUri, | |
| #[MapQueryParameter('scope')] string $scope = '', | |
| ): Response { | |
| $client = $this->clientManager->find($clientId); | |
| if (null === $client) { | |
| throw new BadRequestHttpException(); | |
| } | |
| $scopes = '' === $scope ? array_map(strval(...), $client->getScopes()) : explode(' ', $scope); | |
| return $this->render('oauth2/authorize_decision.html.twig', [ | |
| 'client' => $client, | |
| 'redirect_uri' => $redirectUri, | |
| 'scopes' => $scopes, | |
| 'allow_uri' => $this->buildDecidedUri($request, true), | |
| 'deny_uri' => $this->buildDecidedUri($request, false), | |
| ]); | |
| } | |
| private function buildDecidedUri(Request $request, bool $allowed) | |
| { | |
| $currentQuery = $request->query->all(); | |
| $decidedQuery = array_merge($currentQuery, [SignedAuthorizationRequestSubscriber::ATTRIBUTE_DECISION => $this->buildDecisionValue($allowed)]); | |
| $decidedUri = $this->generateUrl($this->authorizationRoute, $decidedQuery); | |
| return $this->uriSigner->sign($decidedUri); | |
| } | |
| private function buildDecisionValue(bool $allowed): string | |
| { | |
| return $allowed ? SignedAuthorizationRequestSubscriber::ATTRIBUTE_DECISION_ALLOW : ''; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
to solve error
Argument must implement interface Psr\Http\Message\ResponseInterface, instead of Symfony\Component\HttpFoundation\RedirectResponsedo
composer require nyholm/psr7add this on the top
change this line
https://gist.github.com/ajgarlag/1f84d29ee0e1a92c8878f44a902338cd#file-signedauthorizationrequestsubscriber-php-L141
to
hope this help, even a bit late @mssoylu