|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Oakhope\OAuth2\Client\Provider; |
|
5
|
|
|
|
|
6
|
|
|
use League\OAuth2\Client\Grant\AbstractGrant; |
|
7
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
|
8
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
|
9
|
|
|
use League\OAuth2\Client\Provider\ResourceOwnerInterface; |
|
10
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
|
11
|
|
|
use League\OAuth2\Client\Tool\ArrayAccessorTrait; |
|
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
13
|
|
|
|
|
14
|
|
|
class WebProvider extends AbstractProvider |
|
15
|
|
|
{ |
|
16
|
|
|
use ArrayAccessorTrait; |
|
17
|
|
|
|
|
18
|
|
|
protected $appid; |
|
19
|
|
|
protected $secret; |
|
20
|
|
|
protected $redirect_uri; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Returns the base URL for authorizing a client. |
|
24
|
|
|
* |
|
25
|
|
|
* @return string |
|
26
|
|
|
*/ |
|
27
|
9 |
|
public function getBaseAuthorizationUrl() |
|
28
|
|
|
{ |
|
29
|
9 |
|
return 'https://open.weixin.qq.com/connect/qrconnect'; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Returns authorization parameters based on provided options. |
|
34
|
|
|
* |
|
35
|
|
|
* @param array $options |
|
36
|
|
|
* @return array Authorization parameters |
|
37
|
|
|
*/ |
|
38
|
9 |
|
protected function getAuthorizationParameters(array $options) |
|
39
|
|
|
{ |
|
40
|
|
|
$options += [ |
|
41
|
9 |
|
'appid' => $this->appid |
|
42
|
6 |
|
]; |
|
43
|
|
|
|
|
44
|
9 |
|
if (!isset($options['redirect_uri'])) { |
|
45
|
9 |
|
$options['redirect_uri'] = $this->redirect_uri; |
|
46
|
6 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
$options += [ |
|
49
|
3 |
|
'response_type' => 'code' |
|
50
|
6 |
|
]; |
|
51
|
|
|
|
|
52
|
9 |
|
if (empty($options['scope'])) { |
|
53
|
6 |
|
$options['scope'] = 'snsapi_login'; |
|
54
|
4 |
|
} |
|
55
|
|
|
|
|
56
|
9 |
|
if (is_array($options['scope'])) { |
|
57
|
3 |
|
$separator = $this->getScopeSeparator(); |
|
58
|
3 |
|
$options['scope'] = implode($separator, $options['scope']); |
|
59
|
2 |
|
} |
|
60
|
|
|
|
|
61
|
9 |
|
if (empty($options['state'])) { |
|
62
|
9 |
|
$options['state'] = $this->getRandomState().'#wechat_redirect'; |
|
63
|
6 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
// Store the state as it may need to be accessed later on. |
|
66
|
9 |
|
$this->state = $options['state']; |
|
67
|
|
|
|
|
68
|
9 |
|
return $options; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Returns the base URL for requesting an access token. |
|
73
|
|
|
* |
|
74
|
|
|
* @param array $params |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
12 |
|
public function getBaseAccessTokenUrl(array $params) |
|
78
|
|
|
{ |
|
79
|
12 |
|
return 'https://api.weixin.qq.com/sns/oauth2/access_token'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Requests an access token using a specified grant and option set. |
|
84
|
|
|
* |
|
85
|
|
|
* @param mixed $grant |
|
86
|
|
|
* @param array $options |
|
87
|
|
|
* @return AccessToken |
|
88
|
|
|
*/ |
|
89
|
9 |
|
public function getAccessToken($grant, array $options = []) |
|
90
|
|
|
{ |
|
91
|
9 |
|
$grant = $this->verifyGrant($grant); |
|
92
|
|
|
$params = [ |
|
93
|
9 |
|
'appid' => $this->appid, |
|
94
|
9 |
|
'secret' => $this->secret |
|
95
|
6 |
|
]; |
|
96
|
|
|
|
|
97
|
9 |
|
$params = $grant->prepareRequestParameters($params, $options); |
|
98
|
9 |
|
$request = $this->getAccessTokenRequest($params); |
|
99
|
9 |
|
$response = $this->getParsedResponse($request); |
|
100
|
9 |
|
$prepared = $this->prepareAccessTokenResponse($response); |
|
|
|
|
|
|
101
|
9 |
|
$token = $this->createAccessToken($prepared, $grant); |
|
102
|
|
|
|
|
103
|
9 |
|
return $token; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Creates an access token from a response. |
|
108
|
|
|
* |
|
109
|
|
|
* The grant that was used to fetch the response can be used to provide |
|
110
|
|
|
* additional context. |
|
111
|
|
|
* |
|
112
|
|
|
* @param array $response |
|
113
|
|
|
* @param AbstractGrant $grant |
|
114
|
|
|
* @return AccessToken |
|
115
|
|
|
*/ |
|
116
|
9 |
|
protected function createAccessToken(array $response, AbstractGrant $grant) |
|
117
|
|
|
{ |
|
118
|
9 |
|
return new AccessToken($response); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Returns the URL for requesting the resource owner's details. |
|
123
|
|
|
* |
|
124
|
|
|
* @param AccessToken $token |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
6 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
|
128
|
|
|
{ |
|
129
|
|
|
return 'https://api.weixin.qq.com/sns/userinfo?access_token='. |
|
130
|
6 |
|
$token->getToken().'&openid='.$token->getValues()['openid']; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Returns the default scopes used by this provider. |
|
135
|
|
|
* |
|
136
|
|
|
* This should only be the scopes that are required to request the details |
|
137
|
|
|
* of the resource owner, rather than all the available scopes. |
|
138
|
|
|
* |
|
139
|
|
|
* @return array |
|
140
|
|
|
*/ |
|
141
|
|
|
protected function getDefaultScopes() |
|
142
|
|
|
{ |
|
143
|
|
|
return ['snsapi_userinfo']; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Checks a provider response for errors. |
|
148
|
|
|
* |
|
149
|
|
|
* @throws IdentityProviderException |
|
150
|
|
|
* @param ResponseInterface $response |
|
151
|
|
|
* @param array|string|\Psr\Http\Message\ResponseInterface $data Parsed response data |
|
152
|
|
|
* @return void |
|
153
|
|
|
*/ |
|
154
|
9 |
|
protected function checkResponse(ResponseInterface $response, $data) |
|
155
|
|
|
{ |
|
156
|
9 |
|
$errcode = $this->getValueByKey($data, 'errcode'); |
|
157
|
9 |
|
$errmsg = $this->getValueByKey($data, 'errmsg'); |
|
158
|
|
|
|
|
159
|
9 |
|
if ($errcode || $errmsg) { |
|
160
|
3 |
|
throw new IdentityProviderException($errmsg, $errcode, $response); |
|
|
|
|
|
|
161
|
|
|
}; |
|
162
|
9 |
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Generates a resource owner object from a successful resource owner |
|
166
|
|
|
* details request. |
|
167
|
|
|
* |
|
168
|
|
|
* @param array $response |
|
169
|
|
|
* @param AccessToken $token |
|
170
|
|
|
* @return ResourceOwnerInterface |
|
171
|
|
|
*/ |
|
172
|
6 |
|
protected function createResourceOwner(array $response, AccessToken $token) |
|
173
|
|
|
{ |
|
174
|
6 |
|
return new WebResourceOwner($response); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.