symfony4-oauth2-server-league icon indicating copy to clipboard operation
symfony4-oauth2-server-league copied to clipboard

possible fix for symfony 5 compatible version

Open elchris opened this issue 5 years ago • 0 comments

https://github.com/JeffreyVerreckt/symfony4-oauth2-server-league/blob/01e14258eab785cd2214ce3c7547f351182777a6/src/Application/EventSubscriber/TokenSubscriber.php#L64

Below is a modified version of this file from following breadcrumbs of deprecation in symfony 5, most notably replacing Diactoros with PsrHttpFactory and passing it a single instance of Psr17Factory (from nyholm/psr-17) for all argument constructors ... I'm no expert though, so I'm really guessing things.

Thank you for all your work.

`

resourceServer = $resourceServer; } /** * @return array */ public static function getSubscribedEvents(): array { return [ KernelEvents::CONTROLLER => 'onKernelController', KernelEvents::EXCEPTION => 'onKernelException' ]; } /** * @param ControllerEvent $event * @throws OAuthServerException */ public function onKernelController(ControllerEvent $event): void { $controller = $event->getController(); /* * $controller passed can be either a class or a Closure. * This is not usual in Symfony but it may happen. * If it is a class, it comes in array format */ if (!\is_array($controller)) { return; } if ($controller[0] instanceof TokenAuthenticatedController) { $request = $event->getRequest(); $psr17Factory = new Psr17Factory(); $psrRequest = (new PsrHttpFactory( $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory ))->createRequest($request); try { $psrRequest = $this->resourceServer->validateAuthenticatedRequest($psrRequest); } catch (OAuthServerException $exception) { throw $exception; } catch (\Exception $exception) { throw new OAuthServerException($exception->getMessage(), 0, 'unknown_error', Response::HTTP_INTERNAL_SERVER_ERROR); } $this->enrichSymfonyRequestWithAuthData($request, $psrRequest); } } /** * @param Request $request * @param ServerRequestInterface $psrRequest */ private function enrichSymfonyRequestWithAuthData(Request $request, ServerRequestInterface $psrRequest): void { $request = $request->request; $requestArray = $request->all(); $requestArray['oauth_user_id'] = $psrRequest->getAttribute('oauth_user_id'); $requestArray['oauth_access_token_id'] = $psrRequest->getAttribute('oauth_access_token_id'); $requestArray['oauth_client_id'] = $psrRequest->getAttribute('oauth_client_id'); $request->replace($requestArray); } /** * @param ExceptionEvent $event */ public function onKernelException(ExceptionEvent $event): void { $exception = $event->getThrowable(); if (!($exception instanceof OAuthServerException)) { return; } $response = new JsonResponse(['error' => $exception->getMessage()], $exception->getHttpStatusCode()); $event->setResponse($response); } } `

elchris avatar Jan 02 '20 02:01 elchris