|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Freyo\Flysystem\QcloudCOSv5\Plugins; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use League\Flysystem\Plugin\AbstractPlugin; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class GetFederationToken. |
|
10
|
|
|
*/ |
|
11
|
|
|
class GetFederationToken extends AbstractPlugin |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Get the method name. |
|
15
|
|
|
* |
|
16
|
|
|
* @return string |
|
17
|
|
|
*/ |
|
18
|
|
|
public function getMethod() |
|
19
|
|
|
{ |
|
20
|
|
|
return 'getFederationToken'; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @see https://cloud.tencent.com/document/product/598/13896 |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $path |
|
27
|
|
|
* @param int $seconds |
|
28
|
|
|
* @param Closure $customPolicy |
|
29
|
|
|
* @param string $name |
|
30
|
|
|
* |
|
31
|
|
|
* @return bool|array |
|
32
|
|
|
*/ |
|
33
|
2 |
|
public function handle($path = '*', $seconds = 7200, Closure $customPolicy = null, $name = 'cos') |
|
34
|
|
|
{ |
|
35
|
2 |
|
$policy = !is_null($customPolicy) |
|
36
|
2 |
|
? $this->getCustomPolicy($customPolicy, $path) |
|
37
|
2 |
|
: $this->getDefaultPolicy($path); |
|
38
|
|
|
|
|
39
|
|
|
$params = [ |
|
40
|
2 |
|
'durationSeconds' => $seconds, |
|
41
|
2 |
|
'name' => $name, |
|
42
|
2 |
|
'policy' => urlencode($policy), |
|
43
|
2 |
|
]; |
|
44
|
|
|
|
|
45
|
2 |
|
return $this->request($params, 'GetFederationToken'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param Closure $callable |
|
50
|
|
|
* @param $path |
|
51
|
|
|
* |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
1 |
|
protected function getCustomPolicy(Closure $callable, $path) |
|
55
|
|
|
{ |
|
56
|
1 |
|
$policy = call_user_func($callable, $path, $this->getConfig()); |
|
57
|
|
|
|
|
58
|
1 |
|
return \GuzzleHttp\json_encode($policy, JSON_UNESCAPED_SLASHES); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @see https://cloud.tencent.com/document/product/436/31923 |
|
63
|
|
|
* |
|
64
|
|
|
* @param $path |
|
65
|
|
|
* |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
1 |
|
protected function getDefaultPolicy($path) |
|
69
|
|
|
{ |
|
70
|
1 |
|
$appId = $this->getCredentials()['appId']; |
|
71
|
|
|
|
|
72
|
1 |
|
$region = $this->getConfig()->get('region'); |
|
73
|
1 |
|
$bucket = $this->getConfig()->get('bucket'); |
|
74
|
|
|
|
|
75
|
|
|
$policy = [ |
|
76
|
1 |
|
'version' => '2.0', |
|
77
|
|
|
'statement' => [ |
|
78
|
|
|
'action' => [ |
|
79
|
|
|
// 简单上传 |
|
80
|
1 |
|
'name/cos:PutObject', |
|
81
|
1 |
|
'name/cos:PostObject', |
|
82
|
|
|
// 分片上传 |
|
83
|
1 |
|
'name/cos:InitiateMultipartUpload', |
|
84
|
1 |
|
'name/cos:ListParts', |
|
85
|
1 |
|
'name/cos:UploadPart', |
|
86
|
1 |
|
'name/cos:CompleteMultipartUpload', |
|
87
|
1 |
|
'name/cos:AbortMultipartUpload', |
|
88
|
1 |
|
], |
|
89
|
1 |
|
'effect' => 'allow', |
|
90
|
1 |
|
'principal' => ['qcs' => ['*']], |
|
91
|
|
|
'resource' => [ |
|
92
|
1 |
|
"qcs::cos:$region:uid/$appId:prefix//$appId/$bucket/$path", |
|
93
|
1 |
|
], |
|
94
|
1 |
|
], |
|
95
|
1 |
|
]; |
|
96
|
|
|
|
|
97
|
1 |
|
return \GuzzleHttp\json_encode($policy, JSON_UNESCAPED_SLASHES); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return \League\Flysystem\Config |
|
102
|
|
|
*/ |
|
103
|
2 |
|
protected function getConfig() |
|
104
|
|
|
{ |
|
105
|
2 |
|
return $this->filesystem->getConfig(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return array |
|
110
|
|
|
*/ |
|
111
|
2 |
|
protected function getCredentials() |
|
112
|
|
|
{ |
|
113
|
2 |
|
return $this->getConfig()->get('credentials'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param array $args |
|
118
|
|
|
* @param $action |
|
119
|
|
|
* |
|
120
|
|
|
* @return bool|array |
|
121
|
|
|
*/ |
|
122
|
2 |
|
protected function request(array $args, $action) |
|
123
|
|
|
{ |
|
124
|
2 |
|
$client = $this->getHttpClient(); |
|
125
|
|
|
|
|
126
|
2 |
|
$response = $client->post('/v2/index.php', [ |
|
127
|
2 |
|
'form_params' => $this->buildFormParams($args, $action), |
|
128
|
2 |
|
]); |
|
129
|
|
|
|
|
130
|
2 |
|
$contents = $response->getBody()->getContents(); |
|
131
|
|
|
|
|
132
|
2 |
|
return $this->normalize($contents); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return \GuzzleHttp\Client |
|
137
|
|
|
*/ |
|
138
|
2 |
|
protected function getHttpClient() |
|
139
|
|
|
{ |
|
140
|
2 |
|
return new \GuzzleHttp\Client([ |
|
141
|
2 |
|
'base_uri' => 'https://sts.api.qcloud.com', |
|
142
|
2 |
|
]); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param array $params |
|
147
|
|
|
* @param string $action |
|
148
|
|
|
* |
|
149
|
|
|
* @return array |
|
150
|
|
|
*/ |
|
151
|
2 |
|
protected function buildFormParams(array $params, $action) |
|
152
|
|
|
{ |
|
153
|
2 |
|
$params = $this->addCommonParams($params, $action); |
|
154
|
|
|
|
|
155
|
2 |
|
return $this->addSignature($params); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param array $params |
|
160
|
|
|
* @param string $action |
|
161
|
|
|
* |
|
162
|
|
|
* @return array |
|
163
|
|
|
*/ |
|
164
|
2 |
|
protected function addCommonParams(array $params, $action) |
|
165
|
|
|
{ |
|
166
|
2 |
|
return array_merge([ |
|
167
|
2 |
|
'Region' => $this->getConfig()->get('region'), |
|
168
|
2 |
|
'Action' => $action, |
|
169
|
2 |
|
'SecretId' => $this->getCredentials()['secretId'], |
|
170
|
2 |
|
'Timestamp' => time(), |
|
171
|
2 |
|
'Nonce' => rand(1, 65535), |
|
172
|
2 |
|
], $params); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @param array $params |
|
177
|
|
|
* |
|
178
|
|
|
* @return array |
|
179
|
|
|
*/ |
|
180
|
2 |
|
protected function addSignature(array $params) |
|
181
|
|
|
{ |
|
182
|
2 |
|
$params['Signature'] = $this->getSignature($params); |
|
183
|
|
|
|
|
184
|
2 |
|
return $params; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @param array $params |
|
189
|
|
|
* |
|
190
|
|
|
* @return string |
|
191
|
|
|
*/ |
|
192
|
2 |
|
protected function getSignature(array $params) |
|
193
|
|
|
{ |
|
194
|
2 |
|
ksort($params); |
|
195
|
|
|
|
|
196
|
2 |
|
$srcStr = 'POSTsts.api.qcloud.com/v2/index.php?'.urldecode(http_build_query($params)); |
|
197
|
|
|
|
|
198
|
2 |
|
return base64_encode(hash_hmac('sha1', $srcStr, $this->getCredentials()['secretKey'], true)); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @param string $contents |
|
203
|
|
|
* |
|
204
|
|
|
* @return bool|array |
|
205
|
|
|
*/ |
|
206
|
2 |
|
protected function normalize($contents) |
|
207
|
|
|
{ |
|
208
|
2 |
|
$data = json_decode($contents, true); |
|
209
|
|
|
|
|
210
|
2 |
|
if (json_last_error() !== JSON_ERROR_NONE || $data['code'] !== 0) { |
|
211
|
|
|
return false; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
2 |
|
return $data['data']; |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|