<s> '''<STR_LIT>''' <EOL> import base64 <EOL> import hmac <EOL> from hashlib import sha1 <EOL> from rauth . compat import is_basestring , quote , urlencode , urlsplit , urlunsplit <EOL> from rauth . utils import FORM_URLENCODED <EOL> class SignatureMethod ( object ) : <EOL> '''<STR_LIT>''' <EOL> def _ensure_unicode ( self , s ) : <EOL> if not isinstance ( s , bytes ) : <EOL> return s . encode ( '<STR_LIT:utf-8>' ) <EOL> return s . decode ( '<STR_LIT:utf-8>' ) <EOL> def _escape ( self , s ) : <EOL> '''<STR_LIT>''' <EOL> return quote ( self . _ensure_unicode ( s ) , safe = '<STR_LIT>' ) . encode ( '<STR_LIT:utf-8>' ) <EOL> def _remove_qs ( self , url ) : <EOL> '''<STR_LIT>''' <EOL> scheme , netloc , path , query , fragment = urlsplit ( url ) <EOL> return urlunsplit ( ( scheme , netloc , path , '<STR_LIT>' , fragment ) ) <EOL> def _normalize_request_parameters ( self , oauth_params , req_kwargs ) : <EOL> '''<STR_LIT>''' <EOL> normalized = [ ] <EOL> params = req_kwargs . get ( '<STR_LIT>' , { } ) <EOL> data = req_kwargs . get ( '<STR_LIT:data>' , { } ) <EOL> headers = req_kwargs . get ( '<STR_LIT>' , { } ) <EOL> for k , v in params . items ( ) : <EOL> if v is not None : <EOL> normalized += [ ( k , v ) ] <EOL> if '<STR_LIT:Content-Type>' in headers and headers [ '<STR_LIT:Content-Type>' ] == FORM_URLENCODED : <EOL> for k , v in data . items ( ) : <EOL> normalized += [ ( k , v ) ] <EOL> all_normalized = [ ] <EOL> for t in normalized : <EOL> k , v = t <EOL> if is_basestring ( v ) and not isinstance ( v , bytes ) : <EOL> v = v . encode ( '<STR_LIT:utf-8>' ) <EOL> all_normalized += [ ( k , v ) ] <EOL> for k , v in oauth_params . items ( ) : <EOL> if ( k , v ) in all_normalized : <EOL> continue <EOL> all_normalized += [ ( k , v ) ] <EOL> all_normalized . sort ( ) <EOL> return urlencode ( all_normalized , True ) . replace ( '<STR_LIT:+>' , '<STR_LIT>' ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class HmacSha1Signature ( SignatureMethod ) : <EOL> '''<STR_LIT>''' <EOL> NAME = '<STR_LIT>' <EOL> def sign ( self , <EOL> consumer_secret , <EOL> access_token_secret , <EOL> method , <EOL> url , <EOL> oauth_params , <EOL> req_kwargs ) : <EOL> '''<STR_LIT>''' <EOL> url = self . _remove_qs ( url ) <EOL> oauth_params = self . _normalize_request_parameters ( oauth_params , req_kwargs ) <EOL> parameters = map ( self . _escape , [ method , url , oauth_params ] ) <EOL> key = self . _escape ( consumer_secret ) + b'<STR_LIT:&>' <EOL> if access_token_secret is not None : <EOL> key += self . _escape ( access_token_secret ) <EOL> signature_base_string = b'<STR_LIT:&>' . join ( parameters ) <EOL> hashed = hmac . new ( key , signature_base_string , sha1 ) <EOL> return base64 . b64encode ( hashed . digest ( ) ) . decode ( ) <EOL> class RsaSha1Signature ( SignatureMethod ) : <EOL> '''<STR_LIT>''' <EOL> NAME = '<STR_LIT>' <EOL> def __init__ ( self ) : <EOL> try : <EOL> from Crypto . PublicKey import RSA as r <EOL> from Crypto . Hash import SHA as s <EOL> from Crypto . Signature import PKCS1_v1_5 as p <EOL> self . RSA , self . SHA , self . PKCS1_v1_5 = r , s , p <EOL> except ImportError : <EOL> raise NotImplementedError ( '<STR_LIT>' + self . NAME ) <EOL> def sign ( self , <EOL> consumer_secret , <EOL> access_token_secret , <EOL> method , <EOL> url , <EOL> oauth_params , <EOL> req_kwargs ) : <EOL> '''<STR_LIT>''' <EOL> url = self . _remove_qs ( url ) <EOL> oauth_params = self . _normalize_request_parameters ( oauth_params , req_kwargs ) <EOL> parameters = map ( self . _escape , [ method , url , oauth_params ] ) <EOL> signature_base_string = b'<STR_LIT:&>' . join ( parameters ) <EOL> if is_basestring ( consumer_secret ) : <EOL> consumer_secret = self . RSA . importKey ( consumer_secret ) <EOL> if not isinstance ( consumer_secret , self . RSA . _RSAobj ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> s = self . PKCS1_v1_5 . new ( consumer_secret ) <EOL> h = self . SHA . new ( signature_base_string ) <EOL> hashed = s . sign ( h ) <EOL> return base64 . b64encode ( hashed ) . decode ( ) <EOL> class PlaintextSignature ( SignatureMethod ) : <EOL> '''<STR_LIT>''' <EOL> NAME = '<STR_LIT>' <EOL> def sign ( self , consumer_secret , access_token_secret , method , url , <EOL> oauth_params , req_kwargs ) : <EOL> '''<STR_LIT>''' <EOL> key = self . _escape ( consumer_secret ) + b'<STR_LIT:&>' <EOL> if access_token_secret : <EOL> key += self . _escape ( access_token_secret ) <EOL> return key . decode ( ) </s>
