Skip to content

Commit dd4b3d3

Browse files
authored
Merge pull request #373 from qiniu/features/sign-qiniu-authorization
add signQiniuAuthorization
2 parents e036cb7 + 6bf87f2 commit dd4b3d3

File tree

2 files changed

+215
-0
lines changed

2 files changed

+215
-0
lines changed

src/Qiniu/Auth.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Qiniu;
33

4+
use Qiniu\Http\Header;
45
use Qiniu\Zone;
56

67
final class Auth
@@ -49,6 +50,80 @@ public function signRequest($urlString, $body, $contentType = null)
4950
return $this->sign($data);
5051
}
5152

53+
/**
54+
* @param string $urlString
55+
* @param string $method
56+
* @param string $body
57+
* @param null|Header $headers
58+
*/
59+
public function signQiniuAuthorization($urlString, $method = "GET", $body = "", $headers = null)
60+
{
61+
$url = parse_url($urlString);
62+
if (!$url) {
63+
return array(null, new \Exception("parse_url error"));
64+
}
65+
66+
// append method, path and query
67+
if ($method === "") {
68+
$data = "GET ";
69+
} else {
70+
$data = $method . " ";
71+
}
72+
if (isset($url["path"])) {
73+
$data .= $url["path"];
74+
}
75+
if (isset($url["query"])) {
76+
$data .= "?" . $url["query"];
77+
}
78+
79+
// append Host
80+
$data .= "\n";
81+
$data .= "Host: ";
82+
if (isset($url["host"])) {
83+
$data .= $url["host"];
84+
}
85+
if (isset($url["port"]) && $url["port"] > 0) {
86+
$data .= ":" . $url["port"];
87+
}
88+
89+
// try append content type
90+
if ($headers != null && isset($headers["Content-Type"])) {
91+
// append content type
92+
$data .= "\n";
93+
$data .= "Content-Type: " . $headers["Content-Type"];
94+
}
95+
96+
// try append xQiniuHeaders
97+
if ($headers != null) {
98+
$headerLines = array();
99+
$keyPrefix = "X-Qiniu-";
100+
foreach ($headers as $k => $v) {
101+
if (strlen($k) > strlen($keyPrefix) && strpos($k, $keyPrefix) === 0) {
102+
array_push(
103+
$headerLines,
104+
$k . ": " . $v
105+
);
106+
}
107+
}
108+
if (count($headerLines) > 0) {
109+
$data .= "\n";
110+
sort($headerLines);
111+
$data .= implode("\n", $headerLines);
112+
}
113+
}
114+
115+
// append body
116+
$data .= "\n\n";
117+
if (count($body) > 0
118+
&& isset($headers["Content-Type"])
119+
&& $headers["Content-Type"] != "application/octet-stream"
120+
) {
121+
$data .= $body;
122+
}
123+
124+
return array($this->sign(utf8_encode($data)), null);
125+
}
126+
52127
public function verifyCallback($contentType, $originAuthorization, $url, $body)
53128
{
54129
$authorization = 'QBox ' . $this->signRequest($url, $body, $contentType);

tests/Qiniu/Tests/AuthTest.php

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ function time()
1212

1313
namespace Qiniu\Tests {
1414
use Qiniu\Auth;
15+
use Qiniu\Http\Header;
1516

1617
// @codingStandardsIgnoreEnd
1718

@@ -67,5 +68,144 @@ public function testUploadToken()
6768
public function testVerifyCallback()
6869
{
6970
}
71+
72+
public function testSignQiniuAuthorization()
73+
{
74+
$auth = new Auth("ak", "sk");
75+
76+
// ---
77+
$url = "";
78+
$method = "";
79+
$headers = new Header(array(
80+
"X-Qiniu-" => array("a"),
81+
"X-Qiniu" => array("b"),
82+
"Content-Type" => array("application/x-www-form-urlencoded"),
83+
));
84+
$body = "{\"name\": \"test\"}";
85+
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
86+
$this->assertNull($err);
87+
$this->assertEquals("ak:0i1vKClRDWFyNkcTFzwcE7PzX74=", $sign);
88+
89+
// ---
90+
$url = "";
91+
$method = "";
92+
$headers = new Header(array(
93+
"Content-Type" => array("application/json"),
94+
));
95+
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
96+
$this->assertNull($err);
97+
$this->assertEquals("ak:K1DI0goT05yhGizDFE5FiPJxAj4=", $sign);
98+
99+
// ---
100+
$url = "";
101+
$method = "GET";
102+
$headers = new Header(array(
103+
"X-Qiniu-" => array("a"),
104+
"X-Qiniu" => array("b"),
105+
"Content-Type" => array("application/x-www-form-urlencoded"),
106+
));
107+
$body = "{\"name\": \"test\"}";
108+
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
109+
$this->assertNull($err);
110+
$this->assertEquals("ak:0i1vKClRDWFyNkcTFzwcE7PzX74=", $sign);
111+
112+
// ---
113+
$url = "";
114+
$method = "POST";
115+
$headers = new Header(array(
116+
"Content-Type" => array("application/json"),
117+
"X-Qiniu" => array("b"),
118+
));
119+
$body = "{\"name\": \"test\"}";
120+
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
121+
$this->assertNull($err);
122+
$this->assertEquals("ak:0ujEjW_vLRZxebsveBgqa3JyQ-w=", $sign);
123+
124+
// ---
125+
$url = "http://upload.qiniup.com";
126+
$method = "";
127+
$headers = new Header(array(
128+
"X-Qiniu-" => array("a"),
129+
"X-Qiniu" => array("b"),
130+
"Content-Type" => array("application/x-www-form-urlencoded"),
131+
));
132+
$body = "{\"name\": \"test\"}";
133+
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
134+
$this->assertNull($err);
135+
$this->assertEquals("ak:GShw5NitGmd5TLoo38nDkGUofRw=", $sign);
136+
137+
// ---
138+
$url = "http://upload.qiniup.com";
139+
$method = "";
140+
$headers = new Header(array(
141+
"Content-Type" => array("application/json"),
142+
"X-Qiniu-Bbb" => array("BBB", "AAA"),
143+
"X-Qiniu-Aaa" => array("DDD", "CCC"),
144+
"X-Qiniu-" => array("a"),
145+
"X-Qiniu" => array("b"),
146+
));
147+
$body = "{\"name\": \"test\"}";
148+
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
149+
$this->assertNull($err);
150+
$this->assertEquals("ak:DhNA1UCaBqSHCsQjMOLRfVn63GQ=", $sign);
151+
152+
// ---
153+
$url = "http://upload.qiniup.com";
154+
$method = "";
155+
$headers = new Header(array(
156+
"Content-Type" => array("application/x-www-form-urlencoded"),
157+
"X-Qiniu-Bbb" => array("BBB", "AAA"),
158+
"X-Qiniu-Aaa" => array("DDD", "CCC"),
159+
"X-Qiniu-" => array("a"),
160+
"X-Qiniu" => array("b"),
161+
));
162+
$body = "name=test&language=go";
163+
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
164+
$this->assertNull($err);
165+
$this->assertEquals("ak:KUAhrYh32P9bv0COD8ugZjDCmII=", $sign);
166+
167+
// ---
168+
$url = "http://upload.qiniup.com";
169+
$method = "";
170+
$headers = new Header(array(
171+
"Content-Type" => array("application/x-www"),
172+
"Content-Type" => array("application/x-www-form-urlencoded"),
173+
"X-Qiniu-Bbb" => array("BBB", "AAA"),
174+
"X-Qiniu-Aaa" => array("DDD", "CCC"),
175+
));
176+
$body = "name=test&language=go";
177+
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
178+
$this->assertNull($err);
179+
$this->assertEquals("ak:KUAhrYh32P9bv0COD8ugZjDCmII=", $sign);
180+
181+
// ---
182+
$url = "http://upload.qiniup.com/mkfile/sdf.jpg";
183+
$method = "";
184+
$headers = new Header(array(
185+
"Content-Type" => array("application/x-www-form-urlencoded"),
186+
"X-Qiniu-Bbb" => array("BBB", "AAA"),
187+
"X-Qiniu-Aaa" => array("DDD", "CCC"),
188+
"X-Qiniu-" => array("a"),
189+
"X-Qiniu" => array("b"),
190+
));
191+
$body = "name=test&language=go";
192+
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
193+
$this->assertNull($err);
194+
$this->assertEquals("ak:fkRck5_LeyfwdkyyLk-hyNwGKac=", $sign);
195+
196+
$url = "http://upload.qiniup.com/mkfile/sdf.jpg?s=er3&df";
197+
$method = "";
198+
$headers = new Header(array(
199+
"Content-Type" => array("application/x-www-form-urlencoded"),
200+
"X-Qiniu-Bbb" => array("BBB", "AAA"),
201+
"X-Qiniu-Aaa" => array("DDD", "CCC"),
202+
"X-Qiniu-" => array("a"),
203+
"X-Qiniu" => array("b"),
204+
));
205+
$body = "name=test&language=go";
206+
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
207+
$this->assertNull($err);
208+
$this->assertEquals("ak:PUFPWsEUIpk_dzUvvxTTmwhp3p4=", $sign);
209+
}
70210
}
71211
}

0 commit comments

Comments
 (0)