Skip to content

Commit

Permalink
Fix request token event
Browse files Browse the repository at this point in the history
  • Loading branch information
ajgarlag authored and chalasr committed Oct 7, 2021
1 parent 464dfa8 commit f14c02d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/Controller/TokenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
29 changes: 21 additions & 8 deletions tests/Acceptance/TokenEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,32 @@ 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')
->addListener(OAuth2Events::TOKEN_REQUEST_RESOLVE, static function (TokenRequestResolveEvent $event): 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());
Expand All @@ -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
Expand Down

0 comments on commit f14c02d

Please sign in to comment.