1
|
|
|
<?php |
2
|
|
|
namespace Qiniu; |
3
|
|
|
|
4
|
|
|
use Qiniu\Zone; |
5
|
|
|
|
6
|
|
|
final class Auth |
7
|
|
|
{ |
8
|
|
|
private $accessKey; |
9
|
|
|
private $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
|
93 |
|
public function sign($data) |
23
|
|
|
{ |
24
|
93 |
|
$hmac = hash_hmac('sha1', $data, $this->secretKey, true); |
25
|
93 |
|
return $this->accessKey . ':' . \Qiniu\base64_urlSafeEncode($hmac); |
26
|
|
|
} |
27
|
|
|
|
28
|
24 |
|
public function signWithData($data) |
29
|
|
|
{ |
30
|
24 |
|
$encodedData = \Qiniu\base64_urlSafeEncode($data); |
31
|
24 |
|
return $this->sign($encodedData) . ':' . $encodedData; |
32
|
|
|
} |
33
|
|
|
|
34
|
54 |
|
public function signRequest($urlString, $body, $contentType = null) |
35
|
|
|
{ |
36
|
54 |
|
$url = parse_url($urlString); |
37
|
54 |
|
$data = ''; |
38
|
54 |
|
if (array_key_exists('path', $url)) { |
39
|
51 |
|
$data = $url['path']; |
40
|
34 |
|
} |
41
|
54 |
|
if (array_key_exists('query', $url)) { |
42
|
6 |
|
$data .= '?' . $url['query']; |
43
|
4 |
|
} |
44
|
54 |
|
$data .= "\n"; |
45
|
|
|
|
46
|
54 |
|
if ($body !== null && $contentType === 'application/x-www-form-urlencoded') { |
47
|
24 |
|
$data .= $body; |
48
|
16 |
|
} |
49
|
54 |
|
return $this->sign($data); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function verifyCallback($contentType, $originAuthorization, $url, $body) |
53
|
|
|
{ |
54
|
|
|
$authorization = 'QBox ' . $this->signRequest($url, $body, $contentType); |
55
|
|
|
return $originAuthorization === $authorization; |
56
|
|
|
} |
57
|
|
|
|
58
|
12 |
|
public function privateDownloadUrl($baseUrl, $expires = 3600) |
59
|
|
|
{ |
60
|
12 |
|
$deadline = time() + $expires; |
61
|
|
|
|
62
|
12 |
|
$pos = strpos($baseUrl, '?'); |
63
|
12 |
|
if ($pos !== false) { |
64
|
9 |
|
$baseUrl .= '&e='; |
65
|
6 |
|
} else { |
66
|
3 |
|
$baseUrl .= '?e='; |
67
|
|
|
} |
68
|
12 |
|
$baseUrl .= $deadline; |
69
|
|
|
|
70
|
12 |
|
$token = $this->sign($baseUrl); |
71
|
12 |
|
return "$baseUrl&token=$token"; |
72
|
|
|
} |
73
|
|
|
|
74
|
21 |
|
public function uploadToken( |
75
|
|
|
$bucket, |
76
|
|
|
$key = null, |
77
|
|
|
$expires = 3600, |
78
|
|
|
$policy = null, |
79
|
|
|
$strictPolicy = true, |
80
|
|
|
Zone $zone = null |
81
|
|
|
) { |
82
|
21 |
|
$deadline = time() + $expires; |
83
|
21 |
|
$scope = $bucket; |
84
|
21 |
|
if ($key !== null) { |
85
|
15 |
|
$scope .= ':' . $key; |
86
|
10 |
|
} |
87
|
|
|
|
88
|
21 |
|
$args = self::copyPolicy($args, $policy, $strictPolicy); |
89
|
21 |
|
$args['scope'] = $scope; |
90
|
21 |
|
$args['deadline'] = $deadline; |
91
|
|
|
|
92
|
21 |
|
if ($zone === null) { |
93
|
21 |
|
$zone = new Zone(); |
94
|
14 |
|
} |
95
|
|
|
|
96
|
21 |
|
list($upHosts, $err) = $zone->getUpHosts($this->accessKey, $bucket); |
97
|
21 |
|
if ($err === null) { |
98
|
|
|
$args['upHosts'] = $upHosts; |
99
|
|
|
} |
100
|
|
|
|
101
|
21 |
|
$b = json_encode($args); |
102
|
21 |
|
return $this->signWithData($b); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
*上传策略,参数规格详见 |
107
|
|
|
*http://developer.qiniu.com/docs/v6/api/reference/security/put-policy.html |
108
|
|
|
*/ |
109
|
|
|
private static $policyFields = array( |
110
|
|
|
'callbackUrl', |
111
|
|
|
'callbackBody', |
112
|
|
|
'callbackHost', |
113
|
|
|
'callbackBodyType', |
114
|
|
|
'callbackFetchKey', |
115
|
|
|
|
116
|
|
|
'returnUrl', |
117
|
|
|
'returnBody', |
118
|
|
|
|
119
|
|
|
'endUser', |
120
|
|
|
'saveKey', |
121
|
|
|
'insertOnly', |
122
|
|
|
|
123
|
|
|
'detectMime', |
124
|
|
|
'mimeLimit', |
125
|
|
|
'fsizeMin', |
126
|
|
|
'fsizeLimit', |
127
|
|
|
|
128
|
|
|
'persistentOps', |
129
|
|
|
'persistentNotifyUrl', |
130
|
|
|
'persistentPipeline', |
131
|
|
|
|
132
|
|
|
'deleteAfterDays', |
133
|
|
|
'fileType', |
134
|
|
|
|
135
|
|
|
'upHosts', |
136
|
|
|
); |
137
|
|
|
|
138
|
21 |
|
private static function copyPolicy(&$policy, $originPolicy, $strictPolicy) |
139
|
|
|
{ |
140
|
21 |
|
if ($originPolicy === null) { |
141
|
18 |
|
return array(); |
142
|
|
|
} |
143
|
3 |
|
foreach ($originPolicy as $key => $value) { |
144
|
3 |
|
if (!$strictPolicy || in_array((string) $key, self::$policyFields, true)) { |
145
|
3 |
|
$policy[$key] = $value; |
146
|
2 |
|
} |
147
|
2 |
|
} |
148
|
3 |
|
return $policy; |
149
|
|
|
} |
150
|
|
|
|
151
|
51 |
|
public function authorization($url, $body = null, $contentType = null) |
152
|
|
|
{ |
153
|
51 |
|
$authorization = 'QBox ' . $this->signRequest($url, $body, $contentType); |
154
|
51 |
|
return array('Authorization' => $authorization); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|