1
|
|
|
<?php |
2
|
|
|
namespace Qiniu; |
3
|
|
|
|
4
|
|
|
use Qiniu\Zone; |
5
|
|
|
|
6
|
|
|
final class Auth |
7
|
|
|
{ |
8
|
|
|
public $accessKey; |
9
|
|
|
public $secretKey; |
10
|
|
|
|
11
|
|
|
public function __construct($accessKey, $secretKey) |
12
|
|
|
{ |
13
|
|
|
$this->accessKey = $accessKey; |
14
|
|
|
$this->secretKey = $secretKey; |
15
|
|
|
} |
16
|
|
|
|
17
|
6 |
|
public function getAccessKey() |
18
|
|
|
{ |
19
|
6 |
|
return $this->accessKey; |
20
|
|
|
} |
21
|
|
|
|
22
|
87 |
|
public function sign($data) |
23
|
|
|
{ |
24
|
87 |
|
$hmac = hash_hmac('sha1', $data, $this->secretKey, true); |
25
|
87 |
|
$aa = $this->accessKey . ':' . \Qiniu\base64_urlSafeEncode($hmac); |
|
|
|
|
26
|
87 |
|
return $this->accessKey . ':' . \Qiniu\base64_urlSafeEncode($hmac); |
27
|
|
|
} |
28
|
|
|
|
29
|
24 |
|
public function signWithData($data) |
30
|
|
|
{ |
31
|
24 |
|
$encodedData = \Qiniu\base64_urlSafeEncode($data); |
32
|
24 |
|
return $this->sign($encodedData) . ':' . $encodedData; |
33
|
|
|
} |
34
|
|
|
|
35
|
48 |
|
public function signRequest($urlString, $body, $contentType = null) |
36
|
|
|
{ |
37
|
48 |
|
$url = parse_url($urlString); |
38
|
48 |
|
$data = ''; |
39
|
48 |
|
if (array_key_exists('path', $url)) { |
40
|
45 |
|
$data = $url['path']; |
41
|
45 |
|
} |
42
|
48 |
|
if (array_key_exists('query', $url)) { |
43
|
9 |
|
$data .= '?' . $url['query']; |
44
|
9 |
|
} |
45
|
48 |
|
$data .= "\n"; |
46
|
|
|
|
47
|
48 |
|
if ($body !== null && $contentType === 'application/x-www-form-urlencoded') { |
48
|
24 |
|
$data .= $body; |
49
|
24 |
|
} |
50
|
48 |
|
return $this->sign($data); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function verifyCallback($contentType, $originAuthorization, $url, $body) |
54
|
|
|
{ |
55
|
|
|
$authorization = 'QBox ' . $this->signRequest($url, $body, $contentType); |
56
|
|
|
return $originAuthorization === $authorization; |
57
|
|
|
} |
58
|
|
|
|
59
|
12 |
|
public function privateDownloadUrl($baseUrl, $expires = 3600) |
60
|
|
|
{ |
61
|
12 |
|
$deadline = time() + $expires; |
62
|
|
|
|
63
|
12 |
|
$pos = strpos($baseUrl, '?'); |
64
|
12 |
|
if ($pos !== false) { |
65
|
9 |
|
$baseUrl .= '&e='; |
66
|
9 |
|
} else { |
67
|
3 |
|
$baseUrl .= '?e='; |
68
|
|
|
} |
69
|
12 |
|
$baseUrl .= $deadline; |
70
|
|
|
|
71
|
12 |
|
$token = $this->sign($baseUrl); |
72
|
12 |
|
return "$baseUrl&token=$token"; |
73
|
|
|
} |
74
|
|
|
|
75
|
21 |
|
public function uploadToken($bucket, $key = null, $expires = 3600, $policy = null, $strictPolicy = true) |
76
|
|
|
{ |
77
|
21 |
|
$deadline = time() + $expires; |
78
|
21 |
|
$scope = $bucket; |
79
|
21 |
|
if ($key !== null) { |
80
|
15 |
|
$scope .= ':' . $key; |
81
|
15 |
|
} |
82
|
|
|
|
83
|
21 |
|
$args = self::copyPolicy($args, $policy, $strictPolicy); |
84
|
21 |
|
$args['scope'] = $scope; |
85
|
21 |
|
$args['deadline'] = $deadline; |
86
|
|
|
|
87
|
21 |
|
$b = json_encode($args); |
88
|
21 |
|
return $this->signWithData($b); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
*上传策略,参数规格详见 |
93
|
|
|
*http://developer.qiniu.com/docs/v6/api/reference/security/put-policy.html |
94
|
|
|
*/ |
95
|
|
|
private static $policyFields = array( |
96
|
|
|
'callbackUrl', |
97
|
|
|
'callbackBody', |
98
|
|
|
'callbackHost', |
99
|
|
|
'callbackBodyType', |
100
|
|
|
'callbackFetchKey', |
101
|
|
|
|
102
|
|
|
'returnUrl', |
103
|
|
|
'returnBody', |
104
|
|
|
|
105
|
|
|
'endUser', |
106
|
|
|
'saveKey', |
107
|
|
|
'insertOnly', |
108
|
|
|
|
109
|
|
|
'detectMime', |
110
|
|
|
'mimeLimit', |
111
|
|
|
'fsizeMin', |
112
|
|
|
'fsizeLimit', |
113
|
|
|
|
114
|
|
|
'persistentOps', |
115
|
|
|
'persistentNotifyUrl', |
116
|
|
|
'persistentPipeline', |
117
|
|
|
|
118
|
|
|
'deleteAfterDays', |
119
|
|
|
'fileType', |
120
|
|
|
'isPrefixalScope', |
121
|
|
|
); |
122
|
|
|
|
123
|
21 |
|
private static function copyPolicy(&$policy, $originPolicy, $strictPolicy) |
124
|
|
|
{ |
125
|
21 |
|
if ($originPolicy === null) { |
126
|
18 |
|
return array(); |
127
|
|
|
} |
128
|
3 |
|
foreach ($originPolicy as $key => $value) { |
129
|
3 |
|
if (!$strictPolicy || in_array((string)$key, self::$policyFields, true)) { |
130
|
3 |
|
$policy[$key] = $value; |
131
|
3 |
|
} |
132
|
3 |
|
} |
133
|
3 |
|
return $policy; |
134
|
|
|
} |
135
|
|
|
|
136
|
45 |
|
public function authorization($url, $body = null, $contentType = null) |
137
|
|
|
{ |
138
|
45 |
|
$authorization = 'QBox ' . $this->signRequest($url, $body, $contentType); |
139
|
45 |
|
return array('Authorization' => $authorization); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function authorizationV2($url, $method, $body = null, $contentType = null) |
143
|
|
|
{ |
144
|
|
|
$urlItems = parse_url($url); |
145
|
|
|
$host = $urlItems['host']; |
146
|
|
|
|
147
|
|
|
if (isset($urlItems['port'])) { |
148
|
|
|
$port = $urlItems['port']; |
149
|
|
|
} else { |
150
|
|
|
$port = ''; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$path = $urlItems['path']; |
154
|
|
|
if (isset($urlItems['query'])) { |
155
|
|
|
$query = $urlItems['query']; |
156
|
|
|
} else { |
157
|
|
|
$query = ''; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
//write request uri |
161
|
|
|
$toSignStr = $method . ' ' . $path; |
162
|
|
|
if (!empty($query)) { |
163
|
|
|
$toSignStr .= '?' . $query; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
//write host and port |
167
|
|
|
$toSignStr .= "\nHost: " . $host; |
168
|
|
|
if (!empty($port)) { |
169
|
|
|
$toSignStr .= ":" . $port; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
//write content type |
173
|
|
|
if (!empty($contentType)) { |
174
|
|
|
$toSignStr .= "\nContent-Type: " . $contentType; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$toSignStr .= "\n\n"; |
178
|
|
|
|
179
|
|
|
//write body |
180
|
|
|
if (!empty($body)) { |
181
|
|
|
$toSignStr .= $body; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$sign = $this->sign($toSignStr); |
185
|
|
|
$auth = 'Qiniu ' . $sign; |
186
|
|
|
return array('Authorization' => $auth); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function RtcToken($method, $url, $contentType, $body) |
190
|
|
|
{ |
191
|
|
|
$url = parse_url($url); |
192
|
|
|
$data = ''; |
193
|
|
|
if (!empty($url['path'])) { |
194
|
|
|
$data = $method . ' ' . $url['path']; |
195
|
|
|
} |
196
|
|
|
if (!empty($url['query'])) { |
197
|
|
|
$data .= '?' . $url['query']; |
198
|
|
|
} |
199
|
|
|
if (!empty($url['host'])) { |
200
|
|
|
$data .= "\nHost: " . $url['host']; |
201
|
|
|
if (isset($url['port'])) { |
202
|
|
|
$data .= ':' . $url['port']; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
if (!empty($contentType)) { |
206
|
|
|
$data .= "\nContent-Type: " . $contentType; |
207
|
|
|
} |
208
|
|
|
$data .= "\n\n"; |
209
|
|
|
if (!empty($body)) { |
210
|
|
|
$data .= $body; |
211
|
|
|
} |
212
|
|
|
$sign = $this->sign($data); |
213
|
|
|
return 'Qiniu ' . $sign; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.