Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/Qiniu/Auth.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Qiniu;

use Qiniu\Http\Header;
use Qiniu\Zone;

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

/**
* @param string $urlString
* @param string $method
* @param string $body
* @param null|Header $headers
*/
public function signQiniuAuthorization($urlString, $method = "GET", $body = "", $headers = null)
{
$url = parse_url($urlString);
if (!$url) {
return array(null, new \Exception("parse_url error"));
}

// append method, path and query
if ($method === "") {
$data = "GET ";
} else {
$data = $method . " ";
}
if (isset($url["path"])) {
$data .= $url["path"];
}
if (isset($url["query"])) {
$data .= "?" . $url["query"];
}

// append Host
$data .= "\n";
$data .= "Host: ";
if (isset($url["host"])) {
$data .= $url["host"];
}
if (isset($url["port"]) && $url["port"] > 0) {
$data .= ":" . $url["port"];
}

// try append content type
if ($headers != null && isset($headers["Content-Type"])) {
// append content type
$data .= "\n";
$data .= "Content-Type: " . $headers["Content-Type"];
}

// try append xQiniuHeaders
if ($headers != null) {
$headerLines = array();
$keyPrefix = "X-Qiniu-";
foreach ($headers as $k => $v) {
if (strlen($k) > strlen($keyPrefix) && strpos($k, $keyPrefix) === 0) {
array_push(
$headerLines,
$k . ": " . $v
);
}
}
if (count($headerLines) > 0) {
$data .= "\n";
sort($headerLines);
$data .= implode("\n", $headerLines);
}
}

// append body
$data .= "\n\n";
if (count($body) > 0
&& isset($headers["Content-Type"])
&& $headers["Content-Type"] != "application/octet-stream"
) {
$data .= $body;
}

return array($this->sign(utf8_encode($data)), null);
}

public function verifyCallback($contentType, $originAuthorization, $url, $body)
{
$authorization = 'QBox ' . $this->signRequest($url, $body, $contentType);
Expand Down
140 changes: 140 additions & 0 deletions tests/Qiniu/Tests/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function time()

namespace Qiniu\Tests {
use Qiniu\Auth;
use Qiniu\Http\Header;

// @codingStandardsIgnoreEnd

Expand Down Expand Up @@ -67,5 +68,144 @@ public function testUploadToken()
public function testVerifyCallback()
{
}

public function testSignQiniuAuthorization()
{
$auth = new Auth("ak", "sk");

// ---
$url = "";
$method = "";
$headers = new Header(array(
"X-Qiniu-" => array("a"),
"X-Qiniu" => array("b"),
"Content-Type" => array("application/x-www-form-urlencoded"),
));
$body = "{\"name\": \"test\"}";
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
$this->assertNull($err);
$this->assertEquals("ak:0i1vKClRDWFyNkcTFzwcE7PzX74=", $sign);

// ---
$url = "";
$method = "";
$headers = new Header(array(
"Content-Type" => array("application/json"),
));
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
$this->assertNull($err);
$this->assertEquals("ak:K1DI0goT05yhGizDFE5FiPJxAj4=", $sign);

// ---
$url = "";
$method = "GET";
$headers = new Header(array(
"X-Qiniu-" => array("a"),
"X-Qiniu" => array("b"),
"Content-Type" => array("application/x-www-form-urlencoded"),
));
$body = "{\"name\": \"test\"}";
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
$this->assertNull($err);
$this->assertEquals("ak:0i1vKClRDWFyNkcTFzwcE7PzX74=", $sign);

// ---
$url = "";
$method = "POST";
$headers = new Header(array(
"Content-Type" => array("application/json"),
"X-Qiniu" => array("b"),
));
$body = "{\"name\": \"test\"}";
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
$this->assertNull($err);
$this->assertEquals("ak:0ujEjW_vLRZxebsveBgqa3JyQ-w=", $sign);

// ---
$url = "http://upload.qiniup.com";
$method = "";
$headers = new Header(array(
"X-Qiniu-" => array("a"),
"X-Qiniu" => array("b"),
"Content-Type" => array("application/x-www-form-urlencoded"),
));
$body = "{\"name\": \"test\"}";
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
$this->assertNull($err);
$this->assertEquals("ak:GShw5NitGmd5TLoo38nDkGUofRw=", $sign);

// ---
$url = "http://upload.qiniup.com";
$method = "";
$headers = new Header(array(
"Content-Type" => array("application/json"),
"X-Qiniu-Bbb" => array("BBB", "AAA"),
"X-Qiniu-Aaa" => array("DDD", "CCC"),
"X-Qiniu-" => array("a"),
"X-Qiniu" => array("b"),
));
$body = "{\"name\": \"test\"}";
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
$this->assertNull($err);
$this->assertEquals("ak:DhNA1UCaBqSHCsQjMOLRfVn63GQ=", $sign);

// ---
$url = "http://upload.qiniup.com";
$method = "";
$headers = new Header(array(
"Content-Type" => array("application/x-www-form-urlencoded"),
"X-Qiniu-Bbb" => array("BBB", "AAA"),
"X-Qiniu-Aaa" => array("DDD", "CCC"),
"X-Qiniu-" => array("a"),
"X-Qiniu" => array("b"),
));
$body = "name=test&language=go";
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
$this->assertNull($err);
$this->assertEquals("ak:KUAhrYh32P9bv0COD8ugZjDCmII=", $sign);

// ---
$url = "http://upload.qiniup.com";
$method = "";
$headers = new Header(array(
"Content-Type" => array("application/x-www"),
"Content-Type" => array("application/x-www-form-urlencoded"),
"X-Qiniu-Bbb" => array("BBB", "AAA"),
"X-Qiniu-Aaa" => array("DDD", "CCC"),
));
$body = "name=test&language=go";
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
$this->assertNull($err);
$this->assertEquals("ak:KUAhrYh32P9bv0COD8ugZjDCmII=", $sign);

// ---
$url = "http://upload.qiniup.com/mkfile/sdf.jpg";
$method = "";
$headers = new Header(array(
"Content-Type" => array("application/x-www-form-urlencoded"),
"X-Qiniu-Bbb" => array("BBB", "AAA"),
"X-Qiniu-Aaa" => array("DDD", "CCC"),
"X-Qiniu-" => array("a"),
"X-Qiniu" => array("b"),
));
$body = "name=test&language=go";
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
$this->assertNull($err);
$this->assertEquals("ak:fkRck5_LeyfwdkyyLk-hyNwGKac=", $sign);

$url = "http://upload.qiniup.com/mkfile/sdf.jpg?s=er3&df";
$method = "";
$headers = new Header(array(
"Content-Type" => array("application/x-www-form-urlencoded"),
"X-Qiniu-Bbb" => array("BBB", "AAA"),
"X-Qiniu-Aaa" => array("DDD", "CCC"),
"X-Qiniu-" => array("a"),
"X-Qiniu" => array("b"),
));
$body = "name=test&language=go";
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
$this->assertNull($err);
$this->assertEquals("ak:PUFPWsEUIpk_dzUvvxTTmwhp3p4=", $sign);
}
}
}