Passed
Push — master ( 273c7d...4c8747 )
by ma
02:37
created

Coding::refreshToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 1
dl 0
loc 17
rs 9.9
c 0
b 0
f 0
1
<?php
2
/**
3
 * Coding
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 Coding
13
 * @package tinymeng\OAuth2\Gateways
14
 * @Author: TinyMeng <[email protected]>
15
 * @Created: 2023/07/09
16
 */
17
class Coding extends Gateway
18
{
19
    const API_BASE            = 'https://coding.net/api/';
20
    protected $AuthorizeURL   = 'https://coding.net/oauth_authorize.html';
21
    protected $AccessTokenURL = 'https://coding.net/api/oauth/access_token';
22
    protected $UserInfoURL    = 'https://coding.net/api/account/current_user';
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
            'client_id'     => $this->config['app_id'],
37
            'redirect_uri'  => $this->config['callback'],
38
            'response_type' => $this->config['response_type'],
39
            'scope'         => $this->config['scope'],
40
            'state'         => $this->config['state'],
41
        ];
42
        return $this->AuthorizeURL . '?' . http_build_query($params);
43
    }
44
45
    /**
46
     * Description:  获取格式化后的用户信息
47
     * @return array
48
     * @throws OAuthException
49
     * @author: JiaMeng <[email protected]>
50
     * Updater:
51
     */
52
    public function userInfo()
53
    {
54
        $response = $this->getUserInfo();
55
        
56
        return [
57
            'open_id'  => $response['id'] ?? '',
58
            'union_id' => $response['id'] ?? '',
59
            'access_token' => $this->token['access_token'] ?? '',
60
            'channel' => ConstCode::TYPE_CODING,
61
            'nickname' => $response['name'] ?? '',
62
            'gender'   => ConstCode::GENDER,  // Coding 不返回性别信息
63
            'avatar'   => $response['avatar'] ?? '',
64
            // 额外信息
65
            'email'    => $response['email'] ?? '',
66
            'native'   => $response
67
        ];
68
    }
69
70
    /**
71
     * Description:  获取原始接口返回的用户信息
72
     * @return array
73
     * @throws OAuthException
74
     */
75
    public function getUserInfo()
76
    {
77
        $this->openid();
78
        $data = $this->get($this->UserInfoURL, [
79
            'access_token' => $this->token['access_token']
80
        ]);
81
        $data = json_decode($data, true);
82
        
83
        if(!isset($data['id'])) {
84
            throw new OAuthException("获取Coding用户信息失败:" . ($data['error_description'] ?? '未知错误'));
85
        }
86
        return $data;
87
    }
88
89
    /**
90
     * Description:  获取当前授权用户的openid标识
91
     * @return string
92
     * @throws OAuthException
93
     */
94
    public function openid()
95
    {
96
        $this->getToken();
97
        return $this->token['uid'] ?? '';
98
    }
99
100
    /**
101
     * Description:  获取AccessToken
102
     * @throws OAuthException
103
     */
104
    protected function getToken()
105
    {
106
        if (empty($this->token)) {
107
            $this->checkState();
108
            $params = [
109
                'grant_type'    => $this->config['grant_type'],
110
                'client_id'     => $this->config['app_id'],
111
                'client_secret' => $this->config['app_secret'],
112
                'code'          => isset($_REQUEST['code']) ? $_REQUEST['code'] : '',
113
                'redirect_uri'  => $this->config['callback'],
114
            ];
115
            $response = $this->post($this->AccessTokenURL, $params);
116
            $this->token = $this->parseToken($response);
117
        }
118
    }
119
120
    /**
121
     * Description:  解析access_token方法请求后的返回值
122
     * @param $token
123
     * @return mixed
124
     * @throws OAuthException
125
     */
126
    protected function parseToken($token)
127
    {
128
        $data = json_decode($token, true);
129
        if (isset($data['access_token'])) {
130
            return $data;
131
        }
132
        throw new OAuthException("获取Coding ACCESS_TOKEN出错:" . ($data['error_description'] ?? '未知错误'));
133
    }
134
135
    /**
136
     * 刷新AccessToken续期
137
     * @param string $refreshToken
138
     * @return bool
139
     * @throws OAuthException
140
     */
141
    public function refreshToken($refreshToken)
142
    {
143
        $params = [
144
            'grant_type'    => 'refresh_token',
145
            'refresh_token' => $refreshToken,
146
            'client_id'     => $this->config['app_id'],
147
            'client_secret' => $this->config['app_secret'],
148
        ];
149
        
150
        $token = $this->post($this->AccessTokenURL, $params);
151
        $token = $this->parseToken($token);
152
        
153
        if (isset($token['access_token'])) {
154
            $this->token = $token;
155
            return true;
156
        }
157
        return false;
158
    }
159
}
160