Skip to content

Commit

Permalink
Fix parsing OAuth1.0a responses
Browse files Browse the repository at this point in the history
Parses text/html;charset=utf-8 response as string instead of json. This is necessary for OAuth1.0a responses from Twitter.
  • Loading branch information
sjerdo committed Nov 15, 2021
1 parent cd20030 commit f2c9290
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
11 changes: 3 additions & 8 deletions OAuth/ResourceOwner/AbstractResourceOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,17 +290,12 @@ protected function httpRequest($url, $content = null, array $headers = [], $meth

protected function getResponseContent(ResponseInterface $rawResponse): array
{
$contentTypes = $rawResponse->getHeaders(false)['content-type'] ?? [];
if (\in_array('text/plain', $contentTypes, true)) {
parse_str($rawResponse->getContent(false), $response);

return $response;
}

try {
return $rawResponse->toArray(false);
} catch (JsonException $e) {
return [];
parse_str($rawResponse->getContent(false), $response);

return $response;
}
}

Expand Down
46 changes: 46 additions & 0 deletions Tests/OAuth/ResourceOwner/GenericOAuth1ResourceOwnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,52 @@ public function testGetAccessToken(): void
);
}

public function testGetAccessTokenHtmlResponse(): void
{
$request = new Request(['oauth_verifier' => 'code', 'oauth_token' => 'token']);

$resourceOwner = $this->createResourceOwner(
[],
[],
[
$this->createMockResponse('oauth_token=token&oauth_token_secret=secret', 'text/html;charset=utf-8'),
]
);

$this->storage->expects($this->once())
->method('fetch')
->with($resourceOwner, 'token')
->willReturn(['oauth_token' => 'token2', 'oauth_token_secret' => 'secret2']);

$this->assertEquals(
['oauth_token' => 'token', 'oauth_token_secret' => 'secret'],
$resourceOwner->getAccessToken($request, 'http://redirect.to/')
);
}

public function testGetAccessTokenUrlEncodedResponse(): void
{
$request = new Request(['oauth_verifier' => 'code', 'oauth_token' => 'token']);

$resourceOwner = $this->createResourceOwner(
[],
[],
[
$this->createMockResponse('oauth_token=token&oauth_token_secret=secret', 'application/x-www-form-urlencoded'),
]
);

$this->storage->expects($this->once())
->method('fetch')
->with($resourceOwner, 'token')
->willReturn(['oauth_token' => 'token2', 'oauth_token_secret' => 'secret2']);

$this->assertEquals(
['oauth_token' => 'token', 'oauth_token_secret' => 'secret'],
$resourceOwner->getAccessToken($request, 'http://redirect.to/')
);
}

public function testGetAccessTokenJsonResponse(): void
{
$resourceOwner = $this->createResourceOwner(
Expand Down
5 changes: 5 additions & 0 deletions Tests/OAuth/ResourceOwner/GenericOAuth2ResourceOwnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ public function provideAccessTokenData(): iterable
'text/plain',
];

yield 'html text with charset' => [
'access_token=code',
'text/html;charset=utf-8',
];

yield 'json' => [
'{"access_token": "code"}',
'application/json',
Expand Down

0 comments on commit f2c9290

Please sign in to comment.