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