|
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\InvalidArgumentException; |
|
17
|
|
|
use Overtrue\Socialite\ProviderInterface; |
|
18
|
|
|
use Overtrue\Socialite\User; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class WeChatProvider. |
|
22
|
|
|
* |
|
23
|
|
|
* @link http://mp.weixin.qq.com/wiki/9/01f711493b5a02f24b04365ac5d8fd95.html [WeChat - 公众平台OAuth文档] |
|
24
|
|
|
* @link https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN [网站应用微信登录开发指南] |
|
25
|
|
|
*/ |
|
26
|
|
|
class WeChatProvider extends AbstractProvider implements ProviderInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* The base url of WeChat API. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $baseUrl = 'https://api.weixin.qq.com/sns'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc}. |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $openId; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc}. |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $scopes = ['snsapi_login']; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Indicates if the session state should be utilized. |
|
47
|
|
|
* |
|
48
|
|
|
* @var bool |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $stateless = true; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc}. |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function getAuthUrl($state) |
|
56
|
|
|
{ |
|
57
|
|
|
$path = 'oauth2/authorize'; |
|
58
|
|
|
|
|
59
|
|
|
if (in_array('snsapi_login', $this->scopes)) { |
|
60
|
|
|
$path = 'qrconnect'; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $this->buildAuthUrlFromBase("https://open.weixin.qq.com/connect/{$path}", $state); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc}. |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function buildAuthUrlFromBase($url, $state) |
|
70
|
|
|
{ |
|
71
|
|
|
$query = http_build_query($this->getCodeFields($state), '', '&', $this->encodingType); |
|
72
|
|
|
|
|
73
|
|
|
return $url.'?'.$query.'#wechat_redirect'; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc}. |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function getCodeFields($state = null) |
|
80
|
|
|
{ |
|
81
|
|
|
return array_merge([ |
|
82
|
|
|
'appid' => $this->clientId, |
|
83
|
|
|
'redirect_uri' => $this->redirectUrl, |
|
84
|
|
|
'response_type' => 'code', |
|
85
|
|
|
'scope' => $this->formatScopes($this->scopes, $this->scopeSeparator), |
|
86
|
|
|
'state' => $state ?: md5(time()), |
|
87
|
|
|
], $this->parameters); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc}. |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function getTokenUrl() |
|
94
|
|
|
{ |
|
95
|
|
|
if ($this->isOpenPlatform()) { |
|
96
|
|
|
return $this->baseUrl . '/oauth2/component/access_token'; |
|
97
|
|
|
} |
|
98
|
|
|
return $this->baseUrl . '/oauth2/access_token'; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc}. |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function getUserByToken(AccessTokenInterface $token) |
|
105
|
|
|
{ |
|
106
|
|
|
$scopes = explode(',', $token->getAttribute('scope', '')); |
|
107
|
|
|
|
|
108
|
|
|
if (in_array('snsapi_base', $scopes)) { |
|
109
|
|
|
return $token->toArray(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (empty($token['openid'])) { |
|
113
|
|
|
throw new InvalidArgumentException('openid of AccessToken is required.'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$response = $this->getHttpClient()->get($this->baseUrl.'/userinfo', [ |
|
117
|
|
|
'query' => [ |
|
118
|
|
|
'access_token' => $token->getToken(), |
|
119
|
|
|
'openid' => $token['openid'], |
|
120
|
|
|
'lang' => 'zh_CN', |
|
121
|
|
|
], |
|
122
|
|
|
]); |
|
123
|
|
|
|
|
124
|
|
|
return json_decode($response->getBody(), true); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* {@inheritdoc}. |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function mapUserToObject(array $user) |
|
131
|
|
|
{ |
|
132
|
|
|
return new User([ |
|
133
|
|
|
'id' => $this->arrayItem($user, 'openid'), |
|
134
|
|
|
'name' => $this->arrayItem($user, 'nickname'), |
|
135
|
|
|
'nickname' => $this->arrayItem($user, 'nickname'), |
|
136
|
|
|
'avatar' => $this->arrayItem($user, 'headimgurl'), |
|
137
|
|
|
'email' => null, |
|
138
|
|
|
]); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* {@inheritdoc}. |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function getTokenFields($code) |
|
145
|
|
|
{ |
|
146
|
|
|
$base = [ |
|
147
|
|
|
'appid' => $this->clientId, |
|
148
|
|
|
'code' => $code, |
|
149
|
|
|
'grant_type' => 'authorization_code' |
|
150
|
|
|
]; |
|
151
|
|
|
|
|
152
|
|
|
if ($this->isOpenPlatform()) { |
|
153
|
|
|
return array_merge($base, [ |
|
154
|
|
|
'component_appid' => $this->config->get('wechat.open_platform.app_id'), |
|
155
|
|
|
'component_access_token' => $this->config->get('wechat.open_platform.access_token') |
|
156
|
|
|
]); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return array_merge($base, [ |
|
160
|
|
|
'secret' => $this->clientSecret, |
|
161
|
|
|
]); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* {@inheritdoc}. |
|
166
|
|
|
*/ |
|
167
|
|
View Code Duplication |
public function getAccessToken($code) |
|
|
|
|
|
|
168
|
|
|
{ |
|
169
|
|
|
$response = $this->getHttpClient()->get($this->getTokenUrl(), [ |
|
170
|
|
|
'query' => $this->getTokenFields($code), |
|
171
|
|
|
]); |
|
172
|
|
|
|
|
173
|
|
|
return $this->parseAccessToken($response->getBody()->getContents()); |
|
|
|
|
|
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Detect wechat open platform. |
|
178
|
|
|
* |
|
179
|
|
|
* @return mixed |
|
180
|
|
|
*/ |
|
181
|
|
|
protected function isOpenPlatform() |
|
182
|
|
|
{ |
|
183
|
|
|
return $this->config->get('wechat.open_platform'); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Remove the fucking callback parentheses. |
|
188
|
|
|
* |
|
189
|
|
|
* @param mixed $response |
|
190
|
|
|
* |
|
191
|
|
|
* @return string |
|
192
|
|
|
*/ |
|
193
|
|
View Code Duplication |
protected function removeCallback($response) |
|
|
|
|
|
|
194
|
|
|
{ |
|
195
|
|
|
if (strpos($response, 'callback') !== false) { |
|
196
|
|
|
$lpos = strpos($response, '('); |
|
197
|
|
|
$rpos = strrpos($response, ')'); |
|
198
|
|
|
$response = substr($response, $lpos + 1, $rpos - $lpos - 1); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return $response; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
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.