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

RpcRequest::resolveSecurityToken()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 3
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
11
/**
12
 * RESTful RPC Request.
13
 *
14
 * @package   AlibabaCloud\Client\Request
15
 */
16
class RpcRequest extends Request
17
{
18
19
    /**
20
     * @var string
21
     */
22
    private $dateTimeFormat = 'Y-m-d\TH:i:s\Z';
23
24
    /**
25
     * Resolve request parameter.
26
     *
27
     * @param AccessKeyCredential|BearerTokenCredential|StsCredential $credential
28
     *
29
     * @throws ClientException
30
     */
31 44
    public function resolveParameters($credential)
32
    {
33 44
        $this->resolveQuery($credential);
34
35 44
        $this->options['query']['Signature'] = $this->signature(
36 44
            $this->options['query'],
37 44
            $credential->getAccessKeySecret()
38 44
        );
39
40 43
        if ($this->method === 'POST') {
41 29
            foreach ($this->options['query'] as $apiParamKey => $apiParamValue) {
42 29
                $this->options['form_params'][$apiParamKey] = $apiParamValue;
43 29
            }
44 29
            unset($this->options['query']);
45 29
        }
46 43
    }
47
48
    /**
49
     * Resolve request query.
50
     *
51
     * @param AccessKeyCredential|BearerTokenCredential|StsCredential $credential
52
     *
53
     * @throws ClientException
54
     */
55 46
    private function resolveQuery($credential)
56
    {
57 46
        if (isset($this->options['query'])) {
58 20
            foreach ($this->options['query'] as $key => $value) {
59 20
                $this->options['query'][$key] = self::booleanValueToString($value);
60 20
            }
61 20
        }
62 46
        $signature                                  = $this->httpClient()->getSignature();
63 46
        $this->options['query']['RegionId']         = $this->realRegionId();
64 46
        $this->options['query']['AccessKeyId']      = $credential->getAccessKeyId();
65 46
        $this->options['query']['Format']           = $this->format;
66 46
        $this->options['query']['SignatureMethod']  = $signature->getMethod();
67 46
        $this->options['query']['SignatureVersion'] = $signature->getVersion();
68 46
        if ($signature->getType()) {
69 10
            $this->options['query']['SignatureType'] = $signature->getType();
70 10
        }
71 46
        $this->options['query']['SignatureNonce'] = md5(uniqid(mt_rand(), true));
72 46
        $this->options['query']['Timestamp']      = gmdate($this->dateTimeFormat);
73 46
        $this->options['query']['Action']         = $this->action;
74 46
        $this->options['query']['Version']        = $this->version;
75 46
        $this->resolveSecurityToken($credential);
76 46
        $this->resolveBearerToken($credential);
77 46
    }
78
79
    /**
80
     * @param CredentialsInterface $credential
81
     */
82 46
    private function resolveSecurityToken(CredentialsInterface $credential)
83
    {
84 46
        if ($credential instanceof StsCredential && $credential->getSecurityToken()) {
85 2
            $this->options['query']['SecurityToken'] = $credential->getSecurityToken();
86 2
        }
87 46
    }
88
89
    /**
90
     * @param CredentialsInterface $credential
91
     */
92 46
    private function resolveBearerToken(CredentialsInterface $credential)
93
    {
94 46
        if ($credential instanceof BearerTokenCredential) {
95 5
            $this->options['query']['BearerToken'] = $credential->getBearerToken();
96 5
        }
97 46
    }
98
99
    /**
100
     * Convert a Boolean value to a string.
101
     *
102
     * @param bool|string $value
103
     *
104
     * @return string
105
     */
106 27
    private static function booleanValueToString($value)
107
    {
108 27
        if (is_bool($value)) {
109 3
            if ($value) {
110 2
                return 'true';
111
            }
112
113 2
            return 'false';
114
        }
115
116 24
        return $value;
117
    }
118
119
    /**
120
     * Sign the parameters.
121
     *
122
     * @param array  $parameters
123
     * @param string $accessKeySecret
124
     *
125
     * @return mixed
126
     * @throws ClientException
127
     */
128 46
    private function signature($parameters, $accessKeySecret)
129
    {
130 46
        ksort($parameters);
131 46
        $canonicalizedQuery = '';
132 46
        foreach ($parameters as $key => $value) {
133 46
            $canonicalizedQuery .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
134 46
        }
135
136 46
        $this->stringToBeSigned = $this->method
137
                                  . '&%2F&'
138 46
                                  . $this->percentEncode(substr($canonicalizedQuery, 1));
139
140 46
        return $this->httpClient()
141 46
                    ->getSignature()
142 46
                    ->sign($this->stringToBeSigned, $accessKeySecret . '&');
143
    }
144
145
    /**
146
     * @param string $string
147
     *
148
     * @return null|string|string[]
149
     */
150 49
    protected function percentEncode($string)
151
    {
152 49
        $result = urlencode($string);
153 49
        $result = str_replace(['+', '*'], ['%20', '%2A'], $result);
154 49
        $result = preg_replace('/%7E/', '~', $result);
155
156 49
        return $result;
157
    }
158
159
    /**
160
     * Magic method for set or get request parameters.
161
     *
162
     * @param string $name
163
     * @param mixed  $arguments
164
     *
165
     * @return $this
166
     */
167 2
    public function __call($name, $arguments)
168
    {
169 2
        if (\strpos($name, 'get') !== false) {
170 2
            $parameterName = $this->propertyNameByMethodName($name);
171
172 2
            return $this->__get($parameterName);
173
        }
174
175 2
        if (\strpos($name, 'with') !== false) {
176 2
            $parameterName = $this->propertyNameByMethodName($name, 4);
177 2
            $this->__set($parameterName, $arguments[0]);
178 2
            $this->options['query'][$parameterName] = $arguments[0];
179 2
        }
180
181 2
        return $this;
182
    }
183
}
184