|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the overtrue/socialite. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) overtrue <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Overtrue\Socialite\Providers; |
|
13
|
|
|
|
|
14
|
|
|
use Overtrue\Socialite\AccessToken; |
|
15
|
|
|
use Overtrue\Socialite\AccessTokenInterface; |
|
16
|
|
|
use Overtrue\Socialite\ProviderInterface; |
|
17
|
|
|
use Overtrue\Socialite\User; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class QQProvider. |
|
21
|
|
|
* |
|
22
|
|
|
* @link http://wiki.connect.qq.com/oauth2-0%E7%AE%80%E4%BB%8B [QQ - OAuth 2.0 登录QQ] |
|
23
|
|
|
*/ |
|
24
|
|
|
class QQProvider extends AbstractProvider implements ProviderInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* The base url of QQ API. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $baseUrl = 'https://graph.qq.com'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* User openid. |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $openId; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* get token(openid) with unionid. |
|
42
|
|
|
* @var bool |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $withUnionId = false; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* User unionid. |
|
48
|
|
|
* |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $unionId; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* The scopes being requested. |
|
55
|
|
|
* |
|
56
|
|
|
* @var array |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $scopes = ['get_user_info']; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* The uid of user authorized. |
|
62
|
|
|
* |
|
63
|
|
|
* @var int |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $uid; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get the authentication URL for the provider. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $state |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function getAuthUrl($state) |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->buildAuthUrlFromBase($this->baseUrl.'/oauth2.0/authorize', $state); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get the token URL for the provider. |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function getTokenUrl() |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->baseUrl.'/oauth2.0/token'; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get the Post fields for the token request. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $code |
|
93
|
|
|
* |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function getTokenFields($code) |
|
97
|
|
|
{ |
|
98
|
|
|
return parent::getTokenFields($code) + ['grant_type' => 'authorization_code']; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get the access token for the given code. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $code |
|
105
|
|
|
* |
|
106
|
|
|
* @return \Overtrue\Socialite\AccessToken |
|
107
|
|
|
*/ |
|
108
|
|
View Code Duplication |
public function getAccessToken($code) |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
|
|
$response = $this->getHttpClient()->get($this->getTokenUrl(), [ |
|
111
|
|
|
'query' => $this->getTokenFields($code), |
|
112
|
|
|
]); |
|
113
|
|
|
|
|
114
|
|
|
return $this->parseAccessToken($response->getBody()->getContents()); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get the access token from the token response body. |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $body |
|
121
|
|
|
* |
|
122
|
|
|
* @return \Overtrue\Socialite\AccessToken |
|
123
|
|
|
*/ |
|
124
|
|
|
public function parseAccessToken($body) |
|
125
|
|
|
{ |
|
126
|
|
|
parse_str($body, $token); |
|
127
|
|
|
|
|
128
|
|
|
return new AccessToken($token); |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* |
|
133
|
|
|
* @return self |
|
134
|
|
|
*/ |
|
135
|
|
|
public function withUnionId() { |
|
136
|
|
|
$this->withUnionId = true; |
|
137
|
|
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Get the raw user for the given access token. |
|
142
|
|
|
* |
|
143
|
|
|
* @param \Overtrue\Socialite\AccessTokenInterface $token |
|
144
|
|
|
* |
|
145
|
|
|
* @return array |
|
146
|
|
|
*/ |
|
147
|
|
|
protected function getUserByToken(AccessTokenInterface $token) |
|
148
|
|
|
{ |
|
149
|
|
|
$url = $this->baseUrl.'/oauth2.0/me?access_token='.$token->getToken(); |
|
150
|
|
|
$this->withUnionId && $url .= '&unionid=1'; |
|
151
|
|
|
|
|
152
|
|
|
$response = $this->getHttpClient()->get($url); |
|
153
|
|
|
|
|
154
|
|
|
$me = json_decode($this->removeCallback($response->getBody()->getContents()), true); |
|
155
|
|
|
$this->openId = $me['openid']; |
|
156
|
|
|
$this->unionId = isset($me['unionid']) ? $me['unionid'] : ''; |
|
157
|
|
|
|
|
158
|
|
|
$queries = [ |
|
159
|
|
|
'access_token' => $token->getToken(), |
|
160
|
|
|
'openid' => $this->openId, |
|
161
|
|
|
'oauth_consumer_key' => $this->clientId, |
|
162
|
|
|
]; |
|
163
|
|
|
|
|
164
|
|
|
$response = $this->getHttpClient()->get($this->baseUrl.'/user/get_user_info?'.http_build_query($queries)); |
|
165
|
|
|
|
|
166
|
|
|
return json_decode($this->removeCallback($response->getBody()->getContents()), true); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Map the raw user array to a Socialite User instance. |
|
171
|
|
|
* |
|
172
|
|
|
* @param array $user |
|
173
|
|
|
* |
|
174
|
|
|
* @return \Overtrue\Socialite\User |
|
175
|
|
|
*/ |
|
176
|
|
|
protected function mapUserToObject(array $user) |
|
177
|
|
|
{ |
|
178
|
|
|
return new User([ |
|
179
|
|
|
'id' => $this->openId, |
|
180
|
|
|
'unionid' => $this->unionId, |
|
181
|
|
|
'nickname' => $this->arrayItem($user, 'nickname'), |
|
182
|
|
|
'name' => $this->arrayItem($user, 'nickname'), |
|
183
|
|
|
'email' => $this->arrayItem($user, 'email'), |
|
184
|
|
|
'avatar' => $this->arrayItem($user, 'figureurl_qq_2'), |
|
185
|
|
|
]); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Remove the fucking callback parentheses. |
|
190
|
|
|
* |
|
191
|
|
|
* @param string $response |
|
192
|
|
|
* |
|
193
|
|
|
* @return string |
|
194
|
|
|
*/ |
|
195
|
|
View Code Duplication |
protected function removeCallback($response) |
|
|
|
|
|
|
196
|
|
|
{ |
|
197
|
|
|
if (strpos($response, 'callback') !== false) { |
|
198
|
|
|
$lpos = strpos($response, '('); |
|
199
|
|
|
$rpos = strrpos($response, ')'); |
|
200
|
|
|
$response = substr($response, $lpos + 1, $rpos - $lpos - 1); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return $response; |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.