1 | <?php |
||
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) |
|
90 | |||
91 | /** |
||
92 | * Sets the authorizer app id. |
||
93 | * |
||
94 | * @param string $authorizerAppId |
||
95 | */ |
||
96 | 13 | public function setAuthorizerAppId($authorizerAppId) |
|
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() |
|
118 | |||
119 | /** |
||
120 | * Sets the auth code. |
||
121 | * |
||
122 | * @param $code |
||
123 | */ |
||
124 | public function setAuthCode($code) |
||
128 | |||
129 | /** |
||
130 | * Gets the auth code. |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | 4 | public function getAuthCode() |
|
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) |
||
153 | |||
154 | /** |
||
155 | * Handles authorization: calls the API, saves the tokens. |
||
156 | * |
||
157 | * @return Collection |
||
158 | */ |
||
159 | 2 | public function handleAuthorization() |
|
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() |
|
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() |
|
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() |
|
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) |
|
227 | |||
228 | /** |
||
229 | * Gets the authorizer access token. |
||
230 | * |
||
231 | * @return string |
||
232 | */ |
||
233 | 4 | public function getAuthorizerAccessToken() |
|
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) |
|
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() |
|
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() |
||
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() |
||
289 | |||
290 | /** |
||
291 | * Gets the authorizer access token cache key. |
||
292 | * |
||
293 | * @return string |
||
294 | */ |
||
295 | 6 | public function getAuthorizerAccessTokenKey() |
|
299 | |||
300 | /** |
||
301 | * Gets the authorizer refresh token cache key. |
||
302 | * |
||
303 | * @return string |
||
304 | */ |
||
305 | 7 | public function getAuthorizerRefreshTokenKey() |
|
309 | } |
||
310 |