Oschina   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 62
dl 0
loc 128
rs 10
c 2
b 0
f 0
wmc 14

7 Methods

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