Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing documentation on how to implement authorization code grant #51

Open
Tetragramat opened this issue Sep 30, 2021 · 0 comments
Open
Labels
Documentation Improvements or additions to documentation good first issue Good for newcomers help wanted Extra attention is needed

Comments

@Tetragramat
Copy link

Tetragramat commented Sep 30, 2021

The most important thing is documentation on that you need to make event listener that sets authorization resolution to true.
Ideally should have example on how to display page with two buttons to accept or deny authorization.

https://github.com/thephpleague/oauth2-server-bundle/blob/master/src/Controller/AuthorizationController.php#L101

My implementation for inspiration:

<?php

namespace App\EventListener\OAuth2;

use Nyholm\Psr7\Response;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Trikoder\Bundle\OAuth2Bundle\Event\AuthorizationRequestResolveEvent;
use Trikoder\Bundle\OAuth2Bundle\OAuth2Events;

class AuthorizationRequestResolverSubscriber implements EventSubscriberInterface
{
	public const SESSION_AUTHORIZATION_RESULT = '_app.oauth2.authorization_result';

	private RequestStack $requestStack;
	private UrlGeneratorInterface $urlGenerator;

	public function __construct(RequestStack $requestStack, UrlGeneratorInterface $urlGenerator)
	{
		$this->requestStack = $requestStack;
		$this->urlGenerator = $urlGenerator;
	}

	public static function getSubscribedEvents(): array
	{
		return [
			OAuth2Events::AUTHORIZATION_REQUEST_RESOLVE => 'resolve',
		];
	}

	public function resolve(AuthorizationRequestResolveEvent $event): void
	{
		$request = $this->requestStack->getCurrentRequest();

		if ($request->getSession()->has(self::SESSION_AUTHORIZATION_RESULT)) {
			$event->resolveAuthorization($request->getSession()->get(self::SESSION_AUTHORIZATION_RESULT));
			$request->getSession()->remove(self::SESSION_AUTHORIZATION_RESULT);

			return;
		}

		$event->setResponse(new Response(302, [
			'Location' => $this->urlGenerator->generate('app_consent', $request->query->all()),
		]));
	}
}
<?php

namespace App\Controller\Frontend;

use App\EventListener\OAuth2\AuthorizationRequestResolverSubscriber;
use App\Form\Type\PSD2\AuthorizationType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class OAuth2Controller extends AbstractController
{
	/**
	 * @Route("/consent", name="app_consent")
	 * @IsGranted("IS_AUTHENTICATED_FULLY")
	 */
	public function consent(Request $request): Response
	{
		$form = $this->createForm(AuthorizationType::class);
		$form->handleRequest($request);

		if ($form->isSubmitted() && $form->isValid()) {
			switch (true) {
				case $form->get('accept')->isClicked():
					$request->getSession()->set(AuthorizationRequestResolverSubscriber::SESSION_AUTHORIZATION_RESULT, true);
					break;
				case $form->get('refuse')->isClicked():
					$request->getSession()->set(AuthorizationRequestResolverSubscriber::SESSION_AUTHORIZATION_RESULT, false);
					break;
			}

			return $this->redirectToRoute('oauth2_authorize', $request->query->all());
		}

		return $this->render('oauth2/authorization.html.twig', [
			'form' => $form->createView(),
		]);
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Improvements or additions to documentation good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants