1
|
|
|
<?php |
2
|
|
|
namespace tinymeng\OAuth2\Connector; |
3
|
|
|
|
4
|
|
|
use tinymeng\OAuth2\Exception\OAuthException; |
5
|
|
|
use tinymeng\OAuth2\Helper\ConstCode; |
6
|
|
|
use tinymeng\OAuth2\Helper\Str; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* 所有第三方登录必须继承的抽象类 |
10
|
|
|
*/ |
11
|
|
|
abstract class Gateway implements GatewayInterface |
12
|
|
|
{ |
13
|
|
|
use GatewayTrait; |
14
|
|
|
/** |
15
|
|
|
* 授权地址 |
16
|
|
|
* @var |
17
|
|
|
*/ |
18
|
|
|
protected $AuthorizeURL; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* 获取token地址 |
22
|
|
|
* @var |
23
|
|
|
*/ |
24
|
|
|
protected $AccessTokenURL; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* 获取token地址 |
28
|
|
|
* @var |
29
|
|
|
*/ |
30
|
|
|
protected $UserInfoURL; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* 配置参数 |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $config; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* AppId |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $app_id; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* AppSecret |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $app_secret; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* 接口权限值 |
52
|
|
|
* @var |
53
|
|
|
*/ |
54
|
|
|
protected $scope; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* 回调地址 |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
protected $callback; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* 当前时间戳 |
64
|
|
|
* @var int |
65
|
|
|
*/ |
66
|
|
|
protected $timestamp; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* 默认第三方授权页面样式 |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
protected $display = 'default'; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* 登录类型:app applets |
76
|
|
|
* @var bool |
77
|
|
|
*/ |
78
|
|
|
protected $type; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* 第三方Token信息 |
82
|
|
|
* @var array |
83
|
|
|
*/ |
84
|
|
|
protected $token = null; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* 是否验证回跳地址中的state参数 |
88
|
|
|
* @var boolean |
89
|
|
|
*/ |
90
|
|
|
protected $checkState = false; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* 第三方返回的userInfo |
94
|
|
|
* @var array |
95
|
|
|
*/ |
96
|
|
|
protected $userInfo = []; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* 格式化的userInfo |
100
|
|
|
* @var array |
101
|
|
|
*/ |
102
|
|
|
protected $formatUserInfo = []; |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Gateway constructor. |
107
|
|
|
* @param $config |
108
|
|
|
* @throws OAuthException |
109
|
|
|
*/ |
110
|
|
|
public function __construct($config) |
111
|
|
|
{ |
112
|
|
|
if (!$config) { |
113
|
|
|
throw new OAuthException('传入的配置不能为空'); |
114
|
|
|
} |
115
|
|
|
if(isset($_GET['referer']) && $config['callback']){ |
116
|
|
|
$config['callback'] .= ((strpos($config['callback'], '?') !== false) ? '&' : '?').'referer='.$_GET['referer']; |
117
|
|
|
} |
118
|
|
|
//默认参数 |
119
|
|
|
$_config = [ |
120
|
|
|
'app_id' => '', |
121
|
|
|
'app_secret' => '', |
122
|
|
|
'callback' => '', |
123
|
|
|
'response_type' => 'code', |
124
|
|
|
'grant_type' => 'authorization_code', |
125
|
|
|
'proxy' => '', |
126
|
|
|
'state' => '', |
127
|
|
|
'type' => '', |
128
|
|
|
'is_sandbox' => false,//是否是沙箱环境 |
129
|
|
|
]; |
130
|
|
|
$this->config = array_merge($_config, $config); |
131
|
|
|
foreach($this->config as $key=>$val){ |
132
|
|
|
if(property_exists($this,$key)) $this->$key=$val; |
133
|
|
|
} |
134
|
|
|
$this->timestamp = time(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Description: 设置授权页面样式 |
139
|
|
|
* @author: JiaMeng <[email protected]> |
140
|
|
|
* Updater: |
141
|
|
|
* @param $display |
142
|
|
|
* @return $this |
143
|
|
|
*/ |
144
|
|
|
public function setDisplay($display) |
145
|
|
|
{ |
146
|
|
|
$this->display = $display; |
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Description: 设置是否是App |
152
|
|
|
* @author: JiaMeng <[email protected]> |
153
|
|
|
* Updater: |
154
|
|
|
* @return $this |
155
|
|
|
*/ |
156
|
|
|
public function setType($type) |
157
|
|
|
{ |
158
|
|
|
$this->type = $type; |
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Description: 强制验证回跳地址中的state参数 |
164
|
|
|
* @author: JiaMeng <[email protected]> |
165
|
|
|
* Updater: |
166
|
|
|
* @return $this |
167
|
|
|
*/ |
168
|
|
|
public function mustCheckState(){ |
169
|
|
|
$this->checkState = true; |
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* 获取配置信息 |
175
|
|
|
* @Author: TinyMeng <[email protected]> |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
|
|
public function getConfig(){ |
179
|
|
|
return $this->config; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* 设置token(App登录时) |
184
|
|
|
* @param $token |
185
|
|
|
* @return $this |
186
|
|
|
*/ |
187
|
|
|
public function setToken($token){ |
188
|
|
|
$this->token = $token; |
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* 存储state |
194
|
|
|
* @Author: TinyMeng <[email protected]> |
195
|
|
|
*/ |
196
|
|
|
public function saveState(){ |
197
|
|
|
if ($this->checkState === true) { |
198
|
|
|
//是否开启session |
199
|
|
|
if (session_status() !== PHP_SESSION_ACTIVE) { |
200
|
|
|
session_start(); |
201
|
|
|
} |
202
|
|
|
if(empty($this->config['state'])){ |
203
|
|
|
$this->config['state'] = Str::random();//生成随机state |
204
|
|
|
} |
205
|
|
|
//存储到session |
206
|
|
|
$_SESSION['tinymeng_oauth_state'] = $this->config['state']; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* 验证state |
212
|
|
|
* @Author: TinyMeng <[email protected]> |
213
|
|
|
* @throws OAuthException |
214
|
|
|
*/ |
215
|
|
|
public function checkState(){ |
216
|
|
|
if ($this->checkState === true) { |
217
|
|
|
if (session_status() !== PHP_SESSION_ACTIVE) { |
218
|
|
|
session_start(); |
219
|
|
|
} |
220
|
|
|
if (!isset($_REQUEST['state']) || !isset($_SESSION['tinymeng_oauth_state']) || $_REQUEST['state'] != $_SESSION['tinymeng_oauth_state']) { |
221
|
|
|
throw new OAuthException('传递的STATE参数不匹配!'); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* 获取授权后的Code |
229
|
|
|
* @author: JiaMeng <[email protected]> |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
|
|
public function getCode(){ |
233
|
|
|
return isset($_REQUEST['code']) ? $_REQUEST['code'] : ''; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Description: 默认获取AccessToken请求参数 |
238
|
|
|
* @author: JiaMeng <[email protected]> |
239
|
|
|
* Updater: |
240
|
|
|
* @return array |
241
|
|
|
*/ |
242
|
|
|
protected function accessTokenParams(){ |
243
|
|
|
$params = [ |
244
|
|
|
'client_id' => $this->config['app_id'], |
245
|
|
|
'client_secret' => $this->config['app_secret'], |
246
|
|
|
'grant_type' => $this->config['grant_type'], |
247
|
|
|
'code' => $this->getCode(), |
248
|
|
|
'redirect_uri' => $this->config['callback'], |
249
|
|
|
]; |
250
|
|
|
return $params; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Description: 获取AccessToken |
255
|
|
|
* @author: JiaMeng <[email protected]> |
256
|
|
|
* Updater: |
257
|
|
|
*/ |
258
|
|
|
protected function getToken(){ |
259
|
|
|
if (empty($this->token)) { |
260
|
|
|
/** 验证state参数 */ |
261
|
|
|
$this->checkState(); |
262
|
|
|
|
263
|
|
|
/** 获取参数 */ |
264
|
|
|
$params = $this->accessTokenParams(); |
265
|
|
|
|
266
|
|
|
/** 获取access_token */ |
267
|
|
|
$token = $this->post($this->AccessTokenURL, $params,$this->getHeaders()); |
268
|
|
|
/** 解析token值(子类实现此方法) */ |
269
|
|
|
$this->token = $this->parseToken($token); |
|
|
|
|
270
|
|
|
}else{ |
271
|
|
|
return $this->token; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* 格式化性别参数 |
277
|
|
|
* M代表男性,F代表女性 |
278
|
|
|
* @param $gender |
279
|
|
|
*/ |
280
|
|
|
public function getGender($gender){ |
281
|
|
|
return strtolower(substr($gender , 0 , 1)) == 'm' ? ConstCode::GENDER_MAN : ConstCode::GENDER_WOMEN; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* 刷新AccessToken续期 |
287
|
|
|
* @param string $refreshToken |
288
|
|
|
* @return bool |
289
|
|
|
*/ |
290
|
|
|
public function refreshToken($refreshToken) |
291
|
|
|
{ |
292
|
|
|
return true; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* 检验授权凭证AccessToken是否有效 |
297
|
|
|
* @param string $accessToken |
298
|
|
|
* @return bool |
299
|
|
|
*/ |
300
|
|
|
public function validateAccessToken($accessToken = null) |
301
|
|
|
{ |
302
|
|
|
return true; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
} |
306
|
|
|
|