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
|
|
|
* Authorizer.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
|
|
|
|
34
|
|
|
class Authorizer |
35
|
|
|
{ |
36
|
|
|
const CACHE_KEY_ACCESS_TOKEN = 'easywechat.open_platform.authorizer_access_token'; |
37
|
|
|
const CACHE_KEY_REFRESH_TOKEN = 'easywechat.open_platform.authorizer_refresh_token'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Cache. |
41
|
|
|
* |
42
|
|
|
* @var \Doctrine\Common\Cache\Cache |
43
|
|
|
*/ |
44
|
|
|
protected $cache; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Base API. |
48
|
|
|
* |
49
|
|
|
* @var \EasyWeChat\OpenPlatform\Api\BaseApi |
50
|
|
|
*/ |
51
|
|
|
protected $api; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Authorizer AppId. |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $appId; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* OpenPlatform AppId. |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
protected $openPlatformAppId; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Authorizer Constructor. |
69
|
|
|
* |
70
|
|
|
* @param \EasyWeChat\OpenPlatform\Api\BaseApi $api |
71
|
|
|
* @param string $openPlatformAppId OpenPlatform AppId |
72
|
|
|
* @param \Doctrine\Common\Cache\Cache $cache |
73
|
|
|
*/ |
74
|
6 |
|
public function __construct(BaseApi $api, $openPlatformAppId, Cache $cache) |
75
|
|
|
{ |
76
|
6 |
|
$this->api = $api; |
77
|
6 |
|
$this->openPlatformAppId = $openPlatformAppId; |
78
|
6 |
|
$this->cache = $cache; |
79
|
6 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Gets the base api. |
83
|
|
|
* |
84
|
|
|
* @return \EasyWeChat\OpenPlatform\Api\BaseApi |
85
|
|
|
*/ |
86
|
3 |
|
public function getApi() |
87
|
|
|
{ |
88
|
3 |
|
return $this->api; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Sets the authorizer app id. |
93
|
|
|
* |
94
|
|
|
* @param string $appId |
95
|
|
|
* |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
6 |
|
public function setAppId($appId) |
99
|
|
|
{ |
100
|
6 |
|
$this->appId = $appId; |
101
|
|
|
|
102
|
6 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Gets the authorizer app id, or throws if not found. |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
* |
110
|
|
|
* @throws \EasyWeChat\Core\Exception |
111
|
|
|
*/ |
112
|
3 |
|
public function getAppId() |
113
|
|
|
{ |
114
|
3 |
|
if (!$this->appId) { |
115
|
|
|
throw new Exception( |
116
|
|
|
'Authorizer App Id is not present, you may not make the authorizer yet.' |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
3 |
|
return $this->appId; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Saves the authorizer access token in cache. |
125
|
|
|
* |
126
|
|
|
* @param string $token |
127
|
|
|
* @param int $expires |
128
|
|
|
* |
129
|
|
|
* @return $this |
130
|
|
|
*/ |
131
|
1 |
|
public function setAccessToken($token, $expires = 7200) |
132
|
|
|
{ |
133
|
1 |
|
$this->cache->save($this->getAccessTokenCacheKey(), $token, $expires); |
134
|
|
|
|
135
|
1 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Gets the authorizer access token. |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
1 |
|
public function getAccessToken() |
144
|
|
|
{ |
145
|
1 |
|
return $this->cache->fetch($this->getAccessTokenCacheKey()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Saves the authorizer refresh token in cache. |
150
|
|
|
* |
151
|
|
|
* @param string $refreshToken |
152
|
|
|
* |
153
|
|
|
* @return $this |
154
|
|
|
*/ |
155
|
2 |
|
public function setRefreshToken($refreshToken) |
156
|
|
|
{ |
157
|
2 |
|
$this->cache->save($this->getRefreshTokenCacheKey(), $refreshToken); |
158
|
|
|
|
159
|
2 |
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Gets the authorizer refresh token. |
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
* |
167
|
|
|
* @throws \EasyWeChat\Core\Exception when refresh token is not present |
168
|
|
|
*/ |
169
|
1 |
|
public function getRefreshToken() |
170
|
|
|
{ |
171
|
1 |
|
if ($token = $this->cache->fetch($this->getRefreshTokenCacheKey())) { |
172
|
1 |
|
return $token; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
throw new Exception( |
176
|
|
|
'Authorizer Refresh Token is not present, you may not make the authorizer yet.' |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Gets the authorizer access token cache key. |
182
|
|
|
* |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
1 |
|
public function getAccessTokenCacheKey() |
186
|
|
|
{ |
187
|
1 |
|
return self::CACHE_KEY_ACCESS_TOKEN.$this->appId.$this->getAppId(); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Gets the authorizer refresh token cache key. |
192
|
|
|
* |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
2 |
|
public function getRefreshTokenCacheKey() |
196
|
|
|
{ |
197
|
2 |
|
return self::CACHE_KEY_REFRESH_TOKEN.$this->appId.$this->getAppId(); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|