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.

Issues (3)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Rest/Client.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SimaLand\API\Rest;
4
5
use GuzzleHttp\ClientInterface;
6
use GuzzleHttp\RequestOptions;
7
use GuzzleHttp\Psr7\Response;
8
use SimaLand\API\BaseObject;
9
10
/**
11
 * SimaLand клиент.
12
 *
13
 * @link https://www.sima-land.ru/api/v3/help/
14
 */
15
class Client extends BaseObject
16
{
17
    /**
18
     * Базовый url API sima-land.ru.
19
     *
20
     * @var string
21
     */
22
    public $baseUrl = 'https://www.sima-land.ru/api/v3';
23
24
    /**
25
     * @var string
26
     */
27
    public $login;
28
29
    /**
30
     * @var string
31
     */
32
    public $password;
33
34
    /**
35
     * Путь до токена.
36
     *
37
     * @var string
38
     */
39
    public $tokenPath;
40
41
    /**
42
     * @var \GuzzleHttp\ClientInterface
43
     */
44
    private $httpClient;
45
46
    /**
47
     * @var string
48
     */
49
    private $token;
50
51
    /**
52
     * Базовые опции http запроса к API
53
     *
54
     * @var array
55
     */
56
    private $options = [
57
        'http_errors' => false,
58
        'headers' => [
59
            'User-Agent' => 'Sima-land api-php-client/3.0',
60
            'Content-Type' => 'application/json',
61
        ],
62
    ];
63
64
    /**
65
     * @param array $options
66
     * @throws \Exception
67
     */
68 120
    public function __construct(array $options = [])
69
    {
70 120
        if (!isset($options['login'])) {
71 3
            throw new \Exception('Login can`t be empty');
72
        }
73 117
        if (!isset($options['password'])) {
74 3
            throw new \Exception('Password can`t be empty');
75
        }
76 114
        parent::__construct($options);
77 114
    }
78
79
    /**
80
     * Получить http клиент.
81
     *
82
     * @return \GuzzleHttp\ClientInterface
83
     */
84 72
    public function getHttpClient()
85
    {
86 72
        if (is_null($this->httpClient)) {
87 6
            $this->httpClient = new \GuzzleHttp\Client();
88 2
        }
89 72
        return $this->httpClient;
90
    }
91
92
    /**
93
     * Установить http клиент.
94
     *
95
     * @param \GuzzleHttp\ClientInterface $httpClient
96
     * @return Client
97
     */
98 102
    public function setHttpClient(ClientInterface $httpClient)
99
    {
100 102
        $this->httpClient = $httpClient;
101 102
        return $this;
102
    }
103
104
    /**
105
     * Групповой запрос к API.
106
     *
107
     * @param array $requests
108
     * @return Response[]
109
     * @throws \Exception
110
     */
111 69
    public function batchQuery(array $requests)
112
    {
113 69
        $client = $this->getHttpClient();
114 69
        $promises = [];
115 69
        foreach ($requests as $name => $request) {
116 69
            if (!($request instanceof Request)) {
117 3
                throw new \Exception('Request must be implement "\SimaLand\API\Rest\Request"');
118
            }
119 66
            $url = $this->createUrl($request->entity);
120 66
            $this->getLogger()->info(
121 66
                "Send request {$url}",
122
                [
123 66
                    'getParams' => $request->getParams,
124 66
                    'postParams' => $request->postParams
125 22
                ]
126 22
            );
127 66
            $promises[$name] = $client->requestAsync(
128 66
                $request->method,
129 66
                $url,
130 66
                $this->getOptions($request)
131 20
            );
132 20
        }
133
        /** @var \GuzzleHttp\Psr7\Response[] $responses */
134 60
        $responses = \GuzzleHttp\Promise\unwrap($promises);
135 57
        foreach ($responses as $key => $response) {
136 57
            if ($response->getStatusCode() == 401) {
137 3
                $this->deleteToken();
138 39
                return $this->batchQuery($requests);
139
            }
140 19
        }
141 57
        return $responses;
142
    }
143
144
    /**
145
     * Запрос к API.
146
     *
147
     * @param string $method
148
     * @param string $entity
149
     * @param array $getParams
150
     * @param array $postParams
151
     * @return Response
152
     * @throws \Exception
153
     */
154 12
    public function query($method, $entity, array $getParams = [], array $postParams = [])
155
    {
156 12
        $response = $this->batchQuery([
157 12
            new Request([
158 12
                'entity' => $entity,
159 12
                'method' => $method,
160 12
                'getParams' => $getParams,
161 8
                'postParams' => $postParams
162 4
            ])
163 4
        ]);
164 6
        return reset($response);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression reset($response); of type GuzzleHttp\Psr7\Response|false adds false to the return on line 164 which is incompatible with the return type documented by SimaLand\API\Rest\Client::query of type GuzzleHttp\Psr7\Response. It seems like you forgot to handle an error condition.
Loading history...
165
    }
166
167
    /**
168
     * GET запрос к API.
169
     *
170
     * @param string $entity
171
     * @param array $getParams
172
     * @return Response
173
     */
174 12
    public function get($entity, array $getParams = [])
175
    {
176 12
        return $this->query('GET', $entity, $getParams);
177
    }
178
179
    /**
180
     * Удалить файл с токеном.
181
     *
182
     * @return Client
183
     * @throws \Exception
184
     */
185 6
    public function deleteToken()
186
    {
187 6
        $this->token = null;
188 6
        $filename = $this->getTokenFilename();
189 6
        if (file_exists($filename)) {
190 6
            unlink($filename);
191 2
        }
192 6
        return $this;
193
    }
194
195
    /**
196
     * Получить опции для http клиента.
197
     *
198
     * @param Request|null $request
199
     * @return array
200
     */
201 69
    public function getOptions(Request $request = null)
202
    {
203 69
        $options = [];
204 69
        if (!is_null($request)) {
205 69
            if (!empty($request->getParams)) {
206 33
                $options[RequestOptions::QUERY] = $request->getParams;
207 11
            }
208 69
            if (!empty($request->postParams)) {
209 3
                $options[RequestOptions::JSON] = $request->postParams;
210 1
            }
211 23
        }
212 69
        $options = array_merge($this->options, $options);
213 69
        $options['headers']['Authorization'] = 'Bearer ' . $this->getToken();
214 63
        return $options;
215
    }
216
217
    /**
218
     * Аутентификация пользователя.
219
     *
220
     * @throws \Exception
221
     */
222 6
    private function auth()
223
    {
224 6
        $client = $this->getHttpClient();
225 6
        $options = $this->options;
226 6
        $options['headers']['Authorization'] = 'Basic ' . base64_encode($this->login . ":" . $this->password);
227 6
        $response = $client->request('GET', $this->createUrl('auth'), $options);
228 6
        if ($response->getStatusCode() != 200) {
229 3
            throw new \Exception($response->getReasonPhrase(), $response->getStatusCode());
230
        }
231 3
        $response->getStatusCode();
232 3
        $body = json_decode($response->getBody(), true);
233 3
        $this->token = $body['jwt'];
234 3
        file_put_contents($this->getTokenFilename(), $body['jwt']);
235 3
    }
236
237
    /**
238
     * Создания url к сущности.
239
     *
240
     * @param string $entity
241
     * @return string
242
     */
243 66
    private function createUrl($entity)
244
    {
245 66
        $url = $this->baseUrl;
246 66
        $urlLen = strlen($url);
247 66
        $entityLen = strlen($entity);
248 66
        if ($url[$urlLen - 1] != '/' && $entity[0] != '/') {
249 66
            $url .= "/";
250 22
        }
251 66
        if ($entity[$entityLen - 1] != '/') {
252 66
            $entity .= "/";
253 22
        }
254 66
        return $url . $entity;
255
    }
256
257
    /**
258
     * Получить полный путь до токена.
259
     *
260
     * @return string
261
     * @throws \Exception
262
     */
263 72
    private function getTokenFilename()
264
    {
265 72
        if (is_null($this->tokenPath)) {
266 3
            $this->tokenPath = sys_get_temp_dir();
267 1
        }
268 72
        if (!file_exists($this->tokenPath)) {
269 3
            throw new \Exception("Path {$this->tokenPath} not found");
270
        }
271 69
        if (substr($this->tokenPath, -1) != '/') {
272 69
            $this->tokenPath .= '/';
273 23
        }
274 69
        return $this->tokenPath . 'token.txt';
275
    }
276
277
    /**
278
     * Получить токен.
279
     *
280
     * @return string
281
     * @throws \Exception
282
     */
283 69
    private function getToken()
284
    {
285 69
        if (is_null($this->token)) {
286 69
            $filename = $this->getTokenFilename();
287 66
            if (file_exists($filename)) {
288 63
                $this->token = file_get_contents($filename);
289 21
            } else {
290 6
                $this->auth();
291
            }
292 21
        }
293 63
        return $this->token;
294
    }
295
}
296