Skip to content

Missing documentation on how to implement authorization code grant #51

Open
@Tetragramat

Description

@Tetragramat

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(),
		]);
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationImprovements or additions to documentationgood first issueGood for newcomershelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions