Skip to content

Commit

Permalink
Merge pull request #676 from pradtke/bug/handle-malformed-error
Browse files Browse the repository at this point in the history
Avoid 'Fatal error: Wrong parameters for Exception' in error response
  • Loading branch information
ramsey authored Jan 13, 2018
2 parents 63cd3ec + 5ad573b commit 909a56d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/Provider/GenericProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,13 @@ protected function checkResponse(ResponseInterface $response, $data)
{
if (!empty($data[$this->responseError])) {
$error = $data[$this->responseError];
$code = $this->responseCode ? $data[$this->responseCode] : 0;
if (!is_string($error)) {
$error = var_export($error, true);
}
$code = $this->responseCode && !empty($data[$this->responseCode])? $data[$this->responseCode] : 0;
if (!is_int($code)) {
$code = intval($code);
}
throw new IdentityProviderException($error, $code, $data);
}
}
Expand Down
23 changes: 18 additions & 5 deletions test/src/Provider/GenericProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,12 @@ public function testCheckResponse()
}

/**
* @param array $error The error response to parse
* @param array $extraOptions Any extra options to configure the generic provider with.
* @dataProvider checkResponseThrowsExceptionProvider
* @expectedException League\Oauth2\Client\Provider\Exception\IdentityProviderException
*/
public function testCheckResponseThrowsException()
public function testCheckResponseThrowsException(array $error, array $extraOptions = [])
{
$response = Phony::mock(ResponseInterface::class);

Expand All @@ -141,15 +144,25 @@ public function testCheckResponseThrowsException()
'urlResourceOwnerDetails' => 'http://example.com/user',
];

$provider = new GenericProvider($options);
$provider = new GenericProvider($options + $extraOptions);

$reflection = new \ReflectionClass(get_class($provider));

$checkResponse = $reflection->getMethod('checkResponse');
$checkResponse->setAccessible(true);

$checkResponse->invokeArgs($provider, [$response->get(), [
'error' => 'foobar',
]]);
$checkResponse->invokeArgs($provider, [$response->get(), $error]);
}

public function checkResponseThrowsExceptionProvider() {
return [
[['error' => 'foobar',]],
[['error' => 'foobar',] , ['responseCode' => 'code']],
// Some servers return non-compliant responses. Provider shouldn't 'Fatal error: Wrong parameters'
[['error' => 'foobar', 'code' => 'abc55'], ['responseCode' => 'code']],
[['error' => 'foobar', 'code' => ['badformat']], ['responseCode' => 'code']],
[['error' => ['message' => 'msg', 'code' => 56]]],
[['error' => ['errors' => ['code' => 67, 'message' => 'msg']]]],
];
}
}

0 comments on commit 909a56d

Please sign in to comment.