1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the overtrue/wechat. |
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
|
|
|
/** |
13
|
|
|
* Authorization.php. |
14
|
|
|
* |
15
|
|
|
* Part of Overtrue\WeChat. |
16
|
|
|
* |
17
|
|
|
* For the full copyright and license information, please view the LICENSE |
18
|
|
|
* file that was distributed with this source code. |
19
|
|
|
* |
20
|
|
|
* @author lixiao <[email protected]> |
21
|
|
|
* @author mingyoung <[email protected]> |
22
|
|
|
* @copyright 2016 |
23
|
|
|
* |
24
|
|
|
* @see https://github.com/overtrue |
25
|
|
|
* @see http://overtrue.me |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
namespace EasyWeChat\OpenPlatform; |
29
|
|
|
|
30
|
|
|
use Doctrine\Common\Cache\Cache; |
31
|
|
|
use EasyWeChat\Core\Exception; |
32
|
|
|
use EasyWeChat\OpenPlatform\Api\BaseApi; |
33
|
|
|
use EasyWeChat\Support\Collection; |
34
|
|
|
|
35
|
|
|
class Authorization |
36
|
|
|
{ |
37
|
|
|
const CACHE_KEY_ACCESS_TOKEN = 'easywechat.open_platform.authorizer_access_token'; |
38
|
|
|
const CACHE_KEY_REFRESH_TOKEN = 'easywechat.open_platform.authorizer_refresh_token'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Cache. |
42
|
|
|
* |
43
|
|
|
* @var \Doctrine\Common\Cache\Cache |
44
|
|
|
*/ |
45
|
|
|
protected $cache; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Base API. |
49
|
|
|
* |
50
|
|
|
* @var \EasyWeChat\OpenPlatform\Api\BaseApi |
51
|
|
|
*/ |
52
|
|
|
private $api; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Open Platform App Id, aka, Component App Id. |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
private $appId; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Authorizer App Id. |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
private $authorizerAppId; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Auth code. |
70
|
|
|
* |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
private $authCode; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Authorization Constructor. |
77
|
|
|
* |
78
|
|
|
* Users need not concern the details. |
79
|
|
|
* |
80
|
|
|
* @param \EasyWeChat\OpenPlatform\Api\BaseApi $api |
81
|
|
|
* @param string $appId |
82
|
|
|
* @param \Doctrine\Common\Cache\Cache $cache |
83
|
|
|
*/ |
84
|
15 |
|
public function __construct(BaseApi $api, $appId, Cache $cache) |
85
|
|
|
{ |
86
|
15 |
|
$this->api = $api; |
87
|
15 |
|
$this->appId = $appId; |
88
|
15 |
|
$this->cache = $cache; |
89
|
15 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Sets the authorizer app id. |
93
|
|
|
* |
94
|
|
|
* @param string $authorizerAppId |
95
|
|
|
*/ |
96
|
13 |
|
public function setAuthorizerAppId($authorizerAppId) |
97
|
|
|
{ |
98
|
13 |
|
$this->authorizerAppId = $authorizerAppId; |
99
|
13 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Gets the authorizer app id, or throws if not found. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
* |
106
|
|
|
* @throws \EasyWeChat\Core\Exception |
107
|
|
|
*/ |
108
|
11 |
|
public function getAuthorizerAppId() |
109
|
|
|
{ |
110
|
11 |
|
if (!$this->authorizerAppId) { |
111
|
|
|
throw new Exception( |
112
|
|
|
'Authorizer App Id is not present, you may not make the authorization yet.' |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
11 |
|
return $this->authorizerAppId; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Sets the auth code. |
121
|
|
|
* |
122
|
|
|
* @param $code |
123
|
|
|
*/ |
124
|
|
|
public function setAuthCode($code) |
125
|
|
|
{ |
126
|
|
|
$this->authCode = $code; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Gets the auth code. |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
4 |
|
public function getAuthCode() |
135
|
|
|
{ |
136
|
4 |
|
return $this->authCode; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Sets the auth info from the message of the auth event sent by WeChat. |
141
|
|
|
* |
142
|
|
|
* @param \EasyWeChat\Support\Collection $message |
143
|
|
|
*/ |
144
|
|
|
public function setFromAuthMessage(Collection $message) |
145
|
|
|
{ |
146
|
|
|
if ($authorizerAppId = $message->get('AuthorizerAppid')) { |
147
|
|
|
$this->setAuthorizerAppId($authorizerAppId); |
148
|
|
|
} |
149
|
|
|
if ($authorizationCode = $message->get('AuthorizationCode')) { |
150
|
|
|
$this->setAuthCode($authorizationCode); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Handles authorization: calls the API, saves the tokens. |
156
|
|
|
* |
157
|
|
|
* @return Collection |
158
|
|
|
*/ |
159
|
2 |
|
public function handleAuthorization() |
160
|
|
|
{ |
161
|
2 |
|
$info = $this->getAuthorizationInfo(); |
162
|
|
|
|
163
|
2 |
|
$appId = $info['authorization_info']['authorizer_appid']; |
164
|
2 |
|
$this->setAuthorizerAppId($appId); |
165
|
|
|
|
166
|
2 |
|
$this->setAuthorizerAccessToken($info['authorization_info']['authorizer_access_token']); |
167
|
2 |
|
$this->setAuthorizerRefreshToken($info['authorization_info']['authorizer_refresh_token']); |
168
|
|
|
|
169
|
2 |
|
$authorizerInfo = $this->getAuthorizerInfo(); |
170
|
|
|
// Duplicated info. |
171
|
2 |
|
$authorizerInfo->forget('authorization_info'); |
172
|
2 |
|
$info->merge($authorizerInfo->all()); |
173
|
|
|
|
174
|
2 |
|
return $info; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Handles the authorizer access token: calls the API, saves the token. |
179
|
|
|
* |
180
|
|
|
* @return string the authorizer access token |
181
|
|
|
*/ |
182
|
2 |
|
public function handleAuthorizerAccessToken() |
183
|
|
|
{ |
184
|
2 |
|
$data = $this->api->getAuthorizationToken( |
185
|
2 |
|
$this->getAuthorizerAppId(), |
186
|
2 |
|
$this->getAuthorizerRefreshToken() |
187
|
2 |
|
); |
188
|
|
|
|
189
|
2 |
|
$this->setAuthorizerAccessToken($data); |
190
|
|
|
|
191
|
2 |
|
return $data['authorizer_access_token']; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Gets the authorization information. |
196
|
|
|
* Like authorizer app id, access token, refresh token, function scope, etc. |
197
|
|
|
* |
198
|
|
|
* @return \EasyWeChat\Support\Collection |
199
|
|
|
*/ |
200
|
4 |
|
public function getAuthorizationInfo() |
201
|
|
|
{ |
202
|
4 |
|
return $this->api->getAuthorizationInfo($this->getAuthCode()); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Gets the authorizer information. |
207
|
|
|
* Like authorizer name, logo, business, etc. |
208
|
|
|
* |
209
|
|
|
* @return \EasyWeChat\Support\Collection |
210
|
|
|
*/ |
211
|
4 |
|
public function getAuthorizerInfo() |
212
|
|
|
{ |
213
|
4 |
|
return $this->api->getAuthorizerInfo($this->getAuthorizerAppId()); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Saves the authorizer access token in cache. |
218
|
|
|
* |
219
|
|
|
* @param string $token |
220
|
|
|
* |
221
|
|
|
* @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise |
222
|
|
|
*/ |
223
|
6 |
|
public function setAuthorizerAccessToken($token, $expires = 7200) |
224
|
|
|
{ |
225
|
6 |
|
return $this->cache->save($this->getAuthorizerAccessTokenKey(), $token, $expires - 1500); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Gets the authorizer access token. |
230
|
|
|
* |
231
|
|
|
* @return string |
232
|
|
|
*/ |
233
|
4 |
|
public function getAuthorizerAccessToken() |
234
|
|
|
{ |
235
|
4 |
|
return $this->cache->fetch($this->getAuthorizerAccessTokenKey()); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Saves the authorizer refresh token in cache. |
240
|
|
|
* |
241
|
|
|
* @param string $refreshToken |
242
|
|
|
* |
243
|
|
|
* @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise |
244
|
|
|
*/ |
245
|
7 |
|
public function setAuthorizerRefreshToken($refreshToken) |
246
|
|
|
{ |
247
|
7 |
|
return $this->cache->save($this->getAuthorizerRefreshTokenKey(), $refreshToken); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Gets the authorizer refresh token. |
252
|
|
|
* |
253
|
|
|
* @return string |
254
|
|
|
* |
255
|
|
|
* @throws Exception when refresh token is not present |
256
|
|
|
*/ |
257
|
6 |
|
public function getAuthorizerRefreshToken() |
258
|
|
|
{ |
259
|
6 |
|
if ($token = $this->cache->fetch($this->getAuthorizerRefreshTokenKey())) { |
260
|
6 |
|
return $token; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
throw new Exception( |
264
|
|
|
'Authorizer Refresh Token is not present, you may not make the authorization yet.' |
265
|
|
|
); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Removes the authorizer access token from cache. |
270
|
|
|
* |
271
|
|
|
* @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise. |
272
|
|
|
* Deleting a non-existing entry is considered successful |
273
|
|
|
*/ |
274
|
|
|
public function removeAuthorizerAccessToken() |
275
|
|
|
{ |
276
|
|
|
return $this->cache->delete($this->getAuthorizerAccessTokenKey()); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Removes the authorizer refresh token from cache. |
281
|
|
|
* |
282
|
|
|
* @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise. |
283
|
|
|
* Deleting a non-existing entry is considered successful |
284
|
|
|
*/ |
285
|
|
|
public function removeAuthorizerRefreshToken() |
286
|
|
|
{ |
287
|
|
|
return $this->cache->delete($this->getAuthorizerRefreshTokenKey()); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Gets the authorizer access token cache key. |
292
|
|
|
* |
293
|
|
|
* @return string |
294
|
|
|
*/ |
295
|
6 |
|
public function getAuthorizerAccessTokenKey() |
296
|
|
|
{ |
297
|
6 |
|
return self::CACHE_KEY_ACCESS_TOKEN.$this->appId.$this->getAuthorizerAppId(); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Gets the authorizer refresh token cache key. |
302
|
|
|
* |
303
|
|
|
* @return string |
304
|
|
|
*/ |
305
|
7 |
|
public function getAuthorizerRefreshTokenKey() |
306
|
|
|
{ |
307
|
7 |
|
return self::CACHE_KEY_REFRESH_TOKEN.$this->appId.$this->getAuthorizerAppId(); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|