<s> from django . core . exceptions import ImproperlyConfigured <EOL> from django . conf import settings <EOL> from django . utils . importlib import import_module <EOL> try : <EOL> from restkit import oauth2 <EOL> except ImportError : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> from webmachine . auth . base import Auth <EOL> from webmachine . util . const import TOKEN_REQUEST , TOKEN_ACCESS <EOL> def load_oauth_datastore ( ) : <EOL> datastore = getattr ( settings , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> i = datastore . rfind ( '<STR_LIT:.>' ) <EOL> module , clsname = datastore [ : i ] , datastore [ i + <NUM_LIT:1> : ] <EOL> try : <EOL> mod = import_module ( module ) <EOL> except ImportError : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" % module ) <EOL> try : <EOL> cls = getattr ( mod , clsname ) <EOL> except AttributeError : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" % ( clsname , module ) ) <EOL> return cls <EOL> class OAuthServer ( oauth2 . Server ) : <EOL> def __init__ ( self , datastore ) : <EOL> self . datastore = datastore <EOL> super ( OAuthServer , self ) . __init__ ( ) <EOL> def fetch_request_token ( self , oauth_request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> token = self . _get_token ( oauth_request , TOKEN_REQUEST ) <EOL> except oauth2 . Error : <EOL> timestamp = self . _get_timestamp ( oauth_request ) <EOL> version = self . _get_version ( oauth_request ) <EOL> consumer = self . _get_consumer ( oauth_request ) <EOL> try : <EOL> callback = self . get_callback ( oauth_request ) <EOL> except oauth2 . Error : <EOL> callback = None <EOL> self . _check_signature ( oauth_request , consumer , None ) <EOL> token = self . datastore . fetch_request_token ( consumer , <EOL> callback , timestamp ) <EOL> return token <EOL> def fetch_access_token ( self , oauth_request ) : <EOL> """<STR_LIT>""" <EOL> timestamp = self . _get_timestamp ( oauth_request ) <EOL> version = self . _get_version ( oauth_request ) <EOL> consumer = self . _get_consumer ( oauth_request ) <EOL> try : <EOL> verifier = self . _get_verifier ( oauth_request ) <EOL> except oauth2 . Error : <EOL> verifier = None <EOL> token = self . _get_token ( oauth_request , TOKEN_REQUEST ) <EOL> self . _check_signature ( oauth_request , consumer , token ) <EOL> new_token = self . datastore . fetch_access_token ( consumer , token , <EOL> verifier , timestamp ) <EOL> return new_token <EOL> def verify_request ( self , oauth_request ) : <EOL> consumer = self . _get_consumer ( oauth_request ) <EOL> token = self . _get_token ( oauth_request , TOKEN_ACCESS ) <EOL> parameters = super ( OAuthServer , self ) . verify_request ( oauth_request , <EOL> consumer , token ) <EOL> return consumer , token , parameters <EOL> def authorize_token ( self , token , user ) : <EOL> """<STR_LIT>""" <EOL> return self . datastore . authorize_request_token ( token , user ) <EOL> def get_callback ( self , oauth_request ) : <EOL> """<STR_LIT>""" <EOL> return oauth_request . get_parameter ( '<STR_LIT>' ) <EOL> def _get_consumer ( self , oauth_request ) : <EOL> consumer_key = oauth_request . get_parameter ( '<STR_LIT>' ) <EOL> consumer = self . datastore . lookup_consumer ( consumer_key ) <EOL> if not consumer : <EOL> raise oauth2 . Error ( '<STR_LIT>' ) <EOL> return consumer <EOL> def _get_token ( self , oauth_request , token_type = TOKEN_ACCESS ) : <EOL> """<STR_LIT>""" <EOL> token_field = oauth_request . get_parameter ( '<STR_LIT>' ) <EOL> token = self . datastore . lookup_token ( token_type , token_field ) <EOL> if not token : <EOL> raise oauth2 . Error ( '<STR_LIT>' % ( token_type , token_field ) ) <EOL> return token <EOL> def _check_nonce ( self , consumer , token , nonce ) : <EOL> """<STR_LIT>""" <EOL> nonce = self . datastore . lookup_nonce ( consumer , token , nonce ) <EOL> if nonce : <EOL> raise oauth2 . Error ( '<STR_LIT>' % str ( nonce ) ) <EOL> def _get_timestamp ( self , oauth_request ) : <EOL> return int ( oauth_request . get_parameter ( '<STR_LIT>' ) ) <EOL> class Oauth ( Auth ) : <EOL> def __init__ ( self , realm = "<STR_LIT>" ) : <EOL> oauth_datastore = load_oauth_datastore ( ) <EOL> self . realm = realm <EOL> self . oauth_server = OAuthServer ( oauth_datastore ( ) ) <EOL> self . oauth_server . add_signature_method ( oauth2 . SignatureMethod_PLAINTEXT ( ) ) <EOL> self . oauth_server . add_signature_method ( oauth2 . SignatureMethod_HMAC_SHA1 ( ) ) <EOL> def authorized ( self , req , resp ) : <EOL> params = { } <EOL> headers = { } <EOL> if req . method == "<STR_LIT:POST>" : <EOL> params = req . REQUEST . items ( ) <EOL> if '<STR_LIT>' in req . META : <EOL> headers [ '<STR_LIT>' ] = req . META . get ( '<STR_LIT>' ) <EOL> oauth_request = oauth2 . Request . from_request ( req . method , <EOL> req . build_absolute_uri ( ) , headers = headers , <EOL> parameters = params , <EOL> query_string = req . META . get ( '<STR_LIT>' ) ) <EOL> if not oauth_request : <EOL> return '<STR_LIT>' % self . realm <EOL> try : <EOL> consumer , token , params = self . oauth_server . verify_request ( oauth_request ) <EOL> except oauth2 . Error , err : <EOL> resp . content = str ( err ) <EOL> return '<STR_LIT>' % self . realm <EOL> req . user = consumer . user <EOL> return True </s>
