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.
Test Failed
Push — master ( 82f96f...07aa43 )
by Yong
05:43
created

RoaRequest::signature()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 11
ccs 11
cts 11
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AlibabaCloud\Client\Request;
4
5
use AlibabaCloud\Client\Credentials\AccessKeyCredential;
6
use AlibabaCloud\Client\Credentials\BearerTokenCredential;
7
use AlibabaCloud\Client\Credentials\CredentialsInterface;
8
use AlibabaCloud\Client\Credentials\StsCredential;
9
use AlibabaCloud\Client\Exception\ClientException;
10
use AlibabaCloud\Client\Filter\ApiFilter;
11
use AlibabaCloud\Client\Filter\Filter;
12
use AlibabaCloud\Client\Request\Traits\DeprecatedRoaTrait;
13
use AlibabaCloud\Client\SDK;
14
use RuntimeException;
15
16
/**
17
 * RESTful ROA Request.
18
 *
19
 * @package   AlibabaCloud\Client\Request
20
 */
21
class RoaRequest extends Request
22
{
23
    use DeprecatedRoaTrait;
24
25
    /**
26
     * @var string
27
     */
28
    private static $headerSeparator = "\n";
29
30
    /**
31
     * @var string
32
     */
33
    private static $querySeparator = '&';
34
35
    /**
36
     * @var string
37
     */
38
    public $pathPattern = '/';
39
40
    /**
41
     * @var array
42
     */
43
    public $pathParameters = [];
44
45
    /**
46
     * @var string
47
     */
48
    private $dateTimeFormat = "D, d M Y H:i:s \G\M\T";
49
50
    /**
51
     * Resolve request parameter.
52
     *
53
     * @param AccessKeyCredential|BearerTokenCredential|StsCredential|CredentialsInterface $credential
54
     *
55
     * @throws ClientException
56
     */
57 17
    public function resolveParameters($credential)
58
    {
59 17
        $signature                                           = $this->httpClient()->getSignature();
60 17
        $this->options['query']['Version']                   = $this->version;
61 17
        $this->options['headers']['x-acs-version']           = $this->version;
62 17
        $this->options['headers']['Date']                    = gmdate($this->dateTimeFormat);
63 17
        $this->options['headers']['Accept']                  = self::formatToAccept($this->format);
64 17
        $this->options['headers']['x-acs-signature-method']  = $signature->getMethod();
65 17
        $this->options['headers']['x-acs-signature-version'] = $signature->getVersion();
66 17
        if ($signature->getType()) {
67 4
            $this->options['headers']['x-acs-signature-type'] = $signature->getType();
68 4
        }
69 17
        $this->options['headers']['x-acs-region-id'] = $this->realRegionId();
70 17
        if (isset($this->options['form_params'])) {
71 2
            $this->options['headers']['Content-MD5'] = $this->contentMD5();
72 2
        }
73 17
        $this->options['headers']['Content-Type'] = "{$this->options['headers']['Accept']};chrset=utf-8";
74
75 17
        $this->resolveSecurityToken($credential);
76 17
        $this->resolveBearerToken($credential);
77
78 17
        $this->options['headers']['Authorization'] = $this->signature($credential);
79 17
    }
80
81
    /**
82
     * Returns the accept header according to format.
83
     *
84
     * @param string $format
85
     *
86
     * @return string
87
     */
88 21
    private static function formatToAccept($format)
89
    {
90 21
        switch (\strtoupper($format)) {
91 21
            case 'JSON':
92 18
                return 'application/json';
93 3
            case 'XML':
94 1
                return 'application/xml';
95 2
            default:
96 2
                return 'application/octet-stream';
97 2
        }
98
    }
99
100
    /**
101
     * Calculate the md5 value of the content.
102
     *
103
     * @return string
104
     */
105 3
    private function contentMD5()
106
    {
107 3
        return base64_encode(
108 3
            md5(json_encode($this->options['form_params']), true)
109 3
        );
110
    }
111
112
    /**
113
     * @param CredentialsInterface $credential
114
     */
115 17
    private function resolveSecurityToken(CredentialsInterface $credential)
116
    {
117 17
        if ($credential instanceof StsCredential && $credential->getSecurityToken()) {
118 1
            $this->options['headers']['x-acs-security-token'] = $credential->getSecurityToken();
119 1
        }
120 17
    }
121
122
    /**
123
     * @param CredentialsInterface $credential
124
     */
125 17
    private function resolveBearerToken(CredentialsInterface $credential)
126
    {
127 17
        if ($credential instanceof BearerTokenCredential) {
128 3
            $this->options['headers']['x-acs-bearer-token'] = $credential->getBearerToken();
129 3
        }
130 17
    }
131
132
    /**
133
     * @return string
134
     */
135
    private function headerStringToSign()
136
    {
137
        $string = $this->method . self::$headerSeparator;
138
        if (isset($this->options['headers']['Accept'])) {
139 17
            $string .= $this->options['headers']['Accept'];
140
        }
141 17
        $string .= self::$headerSeparator;
142 17
143 17
        if (isset($this->options['headers']['Content-MD5'])) {
144 17
            $string .= $this->options['headers']['Content-MD5'];
145 17
        }
146
        $string .= self::$headerSeparator;
147 17
148 2
        if (isset($this->options['headers']['Content-Type'])) {
149 2
            $string .= $this->options['headers']['Content-Type'];
150 17
        }
151
        $string .= self::$headerSeparator;
152 17
153 17
        if (isset($this->options['headers']['Date'])) {
154 17
            $string .= $this->options['headers']['Date'];
155 17
        }
156
        $string .= self::$headerSeparator;
157 17
158 17
        $string .= $this->constructAcsHeader();
159 17
160 17
        return $string;
161
    }
162 17
163
    /**
164 17
     * @return string
165 17
     */
166
    private function resourceStringToSign()
167 17
    {
168
        $this->uri = $this->uri->withPath($this->assignPathParameters())
169 17
                               ->withQuery($this->queryString());
170
171 17
        return $this->uri->getPath() . '?' . $this->uri->getQuery();
172 17
    }
173 17
174 17
    /**
175 17
     * @return string
176 17
     */
177 17
    public function stringToBeSigned()
178 17
    {
179 17
        return $this->headerStringToSign() . $this->resourceStringToSign();
180 17
    }
181
182
    /**
183
     * Sign the request message.
184
     *
185
     * @param AccessKeyCredential|BearerTokenCredential|StsCredential $credential
186
     *
187 17
     * @return string
188
     * @throws ClientException
189 17
     */
190 17
    private function signature($credential)
191 17
    {
192 17
        $accessKeyId = $credential->getAccessKeyId();
193 17
        $signature   = $this->httpClient()
194 17
                            ->getSignature()
195 17
                            ->sign(
196 17
                                $this->stringToBeSigned(),
197 17
                                $credential->getAccessKeySecret()
198 17
                            );
199 17
200 17
        return "acs $accessKeyId:$signature";
201
    }
202 17
203
    /**
204
     * Construct standard Header for Alibaba Cloud.
205
     *
206
     * @return string
207
     */
208
    private function constructAcsHeader()
209
    {
210 19
        $sortMap = [];
211
        foreach ($this->options['headers'] as $headerKey => $headerValue) {
212 19
            $key = strtolower($headerKey);
213 19
            if (strpos($key, 'x-acs-') === 0) {
214 12
                $sortMap[$key] = $headerValue;
215 12
            }
216 19
        }
217
        ksort($sortMap);
218 19
        $headerString = '';
219
        foreach ($sortMap as $sortMapKey => $sortMapValue) {
220
            $headerString .= $sortMapKey . ':' . $sortMapValue . self::$headerSeparator;
221
        }
222
223
        return $headerString;
224
    }
225
226 17
    /**
227
     * Assign path parameters to the url.
228 17
     *
229 17
     * @return string
230 17
     */
231
    private function assignPathParameters()
232 17
    {
233
        $result = $this->pathPattern;
234 17
        foreach ($this->pathParameters as $pathParameterKey => $apiParameterValue) {
235 17
            $target = '[' . $pathParameterKey . ']';
236 17
            $result = str_replace($target, $apiParameterValue, $result);
237
        }
238 17
239
        return $result;
240
    }
241
242
    /**
243
     * Get the query string.
244
     *
245
     * @return bool|mixed|string
246
     */
247
    public function queryString()
248
    {
249 17
        $query = isset($this->options['query'])
250
            ? $this->options['query']
251 17
            : [];
252 17
253 17
        $queryString = $this->ksort($queryString, $query);
254 17
255 17
        if (0 < count($query)) {
256 17
            $queryString = substr($queryString, 0, -1);
257 17
        }
258 17
259
        return $queryString;
260 17
    }
261
262
    /**
263
     * Sort the entries by key.
264
     *
265
     * @param string $queryString
266
     * @param array  $map
267
     *
268
     * @return string
269
     */
270
    private function ksort(&$queryString, array $map)
271
    {
272 16
        ksort($map);
273
        foreach ($map as $sortMapKey => $sortMapValue) {
274 16
            $queryString .= $sortMapKey;
275
            if ($sortMapValue !== null) {
276 14
                $queryString .= '=' . $sortMapValue;
277 1
            }
278 1
            $queryString .= self::$querySeparator;
279
        }
280 1
281
        return $queryString;
282
    }
283 13
284
    /**
285 13
     * Set path parameter by name.
286
     *
287
     * @param string $name
288
     * @param string $value
289
     *
290
     * @return RoaRequest
291
     * @throws ClientException
292
     */
293
    public function pathParameter($name, $value)
294
    {
295
        Filter::name($name);
296 10
297
        if ($value === '') {
298 10
            throw new ClientException(
299
                'Value cannot be empty',
300 8
                SDK::INVALID_ARGUMENT
301
            );
302 8
        }
303
304
        $this->pathParameters[$name] = $value;
305
306
        return $this;
307
    }
308
309
    /**
310
     * Set path pattern.
311
     *
312
     * @param string $pattern
313 12
     *
314
     * @return self
315 12
     * @throws ClientException
316 1
     */
317
    public function pathPattern($pattern)
318 1
    {
319
        ApiFilter::pattern($pattern);
320
321 12
        $this->pathPattern = $pattern;
322 11
323 11
        return $this;
324 11
    }
325
326 11
    /**
327
     * Magic method for set or get request parameters.
328
     *
329 2
     * @param string $name
330 1
     * @param mixed  $arguments
331 1
     *
332
     * @return $this
333 1
     */
334
    public function __call($name, $arguments)
335
    {
336 1
        if (\strpos($name, 'get') === 0) {
337
            $parameterName = $this->propertyNameByMethodName($name);
338
339
            return $this->__get($parameterName);
340
        }
341
342
        if (\strpos($name, 'with') === 0) {
343
            $parameterName = $this->propertyNameByMethodName($name, 4);
344
            $this->__set($parameterName, $arguments[0]);
345
            $this->pathParameters[$parameterName] = $arguments[0];
346
347
            return $this;
348
        }
349
350
        if (\strpos($name, 'set') === 0) {
351
            $parameterName = $this->propertyNameByMethodName($name);
352
            $withMethod    = "with$parameterName";
353
354
            return $this->$withMethod($arguments[0]);
355
        }
356
357
        throw new RuntimeException('Call to undefined method ' . __CLASS__ . '::' . $name . '()');
358
    }
359
}
360