|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Github https://github.com/settings/developers |
|
4
|
|
|
* api接口文档 |
|
5
|
|
|
* https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/ |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace tinymeng\OAuth2\Gateways; |
|
8
|
|
|
|
|
9
|
|
|
use tinymeng\OAuth2\Connector\Gateway; |
|
10
|
|
|
use tinymeng\OAuth2\Exception\OAuthException; |
|
11
|
|
|
use tinymeng\OAuth2\Helper\ConstCode; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Github |
|
15
|
|
|
* @package tinymeng\OAuth2\Gateways |
|
16
|
|
|
* @Author: TinyMeng <[email protected]> |
|
17
|
|
|
* @Created: 2018/11/9 |
|
18
|
|
|
*/ |
|
19
|
|
|
class Github extends Gateway |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
const API_BASE = 'https://api.github.com/'; |
|
23
|
|
|
protected $AuthorizeURL = 'https://github.com/login/oauth/authorize'; |
|
24
|
|
|
protected $AccessTokenURL = 'https://github.com/login/oauth/access_token'; |
|
25
|
|
|
protected $UserInfoURL = 'user'; |
|
26
|
|
|
protected $headers = [ |
|
27
|
|
|
'Accept: application/json' |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param $config |
|
32
|
|
|
* @throws OAuthException |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct($config) |
|
35
|
|
|
{ |
|
36
|
|
|
parent::__construct($config); |
|
37
|
|
|
$this->UserInfoURL = static::API_BASE.$this->UserInfoURL; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Description: 得到跳转地址 |
|
42
|
|
|
* @author: JiaMeng <[email protected]> |
|
43
|
|
|
* Updater: |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getRedirectUrl() |
|
47
|
|
|
{ |
|
48
|
|
|
//存储state |
|
49
|
|
|
$this->saveState(); |
|
50
|
|
|
//登录参数 |
|
51
|
|
|
$this->switchAccessTokenURL(); |
|
52
|
|
|
$params = [ |
|
53
|
|
|
'client_id' => $this->config['app_id'], |
|
54
|
|
|
'redirect_uri' => $this->config['callback'], |
|
55
|
|
|
'scope' => $this->config['scope'], |
|
56
|
|
|
'state' => $this->config['state'], |
|
57
|
|
|
'display' => $this->display, |
|
58
|
|
|
'allow_signup' => true,//是否会在OAuth流程中为未经身份验证的用户提供注册GitHub的选项。默认是true。false在策略禁止注册的情况下使用。 |
|
59
|
|
|
]; |
|
60
|
|
|
return $this->AuthorizeURL . '?' . http_build_query($params); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Description: 获取当前授权用户的openid标识 |
|
65
|
|
|
* @author: JiaMeng <[email protected]> |
|
66
|
|
|
* Updater: |
|
67
|
|
|
* @return mixed |
|
68
|
|
|
* @throws OAuthException |
|
69
|
|
|
*/ |
|
70
|
|
|
public function openid() |
|
71
|
|
|
{ |
|
72
|
|
|
$this->getToken(); |
|
73
|
|
|
|
|
74
|
|
|
if (isset($this->token['openid'])) { |
|
75
|
|
|
return $this->token['openid']; |
|
76
|
|
|
} else { |
|
77
|
|
|
throw new OAuthException('没有获取到新浪微博用户ID!'); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Description: 获取格式化后的用户信息 |
|
83
|
|
|
* @author: JiaMeng <[email protected]> |
|
84
|
|
|
* Updater: |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
|
|
public function userInfo() |
|
88
|
|
|
{ |
|
89
|
|
|
$result = $this->getUserInfo(); |
|
90
|
|
|
|
|
91
|
|
|
$userInfo = [ |
|
92
|
|
|
'open_id' => $result['id'], |
|
93
|
|
|
'access_token' => $this->token['access_token'], |
|
94
|
|
|
'union_id' => $result['id'], |
|
95
|
|
|
'channel' => ConstCode::TYPE_GITHUB, |
|
96
|
|
|
'nickname' => $result['name'], |
|
97
|
|
|
'username' => $result['login'], |
|
98
|
|
|
'avatar' => $result['avatar_url'], |
|
99
|
|
|
'email' => $result['email'], |
|
100
|
|
|
'sign' => $result['bio'], |
|
101
|
|
|
'gender' => ConstCode::GENDER, |
|
102
|
|
|
'native' => $result, |
|
103
|
|
|
]; |
|
104
|
|
|
return $userInfo; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Description: 获取原始接口返回的用户信息 |
|
109
|
|
|
* @author: JiaMeng <[email protected]> |
|
110
|
|
|
* Updater: |
|
111
|
|
|
* @return mixed |
|
112
|
|
|
* @throws OAuthException |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getUserInfo() |
|
115
|
|
|
{ |
|
116
|
|
|
$this->getToken(); |
|
117
|
|
|
|
|
118
|
|
|
$headers = [ |
|
119
|
|
|
'User-Agent: '.$this->config['application_name'], |
|
120
|
|
|
'UserModel-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36 ', |
|
121
|
|
|
'Authorization: token ' . $this->token['access_token'], |
|
122
|
|
|
'Accept: application/json' |
|
123
|
|
|
]; |
|
124
|
|
|
$response = $this->get($this->UserInfoURL, [], $headers); |
|
125
|
|
|
$data = json_decode($response, true); |
|
126
|
|
|
if (!empty($data['error'])) { |
|
127
|
|
|
throw new OAuthException($data['error']); |
|
128
|
|
|
} |
|
129
|
|
|
return $data; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Description: 根据第三方授权页面样式切换跳转地址 |
|
135
|
|
|
* @author: JiaMeng <[email protected]> |
|
136
|
|
|
* Updater: |
|
137
|
|
|
*/ |
|
138
|
|
|
private function switchAccessTokenURL() |
|
139
|
|
|
{ |
|
140
|
|
|
if ($this->display == 'mobile') {} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Description: 解析access_token方法请求后的返回值 |
|
145
|
|
|
* @author: JiaMeng <[email protected]> |
|
146
|
|
|
* Updater: |
|
147
|
|
|
* @param $token |
|
148
|
|
|
* @return mixed |
|
149
|
|
|
* @throws OAuthException |
|
150
|
|
|
*/ |
|
151
|
|
|
protected function parseToken($token) |
|
152
|
|
|
{ |
|
153
|
|
|
$data = json_decode($token, true); |
|
154
|
|
|
if (isset($data['access_token'])) { |
|
155
|
|
|
return $data; |
|
156
|
|
|
} else { |
|
157
|
|
|
throw new OAuthException("获取GitHub ACCESS_TOKEN出错:{$data['error']}"); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|