Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing OAuth1.0a responses for Twitter #1821

Merged
merged 1 commit into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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