1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Gitlab |
4
|
|
|
* api接口文档 |
5
|
|
|
*/ |
6
|
|
|
namespace tinymeng\OAuth2\Gateways; |
7
|
|
|
use tinymeng\OAuth2\Connector\Gateway; |
8
|
|
|
use tinymeng\OAuth2\Exception\OAuthException; |
9
|
|
|
use tinymeng\OAuth2\Helper\ConstCode; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Gitlab |
13
|
|
|
* @package tinymeng\OAuth2\Gateways |
14
|
|
|
* @Author: TinyMeng <[email protected]> |
15
|
|
|
* @Created: 2023/07/09 |
16
|
|
|
*/ |
17
|
|
|
class Gitlab extends Gateway |
18
|
|
|
{ |
19
|
|
|
const API_BASE = 'https://gitlab.com/'; |
20
|
|
|
protected $AuthorizeURL = 'https://gitlab.com/oauth/authorize'; |
21
|
|
|
protected $AccessTokenURL = 'https://gitlab.com/oauth/token'; |
22
|
|
|
protected $UserInfoURL = 'api/v4/user'; |
23
|
|
|
|
24
|
|
|
public function getRedirectUrl() |
25
|
|
|
{ |
26
|
|
|
$this->saveState(); |
27
|
|
|
$params = [ |
28
|
|
|
'client_id' => $this->config['app_id'], |
29
|
|
|
'redirect_uri' => $this->config['callback'], |
30
|
|
|
'response_type' => $this->config['response_type'], |
31
|
|
|
'state' => $this->config['state'], |
32
|
|
|
'scope' => $this->config['scope'] ?: 'read_user', |
33
|
|
|
]; |
34
|
|
|
return $this->AuthorizeURL . '?' . http_build_query($params); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function userInfo() |
38
|
|
|
{ |
39
|
|
|
$result = $this->getUserInfo(); |
40
|
|
|
return [ |
41
|
|
|
'open_id' => $result['id'] ?? '', |
42
|
|
|
'union_id' => $result['id'] ?? '', |
43
|
|
|
'channel' => ConstCode::TYPE_GITLAB, |
44
|
|
|
'nickname' => $result['username'] ?? '', |
45
|
|
|
'gender' => ConstCode::GENDER, |
46
|
|
|
'avatar' => $result['avatar_url'] ?? '', |
47
|
|
|
'email' => $result['email'] ?? '', |
48
|
|
|
'access_token' => $this->token['access_token'] ?? '', |
49
|
|
|
'native' => $result |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getUserInfo() |
54
|
|
|
{ |
55
|
|
|
$this->openid(); |
56
|
|
|
$headers = ['Authorization: Bearer ' . $this->token['access_token']]; |
57
|
|
|
$data = $this->get(static::API_BASE . $this->UserInfoURL, [], $headers); |
58
|
|
|
$data = json_decode($data, true); |
59
|
|
|
|
60
|
|
|
if(!isset($data['id'])) { |
61
|
|
|
throw new OAuthException("获取GitLab用户信息失败:" . ($data['error_description'] ?? '未知错误')); |
62
|
|
|
} |
63
|
|
|
return $data; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Description: 获取当前授权用户的openid标识 |
68
|
|
|
* @return string |
69
|
|
|
* @throws OAuthException |
70
|
|
|
*/ |
71
|
|
|
public function openid() |
72
|
|
|
{ |
73
|
|
|
$this->getToken(); |
74
|
|
|
return $this->token['user_id'] ?? ''; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Description: 获取AccessToken |
79
|
|
|
* @throws OAuthException |
80
|
|
|
*/ |
81
|
|
|
protected function getToken() |
82
|
|
|
{ |
83
|
|
|
if (empty($this->token)) { |
84
|
|
|
$this->checkState(); |
85
|
|
|
$params = [ |
86
|
|
|
'grant_type' => $this->config['grant_type'], |
87
|
|
|
'client_id' => $this->config['app_id'], |
88
|
|
|
'client_secret' => $this->config['app_secret'], |
89
|
|
|
'code' => isset($_REQUEST['code']) ? $_REQUEST['code'] : '', |
90
|
|
|
'redirect_uri' => $this->config['callback'], |
91
|
|
|
]; |
92
|
|
|
$response = $this->post($this->AccessTokenURL, $params); |
93
|
|
|
$this->token = $this->parseToken($response); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Description: 解析access_token方法请求后的返回值 |
99
|
|
|
* @param $token |
100
|
|
|
* @return mixed |
101
|
|
|
* @throws OAuthException |
102
|
|
|
*/ |
103
|
|
|
protected function parseToken($token) |
104
|
|
|
{ |
105
|
|
|
$data = json_decode($token, true); |
106
|
|
|
if (isset($data['access_token'])) { |
107
|
|
|
return $data; |
108
|
|
|
} |
109
|
|
|
throw new OAuthException("获取GitLab ACCESS_TOKEN出错:" . ($data['error_description'] ?? '未知错误')); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* 刷新AccessToken续期 |
114
|
|
|
* @param string $refreshToken |
115
|
|
|
* @return bool |
116
|
|
|
* @throws OAuthException |
117
|
|
|
*/ |
118
|
|
|
public function refreshToken($refreshToken) |
119
|
|
|
{ |
120
|
|
|
$params = [ |
121
|
|
|
'grant_type' => 'refresh_token', |
122
|
|
|
'refresh_token' => $refreshToken, |
123
|
|
|
'client_id' => $this->config['app_id'], |
124
|
|
|
'client_secret' => $this->config['app_secret'], |
125
|
|
|
]; |
126
|
|
|
|
127
|
|
|
$token = $this->post($this->AccessTokenURL, $params); |
128
|
|
|
$token = $this->parseToken($token); |
129
|
|
|
|
130
|
|
|
if (isset($token['access_token'])) { |
131
|
|
|
$this->token = $token; |
132
|
|
|
return true; |
133
|
|
|
} |
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|