@@ -58,14 +58,6 @@ def escape(s):
58
58
return urllib .quote (s , safe = '~' )
59
59
60
60
61
- def _utf8_str (s ):
62
- """Convert unicode to utf-8."""
63
- if isinstance (s , unicode ):
64
- return s .encode ("utf-8" )
65
- else :
66
- return str (s )
67
-
68
-
69
61
def generate_timestamp ():
70
62
"""Get seconds since epoch (UTC)."""
71
63
return int (time .time ())
@@ -280,15 +272,15 @@ def get_nonoauth_parameters(self):
280
272
281
273
def to_header (self , realm = '' ):
282
274
"""Serialize as a header for an HTTPAuth request."""
283
- oauth_params = ((k , v ) for k , v in self .iteritems ()
275
+ oauth_params = ((k , v ) for k , v in self .items ()
284
276
if k .startswith ('oauth_' ))
285
277
stringy_params = ((k , escape (str (v ))) for k , v in oauth_params )
286
278
header_params = ('%s="%s"' % (k , v ) for k , v in stringy_params )
287
279
params_header = ', ' .join (header_params )
288
280
289
281
auth_header = 'OAuth realm="%s"' % realm
290
282
if params_header :
291
- auth_header += params_header
283
+ auth_header = "%s, %s" % ( auth_header , params_header )
292
284
293
285
return {'Authorization' : auth_header }
294
286
@@ -306,7 +298,7 @@ def get_normalized_parameters(self):
306
298
return urllib .urlencode (sorted (items ))
307
299
308
300
def sign_request (self , signature_method , consumer , token ):
309
- """Set the signature parameter to the result of build_signature ."""
301
+ """Set the signature parameter to the result of sign ."""
310
302
self ['oauth_signature_method' ] = signature_method .name
311
303
self ['oauth_signature' ] = signature_method .sign (self , consumer , token )
312
304
@@ -338,7 +330,7 @@ def from_request(cls, http_method, http_url, headers=None, parameters=None,
338
330
header_params = cls ._split_header (auth_header )
339
331
parameters .update (header_params )
340
332
except :
341
- raise OAuthError ('Unable to parse OAuth parameters from '
333
+ raise Error ('Unable to parse OAuth parameters from '
342
334
'Authorization header.' )
343
335
344
336
# GET or POST query string.
@@ -375,12 +367,12 @@ def from_consumer_and_token(cls, oauth_consumer, token=None,
375
367
if token :
376
368
parameters ['oauth_token' ] = token .key
377
369
378
- return OAuthRequest (http_method , http_url , parameters )
370
+ return Request (http_method , http_url , parameters )
379
371
380
372
@classmethod
381
373
def from_token_and_callback (cls , token , callback = None ,
382
- http_method = HTTP_METHOD ,
383
- http_url = None , parameters = None ):
374
+ http_method = HTTP_METHOD , http_url = None , parameters = None ):
375
+
384
376
if not parameters :
385
377
parameters = {}
386
378
@@ -448,7 +440,7 @@ def get_data_store(self):
448
440
return self .data_store
449
441
450
442
def add_signature_method (self , signature_method ):
451
- self .signature_methods [signature_method .get_name () ] = signature_method
443
+ self .signature_methods [signature_method .name ] = signature_method
452
444
return self .signature_methods
453
445
454
446
def fetch_request_token (self , oauth_request ):
@@ -573,13 +565,13 @@ def _check_signature(self, oauth_request, consumer, token):
573
565
token , signature )
574
566
575
567
if not valid_sig :
576
- key , base = signature_method .build_signature_base_string (
568
+ key , base = signature_method .signing_base (
577
569
oauth_request , consumer , token )
578
570
579
571
raise Error ('Invalid signature. Expected signature base '
580
572
'string: %s' % base )
581
573
582
- built = signature_method .build_signature (oauth_request ,
574
+ built = signature_method .sign (oauth_request ,
583
575
consumer , token )
584
576
585
577
def _check_timestamp (self , timestamp ):
@@ -672,34 +664,45 @@ class SignatureMethod(object):
672
664
provide a new way to sign requests.
673
665
"""
674
666
675
- def get_name (self ):
676
- """-> str."""
677
- raise NotImplementedError
667
+ def signing_base (self , request , consumer , token ):
668
+ """Calculates the string that needs to be signed.
669
+
670
+ This method returns a 2-tuple containing the starting key for the
671
+ signing and the message to be signed. The latter may be used in error
672
+ messages to help clients debug their software.
678
673
679
- def build_signature_base_string (self , oauth_request ,
680
- oauth_consumer , oauth_token ):
681
- """-> str key, str raw."""
674
+ """
682
675
raise NotImplementedError
683
676
684
- def build_signature (self , oauth_request , oauth_consumer , oauth_token ):
685
- """-> str."""
677
+ def sign (self , request , consumer , token ):
678
+ """Returns the signature for the given request, based on the consumer
679
+ and token also provided.
680
+
681
+ You should use your implementation of `signing_base()` to build the
682
+ message to sign. Otherwise it may be less useful for debugging.
683
+
684
+ """
686
685
raise NotImplementedError
687
686
688
- def check_signature (self , oauth_request , consumer , token , signature ):
689
- built = self .build_signature (oauth_request , consumer , token )
687
+ def check (self , request , consumer , token , signature ):
688
+ """Returns whether the given signature is the correct signature for
689
+ the given consumer and token signing the given request."""
690
+ built = self .sign (request , consumer , token )
690
691
return built == signature
691
692
693
+ build_signature_base_string = signing_base
694
+ build_signature = sign
695
+ check_signature = check
696
+
692
697
693
698
class SignatureMethod_HMAC_SHA1 (SignatureMethod ):
694
-
695
- def get_name (self ):
696
- return 'HMAC-SHA1'
699
+ name = 'HMAC-SHA1'
697
700
698
- def build_signature_base_string (self , oauth_request , consumer , token ):
701
+ def signing_base (self , request , consumer , token ):
699
702
sig = (
700
- escape (oauth_request . get_normalized_http_method () ),
701
- escape (oauth_request . get_normalized_http_url () ),
702
- escape (oauth_request .get_normalized_parameters ()),
703
+ escape (request . method ),
704
+ escape (request . url ),
705
+ escape (request .get_normalized_parameters ()),
703
706
)
704
707
705
708
key = '%s&' % escape (consumer .secret )
@@ -708,10 +711,9 @@ def build_signature_base_string(self, oauth_request, consumer, token):
708
711
raw = '&' .join (sig )
709
712
return key , raw
710
713
711
- def build_signature (self , oauth_request , consumer , token ):
714
+ def sign (self , request , consumer , token ):
712
715
"""Builds the base signature string."""
713
- key , raw = self .build_signature_base_string (oauth_request , consumer ,
714
- token )
716
+ key , raw = self .signing_base (request , consumer , token )
715
717
716
718
# HMAC object.
717
719
try :
@@ -724,23 +726,21 @@ def build_signature(self, oauth_request, consumer, token):
724
726
# Calculate the digest base 64.
725
727
return binascii .b2a_base64 (hashed .digest ())[:- 1 ]
726
728
727
-
728
729
class SignatureMethod_PLAINTEXT (SignatureMethod ):
729
730
730
- def get_name (self ):
731
- return 'PLAINTEXT'
731
+ name = 'PLAINTEXT'
732
732
733
- def build_signature_base_string (self , oauth_request , consumer , token ):
734
- """Concatenates the consumer key and secret."""
733
+ def signing_base (self , request , consumer , token ):
734
+ """Concatenates the consumer key and secret with the token's
735
+ secret."""
735
736
sig = '%s&' % escape (consumer .secret )
736
737
if token :
737
738
sig = sig + escape (token .secret )
738
739
return sig , sig
739
740
740
- def build_signature (self , oauth_request , consumer , token ):
741
- key , raw = self .build_signature_base_string (oauth_request , consumer ,
742
- token )
743
- return key
741
+ def sign (self , request , consumer , token ):
742
+ key , raw = self .signing_base (request , consumer , token )
743
+ return raw
744
744
745
745
# Backwards compatibility
746
746
OAuthError = Error
0 commit comments