Skip to content

Commit 18f9611

Browse files
authored
Merge pull request #384 from qiniu/features/add-x-qiniu-date
add x qiniu date and fix empty header can't iterate
2 parents 0cc46e4 + f98abc8 commit 18f9611

File tree

4 files changed

+78
-6
lines changed

4 files changed

+78
-6
lines changed

src/Qiniu/Auth.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@ final class Auth
88
{
99
private $accessKey;
1010
private $secretKey;
11+
public $options;
1112

12-
public function __construct($accessKey, $secretKey)
13+
public function __construct($accessKey, $secretKey, $options = null)
1314
{
1415
$this->accessKey = $accessKey;
1516
$this->secretKey = $secretKey;
17+
$defaultOptions = array(
18+
'disableQiniuTimestampSignature' => null
19+
);
20+
if ($options == null) {
21+
$options = $defaultOptions;
22+
}
23+
$this->options = array_merge($defaultOptions, $options);
1624
}
1725

1826
public function getAccessKey()
@@ -216,14 +224,29 @@ public function authorization($url, $body = null, $contentType = null)
216224

217225
public function authorizationV2($url, $method, $body = null, $contentType = null)
218226
{
219-
$headers = null;
227+
$headers = new Header();
220228
$result = array();
221229
if ($contentType != null) {
222-
$headers = new Header(array(
223-
'Content-Type' => array($contentType),
224-
));
230+
$headers['Content-Type'] = $contentType;
225231
$result['Content-Type'] = $contentType;
226232
}
233+
234+
$signDate = gmdate('Ymd\THis\Z', time());
235+
if ($this->options['disableQiniuTimestampSignature'] !== null) {
236+
if (!$this->options['disableQiniuTimestampSignature']) {
237+
$headers['X-Qiniu-Date'] = $signDate;
238+
$result['X-Qiniu-Date'] = $signDate;
239+
}
240+
} elseif (getenv("DISABLE_QINIU_TIMESTAMP_SIGNATURE")) {
241+
if (strtolower(getenv("DISABLE_QINIU_TIMESTAMP_SIGNATURE")) !== "true") {
242+
$headers['X-Qiniu-Date'] = $signDate;
243+
$result['X-Qiniu-Date'] = $signDate;
244+
}
245+
} else {
246+
$headers['X-Qiniu-Date'] = $signDate;
247+
$result['X-Qiniu-Date'] = $signDate;
248+
}
249+
227250
list($sign) = $this->signQiniuAuthorization($url, $method, $body, $headers);
228251
$result['Authorization'] = 'Qiniu ' . $sign;
229252
return $result;

src/Qiniu/Http/Header.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class Header implements \ArrayAccess, \IteratorAggregate, \Countable
99
{
1010
/** @var array normalized key name map */
11-
private $data;
11+
private $data = array();
1212

1313
/**
1414
* @param array $obj non-normalized header object

tests/Qiniu/Tests/AuthTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,5 +207,39 @@ public function testSignQiniuAuthorization()
207207
$this->assertNull($err);
208208
$this->assertEquals("ak:PUFPWsEUIpk_dzUvvxTTmwhp3p4=", $sign);
209209
}
210+
211+
public function testDisableQiniuTimestampSignatureDefault()
212+
{
213+
$auth = new Auth("ak", "sk");
214+
$authedHeaders = $auth->authorizationV2("https://example.com", "GET");
215+
$this->assertArrayHasKey("X-Qiniu-Date", $authedHeaders);
216+
}
217+
218+
public function testDisableQiniuTimestampSignature()
219+
{
220+
$auth = new Auth("ak", "sk", array(
221+
"disableQiniuTimestampSignature" => true
222+
));
223+
$authedHeaders = $auth->authorizationV2("https://example.com", "GET");
224+
$this->assertArrayNotHasKey("X-Qiniu-Date", $authedHeaders);
225+
}
226+
public function testDisableQiniuTimestampSignatureEnv()
227+
{
228+
putenv("DISABLE_QINIU_TIMESTAMP_SIGNATURE=true");
229+
$auth = new Auth("ak", "sk");
230+
$authedHeaders = $auth->authorizationV2("https://example.com", "GET");
231+
$this->assertArrayNotHasKey("X-Qiniu-Date", $authedHeaders);
232+
putenv('DISABLE_QINIU_TIMESTAMP_SIGNATURE');
233+
}
234+
public function testDisableQiniuTimestampSignatureEnvBeIgnored()
235+
{
236+
putenv("DISABLE_QINIU_TIMESTAMP_SIGNATURE=true");
237+
$auth = new Auth("ak", "sk", array(
238+
"disableQiniuTimestampSignature" => false
239+
));
240+
$authedHeaders = $auth->authorizationV2("https://example.com", "GET");
241+
$this->assertArrayHasKey("X-Qiniu-Date", $authedHeaders);
242+
putenv('DISABLE_QINIU_TIMESTAMP_SIGNATURE');
243+
}
210244
}
211245
}

tests/Qiniu/Tests/HeaderTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,21 @@ public function testGetIterator()
145145
$this->assertFalse($hasException);
146146
}
147147

148+
public function testEmptyHeaderIterator()
149+
{
150+
$emptyHeader = new Header();
151+
152+
$hasException = false;
153+
try {
154+
foreach ($emptyHeader as $k => $v) {
155+
$hasException = !isset($header[$k]);
156+
}
157+
} catch (\Exception $e) {
158+
$hasException = true;
159+
}
160+
$this->assertFalse($hasException);
161+
}
162+
148163
public function testCount()
149164
{
150165
$header = new Header($this->heads);

0 commit comments

Comments
 (0)