Skip to content

Commit

Permalink
Using json response.
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Sep 16, 2020
1 parent 8bd24f3 commit d360e06
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 145 deletions.
2 changes: 1 addition & 1 deletion src/Providers/FeiShu.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected function getUserByToken(string $token): array
$response = $this->getHttpClient()->get(
$this->baseUrl . '/authen/v1/user_info',
[
'headers' => ['Content-Type' => 'application/json', 'Authorization'=>'Bearer ' . $token],
'headers' => ['Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $token],
'query' => array_filter(
[
'user_access_token' => $token,
Expand Down
27 changes: 6 additions & 21 deletions src/Providers/QQ.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ protected function getTokenFields($code): array
/**
* @param string $code
*
* @throws \Overtrue\Socialite\Exceptions\AuthorizeFailedException
*
* @return array
* @throws \Overtrue\Socialite\Exceptions\AuthorizeFailedException|\GuzzleHttp\Exception\GuzzleException
*/
public function tokenFromCode($code): array
{
Expand All @@ -65,25 +64,27 @@ public function withUnionId(): self
* @param string $token
*
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function getUserByToken(string $token): array
{
$url = $this->baseUrl.'/oauth2.0/me?access_token='.$token;
$url = $this->baseUrl.'/oauth2.0/me?fmt=json&access_token='.$token;
$this->withUnionId && $url .= '&unionid=1';

$response = $this->getHttpClient()->get($url);

$me = json_decode($this->removeCallback($response->getBody()->getContents()), true);
$me = \json_decode($response->getBody()->getContents(), true);

$queries = [
'access_token' => $token,
'fmt' => 'json',
'openid' => $me['openid'],
'oauth_consumer_key' => $this->getClientId(),
];

$response = $this->getHttpClient()->get($this->baseUrl.'/user/get_user_info?'.http_build_query($queries));

return (\json_decode($this->removeCallback($response->getBody()->getContents()), true) ?? []) + [
return (\json_decode($response->getBody()->getContents(), true) ?? []) + [
'unionid' => $me['unionid'] ?? null,
'openid' => $me['openid'] ?? null,
];
Expand All @@ -104,20 +105,4 @@ protected function mapUserToObject(array $user): User
'avatar' => $user['figureurl_qq_2'] ?? null,
]);
}

/**
* @param string $response
*
* @return string
*/
protected function removeCallback($response)
{
if (false !== strpos($response, 'callback')) {
$lpos = strpos($response, '(');
$rpos = strrpos($response, ')');
$response = substr($response, $lpos + 1, $rpos - $lpos - 1);
}

return $response;
}
}
4 changes: 2 additions & 2 deletions tests/OAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function test_it_can_get_token()
]);

$provider->getHttpClient()->shouldReceive('post')->with('http://token.url', [
'json' => [
'form_params' => [
'client_id' => 'fake_client_id',
'client_secret' => 'fake_client_secret',
'code' => 'fake_code',
Expand Down Expand Up @@ -133,7 +133,7 @@ public function test_it_can_get_user_by_code()
]);

$provider->getHttpClient()->shouldReceive('post')->with('http://token.url', [
'json' => [
'form_params' => [
'client_id' => 'fake_client_id',
'client_secret' => 'fake_client_secret',
'code' => 'fake_code',
Expand Down
2 changes: 1 addition & 1 deletion tests/Providers/WechatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function testWeChatProviderHasCorrectlyRedirectResponse()
]))->redirect();

$this->assertStringStartsWith('https://open.weixin.qq.com/connect/qrconnect', $response);
$this->assertMatchesRegularExpression('/redirect_uri=http%3A%2F%2Flocalhost%2Fsocialite%2Fcallback.php/', $response);
$this->assertRegExp('/redirect_uri=http%3A%2F%2Flocalhost%2Fsocialite%2Fcallback.php/', $response);
}

public function testWeChatProviderTokenUrlAndRequestFields()
Expand Down
120 changes: 0 additions & 120 deletions tests/WechatProviderTest.php

This file was deleted.

0 comments on commit d360e06

Please sign in to comment.