From f14c02d0c8d2aa45ed524ff3ae9b0e112ee0e83f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20J=2E=20Garc=C3=ADa=20Lagar?= Date: Tue, 5 Oct 2021 18:32:34 +0200 Subject: [PATCH] Fix request token event --- src/Controller/TokenController.php | 4 ++-- tests/Acceptance/TokenEndpointTest.php | 29 +++++++++++++++++++------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/Controller/TokenController.php b/src/Controller/TokenController.php index 1a803464..948893ef 100644 --- a/src/Controller/TokenController.php +++ b/src/Controller/TokenController.php @@ -70,11 +70,11 @@ public function indexAction(Request $request): Response $renderedResponse = $this->httpFoundationFactory->createResponse($response); /** @var TokenRequestResolveEvent $event */ - $this->eventDispatcher->dispatch( + $event = $this->eventDispatcher->dispatch( new TokenRequestResolveEvent($renderedResponse), OAuth2Events::TOKEN_REQUEST_RESOLVE ); - return $renderedResponse; + return $event->getResponse(); } } diff --git a/tests/Acceptance/TokenEndpointTest.php b/tests/Acceptance/TokenEndpointTest.php index 26685670..daddf84d 100644 --- a/tests/Acceptance/TokenEndpointTest.php +++ b/tests/Acceptance/TokenEndpointTest.php @@ -105,13 +105,6 @@ public function testSuccessfulRefreshTokenRequest(): void ->get(RefreshTokenManagerInterface::class) ->find(FixtureFactory::FIXTURE_REFRESH_TOKEN); - $this->client->request('POST', '/token', [ - 'client_id' => 'foo', - 'client_secret' => 'secret', - 'grant_type' => 'refresh_token', - 'refresh_token' => TestHelper::generateEncryptedPayload($refreshToken), - ]); - $this->client ->getContainer() ->get('event_dispatcher') @@ -119,6 +112,25 @@ public function testSuccessfulRefreshTokenRequest(): void $event->getResponse()->headers->set('foo', 'bar'); }); + $this->client + ->getContainer() + ->get('event_dispatcher') + ->addListener(OAuth2Events::TOKEN_REQUEST_RESOLVE, static function (TokenRequestResolveEvent $event): void { + if ('bar' === $event->getResponse()->headers->get('foo')) { + $newResponse = clone $event->getResponse(); + $newResponse->headers->remove('foo'); + $newResponse->headers->set('baz', 'qux'); + $event->setResponse($newResponse); + } + }, -1); + + $this->client->request('POST', '/token', [ + 'client_id' => 'foo', + 'client_secret' => 'secret', + 'grant_type' => 'refresh_token', + 'refresh_token' => TestHelper::generateEncryptedPayload($refreshToken), + ]); + $response = $this->client->getResponse(); $this->assertSame(200, $response->getStatusCode()); @@ -131,7 +143,8 @@ public function testSuccessfulRefreshTokenRequest(): void $this->assertGreaterThan(0, $jsonResponse['expires_in']); $this->assertNotEmpty($jsonResponse['access_token']); $this->assertNotEmpty($jsonResponse['refresh_token']); - $this->assertEmpty($response->headers->get('foo'), 'bar'); + $this->assertFalse($response->headers->has('foo')); + $this->assertSame($response->headers->get('baz'), 'qux'); } public function testSuccessfulAuthorizationCodeRequest(): void