<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . conf import settings <EOL> from rest_framework import exceptions <EOL> from rest_framework . authentication import get_authorization_header , BaseAuthentication <EOL> from . compat import oauth , oauth_provider , oauth_provider_store <EOL> from . compat import oauth2_provider , provider_now , check_nonce <EOL> class OAuthAuthentication ( BaseAuthentication ) : <EOL> """<STR_LIT>""" <EOL> www_authenticate_realm = '<STR_LIT>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( OAuthAuthentication , self ) . __init__ ( * args , ** kwargs ) <EOL> if oauth is None : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if oauth_provider is None : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def authenticate ( self , request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> oauth_request = oauth_provider . utils . get_oauth_request ( request ) <EOL> except oauth . Error as err : <EOL> raise exceptions . AuthenticationFailed ( err . message ) <EOL> if not oauth_request : <EOL> return None <EOL> oauth_params = oauth_provider . consts . OAUTH_PARAMETERS_NAMES <EOL> found = any ( param for param in oauth_params if param in oauth_request ) <EOL> missing = list ( param for param in oauth_params if param not in oauth_request ) <EOL> if not found : <EOL> return None <EOL> if missing : <EOL> msg = '<STR_LIT>' % ( '<STR_LIT:U+002CU+0020>' . join ( missing ) ) <EOL> raise exceptions . AuthenticationFailed ( msg ) <EOL> if not self . check_nonce ( request , oauth_request ) : <EOL> msg = '<STR_LIT>' <EOL> raise exceptions . AuthenticationFailed ( msg ) <EOL> try : <EOL> consumer_key = oauth_request . get_parameter ( '<STR_LIT>' ) <EOL> consumer = oauth_provider_store . get_consumer ( request , oauth_request , consumer_key ) <EOL> except oauth_provider . store . InvalidConsumerError : <EOL> msg = '<STR_LIT>' % oauth_request . get_parameter ( '<STR_LIT>' ) <EOL> raise exceptions . AuthenticationFailed ( msg ) <EOL> if consumer . status != oauth_provider . consts . ACCEPTED : <EOL> msg = '<STR_LIT>' % consumer . get_status_display ( ) <EOL> raise exceptions . AuthenticationFailed ( msg ) <EOL> try : <EOL> token_param = oauth_request . get_parameter ( '<STR_LIT>' ) <EOL> token = oauth_provider_store . get_access_token ( request , oauth_request , consumer , token_param ) <EOL> except oauth_provider . store . InvalidTokenError : <EOL> msg = '<STR_LIT>' % oauth_request . get_parameter ( '<STR_LIT>' ) <EOL> raise exceptions . AuthenticationFailed ( msg ) <EOL> try : <EOL> self . validate_token ( request , consumer , token ) <EOL> except oauth . Error as err : <EOL> raise exceptions . AuthenticationFailed ( err . message ) <EOL> user = token . user <EOL> if not user . is_active : <EOL> msg = '<STR_LIT>' % user . username <EOL> raise exceptions . AuthenticationFailed ( msg ) <EOL> return ( token . user , token ) <EOL> def authenticate_header ( self , request ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % self . www_authenticate_realm <EOL> def validate_token ( self , request , consumer , token ) : <EOL> """<STR_LIT>""" <EOL> oauth_server , oauth_request = oauth_provider . utils . initialize_server_request ( request ) <EOL> oauth_server . verify_request ( oauth_request , consumer , token ) <EOL> def check_nonce ( self , request , oauth_request ) : <EOL> """<STR_LIT>""" <EOL> oauth_nonce = oauth_request [ '<STR_LIT>' ] <EOL> oauth_timestamp = oauth_request [ '<STR_LIT>' ] <EOL> return check_nonce ( request , oauth_request , oauth_nonce , oauth_timestamp ) <EOL> class OAuth2Authentication ( BaseAuthentication ) : <EOL> """<STR_LIT>""" <EOL> www_authenticate_realm = '<STR_LIT>' <EOL> allow_query_params_token = settings . DEBUG <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( OAuth2Authentication , self ) . __init__ ( * args , ** kwargs ) <EOL> if oauth2_provider is None : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def authenticate ( self , request ) : <EOL> """<STR_LIT>""" <EOL> auth = get_authorization_header ( request ) . split ( ) <EOL> if len ( auth ) == <NUM_LIT:1> : <EOL> msg = '<STR_LIT>' <EOL> raise exceptions . AuthenticationFailed ( msg ) <EOL> elif len ( auth ) > <NUM_LIT:2> : <EOL> msg = '<STR_LIT>' <EOL> raise exceptions . AuthenticationFailed ( msg ) <EOL> if auth and auth [ <NUM_LIT:0> ] . lower ( ) == b'<STR_LIT>' : <EOL> access_token = auth [ <NUM_LIT:1> ] <EOL> elif '<STR_LIT>' in request . POST : <EOL> access_token = request . POST [ '<STR_LIT>' ] <EOL> elif '<STR_LIT>' in request . GET and self . allow_query_params_token : <EOL> access_token = request . GET [ '<STR_LIT>' ] <EOL> else : <EOL> return None <EOL> return self . authenticate_credentials ( request , access_token ) <EOL> def authenticate_credentials ( self , request , access_token ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> token = oauth2_provider . oauth2 . models . AccessToken . objects . select_related ( '<STR_LIT:user>' ) <EOL> token = token . get ( token = access_token , expires__gt = provider_now ( ) ) <EOL> except oauth2_provider . oauth2 . models . AccessToken . DoesNotExist : <EOL> raise exceptions . AuthenticationFailed ( '<STR_LIT>' ) <EOL> user = token . user <EOL> if not user . is_active : <EOL> msg = '<STR_LIT>' % user . get_username ( ) <EOL> raise exceptions . AuthenticationFailed ( msg ) <EOL> return ( user , token ) <EOL> def authenticate_header ( self , request ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % self . www_authenticate_realm </s>
