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.
Completed
Pull Request — master (#76)
by Yong
08:56 queued 04:09
created

RoaRequest::resolveBearerToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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