1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* 阿里云 |
4
|
|
|
* api接口文档 |
5
|
|
|
* https://help.aliyun.com/zh/ram/user-guide/overview-of-oauth-applications |
6
|
|
|
*/ |
7
|
|
|
namespace tinymeng\OAuth2\Gateways; |
8
|
|
|
use tinymeng\OAuth2\Connector\Gateway; |
9
|
|
|
use tinymeng\OAuth2\Helper\ConstCode; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Aliyun |
13
|
|
|
* @package tinymeng\OAuth2\Gateways |
14
|
|
|
* @Author: TinyMeng <[email protected]> |
15
|
|
|
* @Created: 2023/07/09 |
16
|
|
|
*/ |
17
|
|
|
class Aliyun extends Gateway |
18
|
|
|
{ |
19
|
|
|
const API_BASE = 'https://signin.aliyun.com/'; |
20
|
|
|
protected $AuthorizeURL = 'https://signin.aliyun.com/oauth2/v1/auth'; |
21
|
|
|
protected $AccessTokenURL = 'https://oauth.aliyun.com/v1/token'; |
22
|
|
|
protected $UserInfoURL = 'https://oauth.aliyun.com/v1/userinfo'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Description: 得到跳转地址 |
26
|
|
|
* @author: JiaMeng <[email protected]> |
27
|
|
|
* Updater: |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
public function getRedirectUrl() |
31
|
|
|
{ |
32
|
|
|
//存储state |
33
|
|
|
$this->saveState(); |
34
|
|
|
//登录参数 |
35
|
|
|
$params = [ |
36
|
|
|
'response_type' => $this->config['response_type'], |
37
|
|
|
'client_id' => $this->config['app_id'], |
38
|
|
|
'redirect_uri' => $this->config['callback'], |
39
|
|
|
'state' => $this->config['state'], |
40
|
|
|
'scope' => $this->config['scope'], |
41
|
|
|
]; |
42
|
|
|
return $this->AuthorizeURL . '?' . http_build_query($params); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Description: 获取格式化后的用户信息 |
47
|
|
|
* @return array |
48
|
|
|
* @throws \Exception |
49
|
|
|
* @author: JiaMeng <[email protected]> |
50
|
|
|
* Updater: |
51
|
|
|
*/ |
52
|
|
|
public function userInfo() |
53
|
|
|
{ |
54
|
|
|
$result = $this->getUserInfo(); |
55
|
|
|
$userInfo = [ |
56
|
|
|
'open_id' => isset($result['uid']) ? $result['uid'] : '', |
57
|
|
|
'union_id'=> isset($result['aid']) ? $result['aid'] : '', |
58
|
|
|
'channel' => ConstCode::TYPE_ALIYUN, |
59
|
|
|
'nickname'=> $result['login_name'], |
60
|
|
|
'gender' => ConstCode::GENDER, |
61
|
|
|
'avatar' => '', |
62
|
|
|
'birthday'=> '', |
63
|
|
|
'access_token'=> $this->token['access_token'] ?? '', |
64
|
|
|
'native'=> $result, |
65
|
|
|
]; |
66
|
|
|
return $userInfo; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Description: 获取原始接口返回的用户信息 |
71
|
|
|
* @return array |
72
|
|
|
* @throws \Exception |
73
|
|
|
* @author: JiaMeng <[email protected]> |
74
|
|
|
* Updater: |
75
|
|
|
*/ |
76
|
|
|
public function getUserInfo() |
77
|
|
|
{ |
78
|
|
|
/** 获取用户信息 */ |
79
|
|
|
$this->openid(); |
80
|
|
|
|
81
|
|
|
$headers = ['Authorization: Bearer '.$this->token['access_token']]; |
82
|
|
|
$data = $this->get($this->UserInfoURL, [],$headers); |
83
|
|
|
return json_decode($data, true); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Description: 获取当前授权用户的openid标识 |
88
|
|
|
* @author: JiaMeng <[email protected]> |
89
|
|
|
* Updater: |
90
|
|
|
* @return mixed |
91
|
|
|
* @throws \Exception |
92
|
|
|
*/ |
93
|
|
|
public function openid() |
94
|
|
|
{ |
95
|
|
|
$this->getToken(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Description: 获取AccessToken |
101
|
|
|
* @author: JiaMeng <[email protected]> |
102
|
|
|
* Updater: |
103
|
|
|
*/ |
104
|
|
|
protected function getToken(){ |
105
|
|
|
if (empty($this->token)) { |
106
|
|
|
/** 验证state参数 */ |
107
|
|
|
$this->CheckState(); |
108
|
|
|
|
109
|
|
|
/** 获取参数 */ |
110
|
|
|
$params = $this->accessTokenParams(); |
111
|
|
|
|
112
|
|
|
/** 获取access_token */ |
113
|
|
|
$this->AccessTokenURL = $this->AccessTokenURL . '?' . http_build_query($params); |
114
|
|
|
$token = $this->post($this->AccessTokenURL); |
115
|
|
|
/** 解析token值(子类实现此方法) */ |
116
|
|
|
$this->token = $this->parseToken($token); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Description: 解析access_token方法请求后的返回值 |
122
|
|
|
* @author: JiaMeng <[email protected]> |
123
|
|
|
* Updater: |
124
|
|
|
* @param $token |
125
|
|
|
* @return mixed |
126
|
|
|
* @throws \Exception |
127
|
|
|
*/ |
128
|
|
|
protected function parseToken($token) |
129
|
|
|
{ |
130
|
|
|
$data = json_decode($token, true); |
131
|
|
|
if (isset($data['access_token'])) { |
132
|
|
|
return $data; |
133
|
|
|
} else { |
134
|
|
|
throw new \Exception("获取Aliyun ACCESS_TOKEN出错:{$data['error']}"); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|