Skip to content

Commit 30d7f3f

Browse files
committed
feat 优化签名生成代码
1 parent 9f2de65 commit 30d7f3f

File tree

4 files changed

+44
-57
lines changed

4 files changed

+44
-57
lines changed

examples/client-upload/policy.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33

44
use Upyun\Config;
55
use Upyun\Signature;
6+
use Upyun\Util;
67

78
$config = new Config(BUCKET, USER_NAME, PWD);
89
$config->setFormApiKey('Mv83tlocuzkmfKKUFbz2s04FzTw=');
910

1011
$data['save-key'] = $_GET['save_path'];
1112
$data['expiration'] = time() + 120;
12-
$policy = Signature::getFormSignature($config, $data);
13-
$policy['authorization'] = 'UPYUN ' . USER_NAME . ':' . $policy['signature'];
14-
echo json_encode($policy);
15-
16-
13+
$data['bucket'] = BUCKET;
14+
$policy = Util::base64Json($data);
15+
$method = 'POST';
16+
$uri = '/' . $data['bucket'];
17+
$signature = Signature::getBodySignature($config, $method, $uri, null, $policy);
18+
echo json_encode(array(
19+
'policy' => $policy,
20+
'authorization' => $signature
21+
));

src/Upyun/Api/Form.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Upyun\Api;
44

55
use Upyun\Signature;
6+
use Upyun\Util;
67
use GuzzleHttp\Client;
78

89
class Form extends Rest{
@@ -14,24 +15,24 @@ public function upload($path, $stream, $params) {
1415
$params['expiration'] = time() + 30 * 60 * 60; // 30 分钟
1516
}
1617

17-
$result = Signature::getFormSignature($this->config, $params);
18-
$policy = $result['policy'];
19-
$signature = $result['signature'];
18+
$policy = Util::base64Json($params);
19+
$method = 'POST';
20+
$signature = Signature::getBodySignature($this->config, $method, '/' . $params['bucket'], null, $policy);
2021
$client = new Client([
2122
'timeout' => $this->config->timeout,
2223
]);
2324

2425
$url = ($this->config->useSsl ? 'https://' : 'http://') . $this->endpoint;
2526

26-
$response = $client->request('POST', $url, array(
27+
$response = $client->request($method, $url, array(
2728
'multipart' => array(
2829
array(
2930
'name' => 'policy',
3031
'contents' => $policy,
3132
),
3233
array(
3334
'name' => 'authorization',
34-
'contents' => 'UPYUN ' . $this->config->operatorName . ':' . $signature,
35+
'contents' => $signature,
3536
),
3637
array(
3738
'name' => 'file',
@@ -41,4 +42,4 @@ public function upload($path, $stream, $params) {
4142
));
4243
return $response->getStatusCode() === 200;
4344
}
44-
}
45+
}

src/Upyun/Signature.php

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ class Signature {
2121
const SIGN_VIDEO_NO_OPERATOR = 3;
2222

2323
/**
24-
* 获取 Header 签名
24+
* 获取 Header 签名需要的请求头
2525
*
2626
* @param Config $bucketConfig
27-
* @param $method
27+
* @param $method 请求方法
2828
* @param $path 请求路径
2929
* @param $contentMd5 文件内容 md5
3030
*
@@ -33,20 +33,11 @@ class Signature {
3333
public static function getHeaderSign($bucketConfig, $method, $path, $contentMd5 = null) {
3434
$gmtDate = gmdate('D, d M Y H:i:s \G\M\T');
3535

36-
$signParams = array(
37-
$method,
38-
$path,
39-
$gmtDate
40-
);
41-
42-
if ($contentMd5) {
43-
$signParams[] = $contentMd5;
44-
}
45-
46-
$sign = self::calcSignature($bucketConfig, $signParams);
36+
$policy = null;
37+
$sign = self::getBodySignature($bucketConfig, $method, $path, $gmtDate, $policy, $contentMd5);
4738

4839
$headers = array(
49-
'Authorization' => "UPYUN {$bucketConfig->operatorName}:$sign",
40+
'Authorization' => $sign,
5041
'Date' => $gmtDate,
5142
'User-agent' => 'Php-Sdk/' . $bucketConfig->getVersion()
5243
);
@@ -74,35 +65,32 @@ public static function getPurgeSignHeader(Config $bucketConfig, $urlString) {
7465
/**
7566
* 获取表单 API 需要的签名,依据 body 签名规则计算
7667
* @param Config $bucketConfig
77-
* @param $data
68+
* @param $method 请求方法
69+
* @param $uri 请求路径
70+
* @param $date 请求时间
71+
* @param $policy
72+
* @param $contentMd5 请求 body 的 md5
7873
*
7974
* @return array
8075
*/
81-
public static function getFormSignature(Config $bucketConfig, $data) {
82-
$data['bucket'] = $bucketConfig->bucketName;
83-
$policy = Util::base64Json($data);
84-
$signParams = array(
85-
'method' => 'POST',
86-
'uri' => '/' . $bucketConfig->bucketName,
76+
public static function getBodySignature(Config $bucketConfig, $method, $uri, $date = null, $policy = null, $contentMd5 = null) {
77+
$data = array(
78+
$method,
79+
$uri
8780
);
88-
if (isset($data['date'])) {
89-
$signParams['date'] = $data['date'];
81+
if ($date) {
82+
$data[] = $date;
9083
}
9184

92-
$signParams['policy'] = $policy;
93-
if (isset($data['content-md5'])) {
94-
$signParams['md5'] = $data['content-md5'];
95-
};
96-
97-
$signature = self::calcSignature($bucketConfig, $signParams);
98-
return array(
99-
'policy' => $policy,
100-
'signature' => $signature
101-
);
102-
}
85+
if ($policy) {
86+
$data[] = $policy;
87+
}
10388

104-
private static function calcSignature(Config $bucketConfig, $signParams) {
105-
return base64_encode(hash_hmac('sha1', implode('&', $signParams), $bucketConfig->operatorPassword, true));
89+
if ($contentMd5) {
90+
$data[] = $contentMd5;
91+
}
92+
$signature = base64_encode(hash_hmac('sha1', implode('&', $data), $bucketConfig->operatorPassword, true));
93+
return 'UPYUN ' . $bucketConfig->operatorName . ':' . $signature;
10694
}
10795

10896
public static function getSignature(Config $bucketConfig, $data, $type, $tokenSecret = '') {

tests/SignatureTest.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,8 @@ public function testGetSignature() {
1919
$this->assertEquals($sign , '2aa0afd612df8fab4b3fded36c396234');
2020
}
2121

22-
public function testGetFormSignature() {
23-
$config = new Config('upyun-temp', 'upyun', 'upyun520');
24-
$sign = Signature::getFormSignature($config, array(
25-
'save-key' => '/demo.jpg',
26-
'expiration' => '1478674618',
27-
'date' => 'Wed, 9 Nov 2016 14:26:58 GMT',
28-
'content-md5' => '7ac66c0f148de9519b8bd264312c4d64'
29-
));
30-
$this->assertEquals($sign['policy'], 'eyJzYXZlLWtleSI6Ii9kZW1vLmpwZyIsImV4cGlyYXRpb24iOiIxNDc4Njc0NjE4IiwiZGF0ZSI6IldlZCwgOSBOb3YgMjAxNiAxNDoyNjo1OCBHTVQiLCJjb250ZW50LW1kNSI6IjdhYzY2YzBmMTQ4ZGU5NTE5YjhiZDI2NDMxMmM0ZDY0IiwiYnVja2V0IjoidXB5dW4tdGVtcCJ9');
31-
$this->assertEquals($sign['signature'], 'aWqUna7XpJ3mJ6Clz6AMeay++Qk=');
22+
public function testGetBodySignature() {
23+
$sign = Signature::getBodySignature($this->config, 'POST', '/bucket');
24+
$this->assertEquals($sign, 'UPYUN operator:Xx3G6+DAvUyCL2Y2npSW/giTFI8=');
3225
}
3326
}

0 commit comments

Comments
 (0)