Skip to content

Commit f6c668e

Browse files
authored
Merge pull request #417 from qiniu/features/add-x-qiniu-date
add X-Qiniu-Date
2 parents 0767741 + c4cdd53 commit f6c668e

File tree

5 files changed

+61
-6
lines changed

5 files changed

+61
-6
lines changed

.github/workflows/ci-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- name: Install dependencies
2222
run: |
2323
python -m pip install --upgrade pip
24-
pip install flake8 pytest pytest-cov requests scrutinizer-ocular codecov
24+
pip install flake8 pytest pytest-cov freezegun requests scrutinizer-ocular codecov
2525
- name: Run cases
2626
env:
2727
QINIU_ACCESS_KEY: ${{ secrets.QINIU_ACCESS_KEY }}

qiniu/auth.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ class Auth(object):
4949
__secret_key: 账号密钥对重的secretKey,详见 https://portal.qiniu.com/user/key
5050
"""
5151

52-
def __init__(self, access_key, secret_key):
52+
def __init__(self, access_key, secret_key, disable_qiniu_timestamp_signature=None):
5353
"""初始化Auth类"""
5454
self.__checkKey(access_key, secret_key)
5555
self.__access_key = access_key
5656
self.__secret_key = b(secret_key)
57+
self.disable_qiniu_timestamp_signature = disable_qiniu_timestamp_signature
5758

5859
def get_access_key(self):
5960
return self.__access_key
@@ -229,11 +230,12 @@ class QiniuMacAuth(object):
229230
http://kirk-docs.qiniu.com/apidocs/#TOC_325b437b89e8465e62e958cccc25c63f
230231
"""
231232

232-
def __init__(self, access_key, secret_key):
233+
def __init__(self, access_key, secret_key, disable_qiniu_timestamp_signature=None):
233234
self.qiniu_header_prefix = "X-Qiniu-"
234235
self.__checkKey(access_key, secret_key)
235236
self.__access_key = access_key
236237
self.__secret_key = b(secret_key)
238+
self.disable_qiniu_timestamp_signature = disable_qiniu_timestamp_signature
237239

238240
def __token(self, data):
239241
data = b(data)

qiniu/http.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# -*- coding: utf-8 -*-
22
import logging
3+
import os
34
import platform
5+
from datetime import datetime
46

57
import requests
68
from requests.auth import AuthBase
@@ -20,6 +22,19 @@
2022
_headers = {'User-Agent': USER_AGENT}
2123

2224

25+
def __add_auth_headers(headers, auth):
26+
x_qiniu_date = datetime.utcnow().strftime('%Y%m%dT%H%M%SZ')
27+
if auth.disable_qiniu_timestamp_signature is not None:
28+
if not auth.disable_qiniu_timestamp_signature:
29+
headers['X-Qiniu-Date'] = x_qiniu_date
30+
elif os.getenv('DISABLE_QINIU_TIMESTAMP_SIGNATURE'):
31+
if os.getenv('DISABLE_QINIU_TIMESTAMP_SIGNATURE').lower() != 'true':
32+
headers['X-Qiniu-Date'] = x_qiniu_date
33+
else:
34+
headers['X-Qiniu-Date'] = x_qiniu_date
35+
return headers
36+
37+
2338
def __return_wrapper(resp):
2439
if resp.status_code != 200 or resp.headers.get('X-Reqid') is None:
2540
return None, ResponseInfo(resp)
@@ -155,14 +170,18 @@ def _post_with_qiniu_mac(url, data, auth):
155170
qn_auth = qiniu.auth.QiniuMacRequestsAuth(
156171
auth
157172
) if auth is not None else None
158-
return _post(url, data, None, qn_auth)
173+
headers = __add_auth_headers({}, auth)
174+
175+
return _post(url, data, None, qn_auth, headers=headers)
159176

160177

161178
def _get_with_qiniu_mac(url, params, auth):
162179
qn_auth = qiniu.auth.QiniuMacRequestsAuth(
163180
auth
164181
) if auth is not None else None
165-
return _get(url, params, qn_auth)
182+
headers = __add_auth_headers({}, auth)
183+
184+
return _get(url, params, qn_auth, headers=headers)
166185

167186

168187
def _get_with_qiniu_mac_and_headers(url, params, auth, headers):

qiniu/services/storage/bucket.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ class BucketManager(object):
1717

1818
def __init__(self, auth, zone=None):
1919
self.auth = auth
20-
self.mac_auth = QiniuMacAuth(auth.get_access_key(), auth.get_secret_key())
20+
self.mac_auth = QiniuMacAuth(
21+
auth.get_access_key(),
22+
auth.get_secret_key(),
23+
auth.disable_qiniu_timestamp_signature)
2124
if (zone is None):
2225
self.zone = config.get_default('default_zone')
2326
else:

test_qiniu.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import unittest
1212
import pytest
13+
from freezegun import freeze_time
1314

1415
from qiniu import Auth, set_default, etag, PersistentFop, build_op, op_save, Zone, QiniuMacAuth
1516
from qiniu import put_data, put_file, put_stream
@@ -494,6 +495,36 @@ def test_set_object_lifecycle_with_cond(self):
494495
)
495496
assert info.status_code == 200
496497

498+
@freeze_time("1970-01-01")
499+
def test_invalid_x_qiniu_date(self):
500+
ret, info = self.bucket.stat(bucket_name, 'python-sdk.html')
501+
assert ret is None
502+
assert info.status_code == 403
503+
504+
@freeze_time("1970-01-01")
505+
def test_invalid_x_qiniu_date_with_disable_date_sign(self):
506+
q = Auth(access_key, secret_key, disable_qiniu_timestamp_signature=True)
507+
bucket = BucketManager(q)
508+
ret, info = bucket.stat(bucket_name, 'python-sdk.html')
509+
assert 'hash' in ret
510+
511+
@freeze_time("1970-01-01")
512+
def test_invalid_x_qiniu_date_env(self):
513+
os.environ['DISABLE_QINIU_TIMESTAMP_SIGNATURE'] = 'True'
514+
ret, info = self.bucket.stat(bucket_name, 'python-sdk.html')
515+
os.unsetenv('DISABLE_QINIU_TIMESTAMP_SIGNATURE')
516+
assert 'hash' in ret
517+
518+
@freeze_time("1970-01-01")
519+
def test_invalid_x_qiniu_date_env_be_ignored(self):
520+
os.environ['DISABLE_QINIU_TIMESTAMP_SIGNATURE'] = 'True'
521+
q = Auth(access_key, secret_key, disable_qiniu_timestamp_signature=False)
522+
bucket = BucketManager(q)
523+
ret, info = bucket.stat(bucket_name, 'python-sdk.html')
524+
os.unsetenv('DISABLE_QINIU_TIMESTAMP_SIGNATURE')
525+
assert ret is None
526+
assert info.status_code == 403
527+
497528

498529
class UploaderTestCase(unittest.TestCase):
499530
mime_type = "text/plain"

0 commit comments

Comments
 (0)