GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( da5a74...2fceb9 )
by Coeus
02:04
created

Gateway::clientParams()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 6
nc 8
nop 0
dl 0
loc 10
rs 8.8333
c 0
b 0
f 0
1
<?php
2
namespace anerg\OAuth2\Connector;
3
4
use anerg\OAuth2\Connector\GatewayInterface;
5
6
/**
7
 * 所有第三方登录必须继承的抽象类
8
 */
9
abstract class Gateway implements GatewayInterface
10
{
11
    /**
12
     * 配置参数
13
     * @var array
14
     */
15
    protected $config;
16
17
    /**
18
     * 当前时间戳
19
     * @var int
20
     */
21
    protected $timestamp;
22
23
    /**
24
     * 默认第三方授权页面样式
25
     * @var string
26
     */
27
    protected $display = 'default';
28
29
    /**
30
     * 第三方Token信息
31
     * @var array
32
     */
33
    protected $token = null;
34
35
    /**
36
     * 是否验证回跳地址中的state参数
37
     * @var boolean
38
     */
39
    protected $checkState = false;
40
41
    public function __construct($config = null)
42
    {
43
        if (!$config) {
44
            throw new \Exception('传入的配置不能为空');
45
        }
46
        //默认参数
47
        $_config = [
48
            'app_id'        => '',
49
            'app_secret'    => '',
50
            'callback'      => '',
51
            'response_type' => 'code',
52
            'grant_type'    => 'authorization_code',
53
            'proxy'         => '',
54
            'state'         => '',
55
        ];
56
        $this->config    = array_merge($_config, $config);
57
        $this->timestamp = time();
58
    }
59
60
    /**
61
     * 设置客户端请求的参数
62
     *
63
     * @return void
64
     */
65
    private function clientParams()
0 ignored issues
show
Unused Code introduced by
The method clientParams() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
66
    {
67
        if (isset($this->config['access_token']) && !empty($this->config['access_token'])) {
68
            $this->token['access_token'] = $this->config['access_token'];
69
        }
70
        if (isset($this->config['oauth_token']) && !empty($this->config['oauth_token'])) {
71
            $this->token['oauth_token'] = $this->config['oauth_token'];
72
        }
73
        if (isset($this->config['oauth_token_secret']) && !empty($this->config['oauth_token_secret'])) {
74
            $this->token['oauth_token_secret'] = $this->config['oauth_token_secret'];
75
        }
76
    }
77
78
    /**
79
     * 设置授权页面样式
80
     *
81
     * @param string $display
82
     * @return self
83
     */
84
    public function setDisplay($display)
85
    {
86
        $this->display = $display;
87
        return $this;
88
    }
89
90
    /**
91
     * 强制验证回跳地址中的state参数
92
     *
93
     * @return self
94
     */
95
    public function mustCheckState()
96
    {
97
        $this->checkState = true;
98
        return $this;
99
    }
100
101
    /**
102
     * 执行GET请求操作
103
     *
104
     * @param string $url
105
     * @param array $params
106
     * @param array $headers
107
     * @return string
108
     */
109
    protected function GET($url, $params = [], $headers = [])
110
    {
111
        $client   = new \GuzzleHttp\Client();
112
        $response = $client->request('GET', $url, ['proxy' => $this->config['proxy'], 'headers' => $headers, 'query' => $params]);
113
        return $response->getBody()->getContents();
114
    }
115
116
    /**
117
     * 执行POST请求操作
118
     *
119
     * @param string $url
120
     * @param array $params
121
     * @param array $headers
122
     * @return string
123
     */
124
    protected function POST($url, $params = [], $headers = [])
125
    {
126
        $client   = new \GuzzleHttp\Client();
127
        $response = $client->request('POST', $url, ['proxy' => $this->config['proxy'], 'headers' => $headers, 'form_params' => $params, 'http_errors' => false]);
128
        return $response->getBody()->getContents();
129
    }
130
131
    /**
132
     * 默认的AccessToken请求参数
133
     * @return array
134
     */
135
    protected function accessTokenParams()
136
    {
137
        $params = [
138
            'client_id'     => $this->config['app_id'],
139
            'client_secret' => $this->config['app_secret'],
140
            'grant_type'    => $this->config['grant_type'],
141
            'code'          => isset($_GET['code']) ? $_GET['code'] : '',
142
            'redirect_uri'  => $this->config['callback'],
143
        ];
144
        return $params;
145
    }
146
147
    /**
148
     * 获取AccessToken
149
     *
150
     * @return string
151
     */
152
    protected function getAccessToken()
153
    {
154
        if ($this->checkState === true) {
155
            if (!isset($_GET['state']) || $_GET['state'] != $this->config['state']) {
156
                throw new \Exception('传递的STATE参数不匹配!');
157
            }
158
        }
159
        $params = $this->accessTokenParams();
160
        return $this->POST($this->AccessTokenURL, $params);
161
    }
162
163
    /**
164
     * 获取token信息
165
     *
166
     * @return void
167
     */
168
    protected function getToken()
169
    {
170
        if (empty($this->token)) {
171
            $token = $this->getAccessToken();
172
            /** @scrutinizer ignore-call */
173
            $this->token = $this->parseToken($token);
174
        }
175
    }
176
}
177