33
33
from qingcloud .misc .utils import get_utf8_value , get_ts , base64_url_decode ,\
34
34
base64_url_encode
35
35
36
+
36
37
class HmacKeys (object ):
37
38
""" Key based Auth handler helper.
38
39
"""
@@ -77,6 +78,7 @@ def sign_string(self, string_to_sign):
77
78
to_sign = self .digest (string_to_sign )
78
79
return base64 .b64encode (to_sign ).strip ()
79
80
81
+
80
82
class QuerySignatureAuthHandler (HmacKeys ):
81
83
""" Provides Query Signature Authentication.
82
84
"""
@@ -99,7 +101,7 @@ def _calc_signature(self, params, verb, path):
99
101
urllib .quote (val , safe = '-_~' ))
100
102
qs = '&' .join (pairs )
101
103
string_to_sign += qs
102
- #print "string to sign:[%s]" % string_to_sign
104
+ # print "string to sign:[%s]" % string_to_sign
103
105
b64 = self .sign_string (string_to_sign )
104
106
return (qs , b64 )
105
107
@@ -113,14 +115,14 @@ def add_auth(self, req, **kwargs):
113
115
req .params ['time_stamp' ] = time_stamp
114
116
qs , signature = self ._calc_signature (req .params , req .method ,
115
117
req .auth_path )
116
- #print 'query_string: %s Signature: %s' % (qs, signature)
118
+ # print 'query_string: %s Signature: %s' % (qs, signature)
117
119
if req .method == 'POST' :
118
120
# req and retried req should not have signature
119
121
params = req .params .copy ()
120
122
params ["signature" ] = signature
121
123
req .body = urllib .urlencode (params )
122
124
req .header = {
123
- 'Content-Length' : str (len (req .body )),
125
+ 'Content-Length' : str (len (req .body )),
124
126
'Content-Type' : 'application/x-www-form-urlencoded' ,
125
127
'Accept' : 'text/plain' ,
126
128
'Connection' : 'Keep-Alive'
@@ -131,11 +133,13 @@ def add_auth(self, req, **kwargs):
131
133
# already be there, we need to get rid of that and rebuild it
132
134
req .path = req .path .split ('?' )[0 ]
133
135
req .path = (req .path + '?' + qs +
134
- '&signature=' + urllib .quote_plus (signature ))
136
+ '&signature=' + urllib .quote_plus (signature ))
137
+
135
138
136
139
class AppSignatureAuthHandler (QuerySignatureAuthHandler ):
137
140
""" Provides App Signature Authentication.
138
141
"""
142
+
139
143
def __init__ (self , app_id , secret_app_key , access_token = None ):
140
144
141
145
HmacKeys .__init__ (self , "" , app_id , secret_app_key )
@@ -181,14 +185,14 @@ def add_auth(self, req, **kwargs):
181
185
req .params ['time_stamp' ] = time_stamp
182
186
qs , signature = self ._calc_signature (req .params , req .method ,
183
187
req .auth_path )
184
- #print 'query_string: %s Signature: %s' % (qs, signature)
188
+ # print 'query_string: %s Signature: %s' % (qs, signature)
185
189
if req .method == 'POST' :
186
190
# req and retried req should not have signature
187
191
params = req .params .copy ()
188
192
params ["signature" ] = signature
189
193
req .body = urllib .urlencode (params )
190
194
req .header = {
191
- 'Content-Length' : str (len (req .body )),
195
+ 'Content-Length' : str (len (req .body )),
192
196
'Content-Type' : 'application/x-www-form-urlencoded' ,
193
197
'Accept' : 'text/plain' ,
194
198
'Connection' : 'Keep-Alive'
@@ -199,7 +203,8 @@ def add_auth(self, req, **kwargs):
199
203
# already be there, we need to get rid of that and rebuild it
200
204
req .path = req .path .split ('?' )[0 ]
201
205
req .path = (req .path + '?' + qs +
202
- '&signature=' + signature )
206
+ '&signature=' + signature )
207
+
203
208
204
209
class QSSignatureAuthHandler (HmacKeys ):
205
210
@@ -231,7 +236,8 @@ def _generate_signature(self, method, auth_path, params, headers):
231
236
string_to_sign += "\n %s" % date_str
232
237
233
238
# Generate signed headers
234
- signed_headers = filter (lambda x : x .lower ().startswith ("x-qs-" ), headers .keys ())
239
+ signed_headers = filter (
240
+ lambda x : x .lower ().startswith ("x-qs-" ), headers .keys ())
235
241
for param in sorted (signed_headers ):
236
242
string_to_sign += "\n %s:%s" % (param .lower (), headers [param ])
237
243
@@ -275,7 +281,7 @@ def get_auth_parameters(self, method, auth_path, expires, params=None, headers=N
275
281
]
276
282
277
283
signature = self ._generate_signature (method , auth_path ,
278
- params + auth_params ,
284
+ params + auth_params ,
279
285
headers or {})
280
286
281
287
auth_params .append (("X-QS-Signature" , signature ))
0 commit comments