@@ -68,13 +68,17 @@ def sign_request(self, method, resource, params, headers, body, compute_content_
6868 headers ['x-log-signaturemethod' ] = 'hmac-sha1'
6969 headers ['Date' ] = self ._getGMT ()
7070
71+ content_md5 = None
72+ # we don't need content-md5 in signature if compute_content_hash is False
7173 if body and compute_content_hash :
72- headers ['Content-MD5' ] = Util .cal_md5 (body )
74+ content_md5 = Util .cal_md5 (body )
75+ headers ['Content-MD5' ] = content_md5
76+
7377 if not credentials .get_access_key_secret ():
7478 return six .b ('' )
7579 content = method + '\n '
76- if 'Content-MD5' in headers :
77- content += headers [ 'Content-MD5' ]
80+ if content_md5 is not None :
81+ content += content_md5
7882 content += '\n '
7983 if 'Content-Type' in headers :
8084 content += headers ['Content-Type' ]
@@ -87,31 +91,39 @@ def sign_request(self, method, resource, params, headers, body, compute_content_
8791 headers ['x-log-date' ] = headers ['Date' ] # bypass some proxy doesn't allow "Date" in header issue.
8892
8993
94+ _EMPTY_CONTENT_SHA256 = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
95+
9096class AuthV4 (AuthBase ):
9197 def __init__ (self , credentials_provider , region ):
9298 AuthBase .__init__ (self , credentials_provider )
9399 self ._region = region
94100
95101 def sign_request (self , method , resource , params , headers , body , compute_content_hash = True ):
96102 current_time = datetime .utcnow ().strftime ("%Y%m%dT%H%M%SZ" )
97- headers ['Authorization' ] = self ._do_sign_request (method , resource , params , headers , body , current_time )
103+ headers ['Authorization' ] = self ._do_sign_request (method , resource , params , headers , body , current_time , compute_content_hash = compute_content_hash )
98104
99- def _do_sign_request (self , method , resource , params , headers , body , current_time ):
105+ def _do_sign_request (self , method , resource , params , headers , body , current_time , compute_content_hash = True ):
100106 credentials = self .credentials_provider .get_credentials ()
101107
102108 if credentials .get_security_token ():
103109 headers ['x-acs-security-token' ] = credentials .get_security_token ()
104110
105- content_sha256 = sha256 (body ).hexdigest () \
106- if body else 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
111+ content_sha256 = _EMPTY_CONTENT_SHA256
112+ if compute_content_hash and body :
113+ content_sha256 = sha256 (body ).hexdigest ()
114+
107115 headers ['x-log-content-sha256' ] = content_sha256
108116 headers ['x-log-date' ] = current_time
109117 current_date = current_time [:8 ]
110118 canonical_headers = {}
111119 signed_headers = ''
112120 for original_key , value in headers .items ():
113121 key = original_key .lower ()
114- if key == 'content-type' or key == 'host' or key .startswith ('x-log-' ) or key .startswith ('x-acs-' ):
122+ if (
123+ key == "content-type"
124+ or key == "host"
125+ or Util ._is_extra_sign_header (key )
126+ ):
115127 canonical_headers [key ] = value
116128 headers_to_string = ''
117129 for key , value in sorted (canonical_headers .items ()):
0 commit comments