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\Tool\ArrayAccessorTrait; |
11
|
|
|
use League\OAuth2\Client\Tool\RequiredParameterTrait; |
12
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
13
|
|
|
use Oakhope\OAuth2\Client\Grant\MiniProgram\AuthorizationCode; |
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
15
|
|
|
|
16
|
|
|
class MiniProgramProvider extends AbstractProvider |
17
|
|
|
{ |
18
|
|
|
use ArrayAccessorTrait; |
19
|
|
|
use RequiredParameterTrait; |
20
|
|
|
|
21
|
|
|
protected $appid; |
22
|
|
|
protected $secret; |
23
|
|
|
protected $jscode; |
24
|
|
|
protected $responseUserInfo; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructs an OAuth 2.0 service provider. |
28
|
|
|
* |
29
|
|
|
* @param array $options An array of options to set on this provider. |
30
|
|
|
* Options include `clientId`, `clientSecret`, `redirectUri`, and `state`. |
31
|
|
|
* Individual providers may introduce more options, as needed. |
32
|
|
|
* @param array $collaborators An array of collaborators that may be used to |
33
|
|
|
* override this provider's default behavior. Collaborators include |
34
|
|
|
* `grantFactory`, `requestFactory`, and `httpClient`. |
35
|
|
|
* Individual providers may introduce more collaborators, as needed. |
36
|
|
|
*/ |
37
|
18 |
|
public function __construct(array $options = [], array $collaborators = []) |
38
|
|
|
{ |
39
|
18 |
|
$this->checkRequiredParameters( |
40
|
|
|
[ |
41
|
18 |
|
'appid', |
42
|
12 |
|
'secret', |
43
|
12 |
|
], |
44
|
6 |
|
$options |
45
|
12 |
|
); |
46
|
|
|
|
47
|
18 |
|
$options['access_token'] = 'js_code'; |
48
|
|
|
|
49
|
18 |
|
parent::__construct($options, $collaborators); |
50
|
18 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns the base URL for authorizing a client. |
54
|
|
|
* |
55
|
|
|
* Eg. https://oauth.service.com/authorize |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
3 |
|
public function getBaseAuthorizationUrl() |
60
|
|
|
{ |
61
|
3 |
|
throw new \LogicException('use wx.login(OBJECT) to get js_code'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns the base URL for requesting an access token. |
66
|
|
|
* |
67
|
|
|
* Eg. https://oauth.service.com/token |
68
|
|
|
* |
69
|
|
|
* @param array $params |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
12 |
|
public function getBaseAccessTokenUrl(array $params) |
73
|
|
|
{ |
74
|
12 |
|
return 'https://api.weixin.qq.com/sns/jscode2session'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Requests an access token using a specified grant and option set. |
79
|
|
|
* |
80
|
|
|
* @param string $jsCode |
81
|
|
|
* @param array $options |
82
|
|
|
* @return AccessToken |
83
|
|
|
*/ |
84
|
9 |
|
public function getAccessToken($jsCode, array $options = []) |
85
|
|
|
{ |
86
|
9 |
|
$this->jscode = $jsCode; |
87
|
9 |
|
$grant = new AuthorizationCode(); |
88
|
9 |
|
$grant = $this->verifyGrant($grant); |
89
|
|
|
$params = [ |
90
|
9 |
|
'appid' => $this->appid, |
91
|
9 |
|
'secret' => $this->secret, |
92
|
9 |
|
'js_code' => $jsCode, |
93
|
6 |
|
]; |
94
|
|
|
|
95
|
9 |
|
$params = $grant->prepareRequestParameters($params, $options); |
96
|
9 |
|
$request = $this->getAccessTokenRequest($params); |
97
|
9 |
|
$response = $this->getParsedResponse($request); |
98
|
9 |
|
$prepared = $this->prepareAccessTokenResponse($response); |
|
|
|
|
99
|
9 |
|
$token = $this->createAccessToken($prepared, $grant); |
100
|
|
|
|
101
|
9 |
|
return $token; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Creates an access token from a response. |
106
|
|
|
* |
107
|
|
|
* The grant that was used to fetch the response can be used to provide |
108
|
|
|
* additional context. |
109
|
|
|
* |
110
|
|
|
* @param array $response |
111
|
|
|
* @param AbstractGrant $grant |
112
|
|
|
* @return AccessToken |
113
|
|
|
*/ |
114
|
11 |
|
protected function createAccessToken(array $response, AbstractGrant $grant) |
115
|
|
|
{ |
116
|
11 |
|
return new \Oakhope\OAuth2\Client\Token\MiniProgram\AccessToken( |
117
|
3 |
|
$response |
118
|
6 |
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns the URL for requesting the resource owner's details. |
123
|
|
|
* |
124
|
|
|
* @param AccessToken $token |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
3 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
128
|
|
|
{ |
129
|
3 |
|
throw new \LogicException( |
130
|
1 |
|
'use wx.getUserInfo(OBJECT) to get ResourceOwnerDetails' |
131
|
2 |
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns the default scopes used by this provider. |
136
|
|
|
* |
137
|
|
|
* This should only be the scopes that are required to request the details |
138
|
|
|
* of the resource owner, rather than all the available scopes. |
139
|
|
|
* |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
protected function getDefaultScopes() |
143
|
|
|
{ |
144
|
|
|
return []; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Checks a provider response for errors. |
149
|
|
|
* |
150
|
|
|
* @throws IdentityProviderException |
151
|
|
|
* @param ResponseInterface $response |
152
|
|
|
* @param array|string $data Parsed response data |
153
|
|
|
* @return void |
154
|
|
|
*/ |
155
|
9 |
|
protected function checkResponse(ResponseInterface $response, $data) |
156
|
|
|
{ |
157
|
9 |
|
$errcode = $this->getValueByKey($data, 'errcode'); |
|
|
|
|
158
|
9 |
|
$errmsg = $this->getValueByKey($data, 'errmsg'); |
|
|
|
|
159
|
|
|
|
160
|
9 |
|
if ($errcode || $errmsg) { |
161
|
3 |
|
throw new IdentityProviderException($errmsg, $errcode, $response); |
|
|
|
|
162
|
|
|
}; |
163
|
9 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Generates a resource owner object from a successful resource owner |
167
|
|
|
* details request. |
168
|
|
|
* |
169
|
|
|
* @param array $response |
170
|
|
|
* @param AccessToken $token |
171
|
|
|
* @return ResourceOwnerInterface |
172
|
|
|
*/ |
173
|
3 |
|
public function createResourceOwner(array $response, AccessToken $token) |
174
|
|
|
{ |
175
|
3 |
|
return new MiniProgramResourceOwner($response, $token, $this->appid); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Requests and returns the resource owner of given access token. |
180
|
|
|
* |
181
|
|
|
* @param AccessToken $token |
182
|
|
|
* @return ResourceOwnerInterface |
183
|
|
|
*/ |
184
|
3 |
|
public function getResourceOwner(AccessToken $token) |
185
|
|
|
{ |
186
|
3 |
|
if (null == $this->responseUserInfo) { |
187
|
|
|
throw new \InvalidArgumentException( |
188
|
|
|
"setResponseUserInfo by wx.getUserInfo(OBJECT)'s response data first" |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
|
192
|
3 |
|
return $this->createResourceOwner($this->responseUserInfo, $token); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* set by wx.getUserInfo(OBJECT)'s response data |
197
|
|
|
* |
198
|
|
|
* @param array $response |
199
|
|
|
*/ |
200
|
3 |
|
public function setResponseUserInfo($response) |
201
|
|
|
{ |
202
|
3 |
|
$this->responseUserInfo = $response; |
203
|
3 |
|
} |
204
|
|
|
} |
205
|
|
|
|
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.