Skip to content

Commit c1c96a4

Browse files
authored
Merge pull request #21 from upyun/feature/new-signature
feat use new form api signature
2 parents d86ec68 + 8f8cc64 commit c1c96a4

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

src/Upyun/Api/Form.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public function upload($path, $stream, $params) {
3030
'contents' => $policy,
3131
),
3232
array(
33-
'name' => 'signature',
34-
'contents' => $signature,
33+
'name' => 'authorization',
34+
'contents' => 'UPYUN ' . $this->config->operatorName . ':' . $signature,
3535
),
3636
array(
3737
'name' => 'file',

src/Upyun/Signature.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,20 @@ public static function getPurgeSignHeader(Config $bucketConfig, $urlString) {
6565
public static function getFormSignature(Config $bucketConfig, $data) {
6666
$data['bucket'] = $bucketConfig->bucketName;
6767
$policy = Util::base64Json($data);
68-
$signature = md5($policy . '&' . $bucketConfig->getFormApiKey());
68+
$signParams = array(
69+
'method' => 'POST',
70+
'uri' => '/' . $bucketConfig->bucketName,
71+
);
72+
if (isset($data['date'])) {
73+
$signParams['date'] = $data['date'];
74+
}
75+
76+
$signParams['policy'] = $policy;
77+
if (isset($data['content-md5'])) {
78+
$signParams['md5'] = $data['content-md5'];
79+
};
80+
81+
$signature = base64_encode(hash_hmac('sha1', implode('&', $signParams), $bucketConfig->operatorPassword, true));
6982
return array(
7083
'policy' => $policy,
7184
'signature' => $signature

src/Upyun/Util.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static function getHeaderParams($headers) {
2020
}
2121
return $params;
2222
}
23-
23+
2424
public static function parseDir($body) {
2525
$files = array();
2626
if(!$body) {
@@ -38,7 +38,7 @@ public static function parseDir($body) {
3838
}
3939

4040
public static function base64Json($params) {
41-
return base64_encode(json_encode($params));
41+
return base64_encode(json_encode($params, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
4242
}
4343

4444
public static function stringifyHeaders($headers) {
@@ -48,7 +48,7 @@ public static function stringifyHeaders($headers) {
4848
}
4949
return $return;
5050
}
51-
51+
5252
public static function md5Hash($resource) {
5353
rewind($resource);
5454
$ctx = hash_init('md5');

tests/SignatureTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,16 @@ public function testGetSignature() {
1818
$sign = Signature::getSignature($this->config, array('a' => 'a', 'b' => 'b'), Signature::SIGN_MULTIPART, '123');
1919
$this->assertEquals($sign , '2aa0afd612df8fab4b3fded36c396234');
2020
}
21-
}
21+
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=');
32+
}
33+
}

0 commit comments

Comments
 (0)