1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the light/easemob. |
5
|
|
|
* |
6
|
|
|
* (c) lichunqiang <[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
|
|
|
namespace light\Easemob\Core; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
15
|
|
|
use light\Easemob\Exception\HttpException; |
16
|
|
|
use light\Easemob\Support\Log; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Get the access token. |
20
|
|
|
*/ |
21
|
|
|
class AccessToken |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string client id of app |
25
|
|
|
*/ |
26
|
|
|
protected $clientId; |
27
|
|
|
/** |
28
|
|
|
* @var string client secret of app |
29
|
|
|
*/ |
30
|
|
|
protected $clientSecret; |
31
|
|
|
/** |
32
|
|
|
* @var \Doctrine\Common\Cache\Cache |
33
|
|
|
*/ |
34
|
|
|
protected $cache; |
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $cacheKeyPrefix = 'easemob.access.token'; |
39
|
|
|
/** |
40
|
|
|
* @var Http |
41
|
|
|
*/ |
42
|
|
|
protected $http; |
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $token; |
47
|
|
|
|
48
|
4 |
|
public function __construct( |
49
|
|
|
$clientId, |
50
|
|
|
$clientSecret, |
51
|
|
|
Http $http, |
52
|
|
|
$cache = null |
53
|
|
|
) { |
54
|
4 |
|
$this->clientId = $clientId; |
55
|
4 |
|
$this->clientSecret = $clientSecret; |
56
|
4 |
|
$this->http = $http; |
57
|
4 |
|
$this->cache = $cache; |
58
|
4 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function getClientId() |
64
|
|
|
{ |
65
|
|
|
return $this->clientId; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $clientId |
70
|
|
|
*/ |
71
|
|
|
public function setClientId($clientId) |
72
|
|
|
{ |
73
|
|
|
$this->clientId = $clientId; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function getClientSecret() |
80
|
|
|
{ |
81
|
|
|
return $this->clientSecret; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $clientSecret |
86
|
|
|
*/ |
87
|
|
|
public function setClientSecret($clientSecret) |
88
|
|
|
{ |
89
|
|
|
$this->clientSecret = $clientSecret; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return \Doctrine\Common\Cache\Cache |
94
|
|
|
*/ |
95
|
4 |
|
public function getCache() |
96
|
|
|
{ |
97
|
4 |
|
return $this->cache ?: new FilesystemCache(sys_get_temp_dir()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param \Doctrine\Common\Cache\Cache $cache |
102
|
|
|
*/ |
103
|
|
|
public function setCache($cache) |
104
|
|
|
{ |
105
|
|
|
$this->cache = $cache; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return Http |
110
|
|
|
*/ |
111
|
|
|
public function getHttp() |
112
|
|
|
{ |
113
|
|
|
return $this->http; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param Http $http |
118
|
|
|
*/ |
119
|
|
|
public function setHttp($http) |
120
|
|
|
{ |
121
|
|
|
$this->http = $http; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the access token. |
126
|
|
|
* |
127
|
|
|
* @param bool $forceRefresh |
128
|
|
|
* |
129
|
|
|
* @throws HttpException |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
4 |
|
public function getToken($forceRefresh = false) |
134
|
|
|
{ |
135
|
4 |
|
if (!$this->token) { |
136
|
4 |
|
$this->token = $this->getCache()->fetch($this->cacheKeyPrefix); |
137
|
|
|
|
138
|
4 |
|
if ($forceRefresh || !$this->token) { |
139
|
1 |
|
$token = $this->getTokenFromServer(); |
140
|
1 |
|
Log::debug('Renew the access token', [ |
141
|
1 |
|
'token' => $token, |
142
|
1 |
|
]); |
143
|
|
|
|
144
|
1 |
|
$this->getCache()->save( |
145
|
1 |
|
$this->cacheKeyPrefix, |
146
|
1 |
|
$token['access_token'], |
147
|
1 |
|
$token['expires_in'] - 1500 |
148
|
1 |
|
); |
149
|
|
|
|
150
|
1 |
|
$this->token = $token['access_token']; |
151
|
1 |
|
} |
152
|
4 |
|
} |
153
|
|
|
|
154
|
4 |
|
return $this->token; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Internal fetch token logic. |
159
|
|
|
* |
160
|
|
|
* @throws HttpException |
161
|
|
|
* @throws \light\Easemob\Exception\HttpException |
162
|
|
|
* |
163
|
|
|
* @return array |
164
|
|
|
*/ |
165
|
1 |
|
protected function getTokenFromServer() |
166
|
|
|
{ |
167
|
1 |
|
$this->http->clearMiddlewares(); |
168
|
1 |
|
$response = $this->http->post('token', [ |
169
|
1 |
|
'grant_type' => 'client_credentials', |
170
|
1 |
|
'client_id' => $this->clientId, |
171
|
1 |
|
'client_secret' => $this->clientSecret, |
172
|
1 |
|
]); |
173
|
|
|
|
174
|
1 |
|
Log::debug('Get access token response', ['response' => (string) $response->getBody()]); |
175
|
|
|
|
176
|
1 |
|
$token = $this->http->parseJSON((string) $response->getBody()); |
177
|
|
|
|
178
|
1 |
|
if (!isset($token['access_token'])) { |
179
|
|
|
throw new HttpException('Request AccessToken fail.' . json_encode($token, JSON_UNESCAPED_UNICODE)); |
180
|
|
|
} |
181
|
|
|
|
182
|
1 |
|
return $token; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|