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

Csdn   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 58
dl 0
loc 124
rs 10
c 1
b 0
f 0
wmc 12

7 Methods

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