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

possible fix for symfony 5 compatible version #7

Open
elchris opened this issue Jan 2, 2020 · 0 comments
Open

possible fix for symfony 5 compatible version #7

elchris opened this issue Jan 2, 2020 · 0 comments

Comments

@elchris
Copy link

elchris commented Jan 2, 2020

$psrRequest = (new DiactorosFactory)->createRequest($request);

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); } } `
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant