8000 improve code for x-qiniu-date · qiniu/python-sdk@6762bc7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6762bc7

Browse files
committed
improve code for x-qiniu-date
1 parent b7fe6bc commit 6762bc7

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

qiniu/auth.py

Lines changed: 33 additions & 8 deletions
< 8000 td data-grid-cell-id="diff-84938c972358b39a4e567d79a01521d2de32bea78ba6abbe93ba217f85d08f2a-244-251-1" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">251
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# -*- coding: utf-8 -*-
22
import base64
3+
from datetime import datetime
34
import hmac
5+
import os
46
import time
57
from hashlib import sha1
68
from requests.auth import AuthBase
@@ -242,6 +244,14 @@ def __token(self, data):
242244
hashed = hmac.new(self.__secret_key, data, sha1)
243245
return urlsafe_base64_encode(hashed.digest())
244246

247+
@property
248+
def should_sign_with_timestamp(self):
249+
if self.disable_qiniu_timestamp_signature is not None:
250+
return not self.disable_qiniu_timestamp_signature
+
if os.getenv('DISABLE_QINIU_TIMESTAMP_SIGNATURE', '').lower() == 'true':
252+
return False
253+
return True
254+
245255
def token_of_request(
246256
self,
247257
method,
@@ -294,12 +304,12 @@ def token_of_request(
294304
return '{0}:{1}'.format(self.__access_key, self.__token(data))
295305

296306
def qiniu_headers(self, headers):
297-
qiniu_fields = list(filter(
298-
lambda k: k.startswith(self.qiniu_header_prefix) and len(k) > len(self.qiniu_header_prefix),
299-
headers,
300-
))
301-
return "\n".join([
302-
"%s: %s" % (canonical_mime_header_key(key), headers.get(key)) for key in sorted(qiniu_fields)
307+
qiniu_fields = [
308+
key for key in headers
309+
if key.startswith(self.qiniu_header_prefix) and len(key) > len(self.qiniu_header_prefix)
310+
]
311+
return '\n'.join([
312+
'%s: %s' % (canonical_mime_header_key(key), headers.get(key)) for key in sorted(qiniu_fields)
303313
])
304314

305315
@staticmethod
@@ -309,15 +319,30 @@ def __checkKey(access_key, secret_key):
309319

310320

311321
class QiniuMacRequestsAuth(AuthBase):
322+
"""
323+
Attributes:
324+
auth (QiniuMacAuth):
325+
"""
312326
def __init__(self, auth):
327+
"""
328+
Args:
329+
auth (QiniuMacAuth):
330+
"""
313331
self.auth = auth
314332

315333
def __call__(self, r):
316334
if r.headers.get('Content-Type', None) is None:
317335
8000 r.headers['Content-Type'] = 'application/x-www-form-urlencoded'
336+
337+
if self.auth.should_sign_with_timestamp:
338+
x_qiniu_date = datetime.utcnow().strftime('%Y%m%dT%H%M%SZ')
339+
r.headers['X-Qiniu-Date'] = x_qiniu_date
340+
318341
token = self.auth.token_of_request(
319-
r.method, r.headers.get('Host', None),
320-
r.url, self.auth.qiniu_headers(r.headers),
342+
r.method,
343+
r.headers.get('Host', None),
344+
r.url,
345+
self.auth.qiniu_headers(r.headers),
321346
r.headers.get('Content-Type', None),
322347
r.body
323348
)

qiniu/http/__init__.py

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

75
import requests
86
from requests.adapters import HTTPAdapter
@@ -33,19 +31,6 @@
3331
_headers = {'User-Agent': USER_AGENT}
3432

3533

36-
def __add_auth_headers(headers, auth):
37-
x_qiniu_date = datetime.utcnow().strftime('%Y%m%dT%H%M%SZ')
38-
if auth.disable_qiniu_timestamp_signature is not None:
39-
if not auth.disable_qiniu_timestamp_signature:
40-
headers['X-Qiniu-Date'] = x_qiniu_date
41-
elif os.getenv('DISABLE_QINIU_TIMESTAMP_SIGNATURE'):
42-
if os.getenv('DISABLE_QINIU_TIMESTAMP_SIGNATURE').lower() != 'true':
43-
headers['X-Qiniu-Date'] = x_qiniu_date
44-
else:
45-
headers['X-Qiniu-Date'] = x_qiniu_date
46-
return headers
47-
48-
4934
def __return_wrapper(resp):
5035
if resp.status_code != 200 or resp.headers.get('X-Reqid') is None:
5136
return None, ResponseInfo(resp)
@@ -182,18 +167,16 @@ def _post_with_qiniu_mac(url, data, auth):
182167
qn_auth = qiniu.auth.QiniuMacRequestsAuth(
183168
auth
184169
) if auth is not None else None
185-
headers = __add_auth_headers({}, auth)
186170

187-
return _post(url, data, None, qn_auth, headers=headers)
171+
return _post(url, data, None, qn_auth)
188172

189173

190174
def _get_with_qiniu_mac(url, params, auth):
191175
qn_auth = qiniu.auth.QiniuMacRequestsAuth(
192176
auth
193177
) if auth is not None else None
194-
headers = __add_auth_headers({}, auth)
195178

196-
return _get(url, params, qn_auth, headers=headers)
179+
return _get(url, params, qn_auth)
197180

198181

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

0 commit comments

Comments
 (0)
0