Completed
Push — master ( 005ddd...9800f2 )
by frey
04:38
created

TencentCloudAuthV3::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Freyo\Flysystem\QcloudCOSv5\Plugins\Traits;
4
5
trait TencentCloudAuthV3
6
{
7
    /**
8
     * @return \League\Flysystem\Config
9
     */
10
    protected function getConfig()
11
    {
12
        return $this->filesystem->getConfig();
13
    }
14
15
    /**
16
     * @return array
17
     */
18
    protected function getCredentials()
19
    {
20
        return $this->getConfig()->get('credentials');
21
    }
22
23
    /**
24
     * @param array $args
25
     * @param string $action
26
     * @param string $service
27
     * @param null $timestamp
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $timestamp is correct as it would always require null to be passed?
Loading history...
28
     *
29
     * @return bool|array
30
     */
31
    protected function request(array $args, $action, $service, $timestamp = null)
32
    {
33
        $client = $this->getHttpClient($service);
34
35
        $response = $client->post('/', [
36
            'headers' => [
37
                'X-TC-Action' => $action,
38
                'X-TC-Region' => $this->getConfig()->get('region'),
39
                'X-TC-Timestamp' => $timestamp = $timestamp ?: time(),
0 ignored issues
show
introduced by
$timestamp is of type null, thus it always evaluated to false.
Loading history...
40
                'X-TC-Version' => '2018-03-01',
41
                'Authorization' => $this->getAuthorization($args, $timestamp, $service),
42
            ],
43
            'json' => $args,
44
        ]);
45
46
        $contents = $response->getBody()->getContents();
47
48
        return $this->normalize($contents);
49
    }
50
51
    /**
52
     * @return \GuzzleHttp\Client
53
     */
54
    protected function getHttpClient($service)
55
    {
56
        return new \GuzzleHttp\Client([
57
            'base_uri' => "https://{$service}.tencentcloudapi.com",
58
        ]);
59
    }
60
61
    /**
62
     * @param string $contents
63
     *
64
     * @return bool|array
65
     */
66
    protected function normalize($contents)
67
    {
68
        $data = json_decode($contents, true);
69
70
        if (json_last_error() !== JSON_ERROR_NONE || !isset($data['Response'])) {
71
            return false;
72
        }
73
74
        return $data['Response'];
75
    }
76
77
    /**
78
     * @param $args
79
     * @param $timestamp
80
     * @param $service
81
     *
82
     * @return string
83
     */
84
    protected function getAuthorization($args, $timestamp, $service)
85
    {
86
        return sprintf(
87
            '%s Credential=%s/%s, SignedHeaders=%s, Signature=%s',
88
            'TC3-HMAC-SHA256',
89
            $this->getCredentials()['secretId'],
90
            date('Y-m-d', $timestamp) . "/{$service}/tc3_request",
91
            'content-type;host',
92
            hash_hmac(
93
                'SHA256',
94
                $this->getSignatureString($args, $timestamp, $service),
95
                $this->getRequestKey($timestamp, $service)
96
            )
97
        );
98
    }
99
100
    /**
101
     * @param $timestamp
102
     * @param $service
103
     *
104
     * @return string
105
     */
106
    protected function getRequestKey($timestamp, $service)
107
    {
108
        return hash_hmac('SHA256', 'tc3_request',
109
            hash_hmac('SHA256', $service,
110
                hash_hmac('SHA256', date('Y-m-d', $timestamp),
111
                    'TC3' . $this->getCredentials()['secretKey'], true
112
                ), true
113
            ), true
114
        );
115
    }
116
117
    /**
118
     * @param $args
119
     * @param $service
120
     *
121
     * @return string
122
     */
123
    protected function getCanonicalRequest($args, $service)
124
    {
125
        return implode("\n", [
126
            'POST',
127
            '/',
128
            '',
129
            'content-type:application/json',
130
            "host:{$service}.tencentcloudapi.com",
131
            '',
132
            'content-type;host',
133
            hash('SHA256', \GuzzleHttp\json_encode(
134
                $args, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
135
            )),
136
        ]);
137
    }
138
139
    /**
140
     * @param $args
141
     * @param $timestamp
142
     * @param $service
143
     *
144
     * @return string
145
     */
146
    protected function getSignatureString($args, $timestamp, $service)
147
    {
148
        return implode("\n", [
149
            'TC3-HMAC-SHA256',
150
            $timestamp,
151
            date('Y-m-d', $timestamp) . "/{$service}/tc3_request",
152
            hash('SHA256', $this->getCanonicalRequest($args, $service)),
153
        ]);
154
    }
155
}