Skip to content

Commit 8bcce14

Browse files
authored
Merge pull request #726 from schmengler/json-parse-error
Throw exception on Non-JSON response from access token request
2 parents d6c2003 + 9297f3b commit 8bcce14

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/Provider/AbstractProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,11 @@ public function getAccessToken($grant, array $options = [])
527527
$params = $grant->prepareRequestParameters($params, $options);
528528
$request = $this->getAccessTokenRequest($params);
529529
$response = $this->getParsedResponse($request);
530+
if (false === is_array($response)) {
531+
throw new UnexpectedValueException(
532+
'Invalid response received from Authorization Server. Expected JSON.'
533+
);
534+
}
530535
$prepared = $this->prepareAccessTokenResponse($response);
531536
$token = $this->createAccessToken($prepared, $grant);
532537

test/src/Provider/AbstractProviderTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,24 @@ public function testGetAccessToken($method)
548548
);
549549
}
550550

551+
public function testGetAccessTokenWithNonJsonResponse()
552+
{
553+
$stream = Phony::mock(StreamInterface::class);
554+
$stream->__toString->returns('');
555+
556+
$response = Phony::mock(ResponseInterface::class);
557+
$response->getBody->returns($stream->get());
558+
$response->getHeader->with('content-type')->returns('text/plain');
559+
560+
$client = Phony::mock(ClientInterface::class);
561+
$client->send->returns($response->get());
562+
$this->provider->setHttpClient($client->get());
563+
564+
$this->expectException(\UnexpectedValueException::class);
565+
$this->expectExceptionMessage('Invalid response received from Authorization Server. Expected JSON.');
566+
$this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
567+
}
568+
551569
private function getMethod($class, $name)
552570
{
553571
$class = new \ReflectionClass($class);

0 commit comments

Comments
 (0)