<s> import logging <EOL> from django . http import HttpResponse <EOL> from django . views . decorators . debug import sensitive_post_parameters <EOL> from django . views . generic import View , FormView <EOL> from django . utils import timezone <EOL> from django . utils . decorators import method_decorator <EOL> from braces . views import LoginRequiredMixin , CsrfExemptMixin <EOL> from . . settings import oauth2_settings <EOL> from . . exceptions import OAuthToolkitError <EOL> from . . forms import AllowForm <EOL> from . . http import HttpResponseUriRedirect <EOL> from . . models import get_application_model <EOL> from . mixins import OAuthLibMixin <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> class BaseAuthorizationView ( LoginRequiredMixin , OAuthLibMixin , View ) : <EOL> """<STR_LIT>""" <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> self . oauth2_data = { } <EOL> return super ( BaseAuthorizationView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> def error_response ( self , error , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> redirect , error_response = super ( BaseAuthorizationView , self ) . error_response ( error , ** kwargs ) <EOL> if redirect : <EOL> return HttpResponseUriRedirect ( error_response [ '<STR_LIT:url>' ] ) <EOL> status = error_response [ '<STR_LIT:error>' ] . status_code <EOL> return self . render_to_response ( error_response , status = status ) <EOL> class AuthorizationView ( BaseAuthorizationView , FormView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> form_class = AllowForm <EOL> server_class = oauth2_settings . OAUTH2_SERVER_CLASS <EOL> validator_class = oauth2_settings . OAUTH2_VALIDATOR_CLASS <EOL> oauthlib_backend_class = oauth2_settings . OAUTH2_BACKEND_CLASS <EOL> skip_authorization_completely = False <EOL> def get_initial ( self ) : <EOL> scopes = self . oauth2_data . get ( '<STR_LIT>' , self . oauth2_data . get ( '<STR_LIT>' , [ ] ) ) <EOL> initial_data = { <EOL> '<STR_LIT>' : self . oauth2_data . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : '<STR_LIT:U+0020>' . join ( scopes ) , <EOL> '<STR_LIT>' : self . oauth2_data . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT:state>' : self . oauth2_data . get ( '<STR_LIT:state>' , None ) , <EOL> '<STR_LIT>' : self . oauth2_data . get ( '<STR_LIT>' , None ) , <EOL> } <EOL> return initial_data <EOL> def form_valid ( self , form ) : <EOL> try : <EOL> credentials = { <EOL> '<STR_LIT>' : form . cleaned_data . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : form . cleaned_data . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : form . cleaned_data . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT:state>' : form . cleaned_data . get ( '<STR_LIT:state>' , None ) , <EOL> } <EOL> scopes = form . cleaned_data . get ( '<STR_LIT>' ) <EOL> allow = form . cleaned_data . get ( '<STR_LIT>' ) <EOL> uri , headers , body , status = self . create_authorization_response ( <EOL> request = self . request , scopes = scopes , credentials = credentials , allow = allow ) <EOL> self . success_url = uri <EOL> log . debug ( "<STR_LIT>" . format ( self . success_url ) ) <EOL> return HttpResponseUriRedirect ( self . success_url ) <EOL> except OAuthToolkitError as error : <EOL> return self . error_response ( error ) <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> try : <EOL> scopes , credentials = self . validate_authorization_request ( request ) <EOL> kwargs [ '<STR_LIT>' ] = [ oauth2_settings . SCOPES [ scope ] for scope in scopes ] <EOL> kwargs [ '<STR_LIT>' ] = scopes <EOL> application = get_application_model ( ) . objects . get ( client_id = credentials [ '<STR_LIT>' ] ) <EOL> kwargs [ '<STR_LIT>' ] = application <EOL> kwargs . update ( credentials ) <EOL> self . oauth2_data = kwargs <EOL> form = self . get_form ( self . get_form_class ( ) ) <EOL> kwargs [ '<STR_LIT>' ] = form <EOL> require_approval = request . GET . get ( '<STR_LIT>' , oauth2_settings . REQUEST_APPROVAL_PROMPT ) <EOL> if application . skip_authorization : <EOL> uri , headers , body , status = self . create_authorization_response ( <EOL> request = self . request , scopes = "<STR_LIT:U+0020>" . join ( scopes ) , <EOL> credentials = credentials , allow = True ) <EOL> return HttpResponseUriRedirect ( uri ) <EOL> elif require_approval == '<STR_LIT>' : <EOL> tokens = request . user . accesstoken_set . filter ( application = kwargs [ '<STR_LIT>' ] , <EOL> expires__gt = timezone . now ( ) ) . all ( ) <EOL> for token in tokens : <EOL> if token . allow_scopes ( scopes ) : <EOL> uri , headers , body , status = self . create_authorization_response ( <EOL> request = self . request , scopes = "<STR_LIT:U+0020>" . join ( scopes ) , <EOL> credentials = credentials , allow = True ) <EOL> return HttpResponseUriRedirect ( uri ) <EOL> return self . render_to_response ( self . get_context_data ( ** kwargs ) ) <EOL> except OAuthToolkitError as error : <EOL> return self . error_response ( error ) <EOL> class TokenView ( CsrfExemptMixin , OAuthLibMixin , View ) : <EOL> """<STR_LIT>""" <EOL> server_class = oauth2_settings . OAUTH2_SERVER_CLASS <EOL> validator_class = oauth2_settings . OAUTH2_VALIDATOR_CLASS <EOL> oauthlib_backend_class = oauth2_settings . OAUTH2_BACKEND_CLASS <EOL> @ method_decorator ( sensitive_post_parameters ( '<STR_LIT:password>' ) ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> url , headers , body , status = self . create_token_response ( request ) <EOL> response = HttpResponse ( content = body , status = status ) <EOL> for k , v in headers . items ( ) : <EOL> response [ k ] = v <EOL> return response <EOL> class RevokeTokenView ( CsrfExemptMixin , OAuthLibMixin , View ) : <EOL> """<STR_LIT>""" <EOL> server_class = oauth2_settings . OAUTH2_SERVER_CLASS <EOL> validator_class = oauth2_settings . OAUTH2_VALIDATOR_CLASS <EOL> oauthlib_backend_class = oauth2_settings . OAUTH2_BACKEND_CLASS <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> url , headers , body , status = self . create_revocation_response ( request ) <EOL> response = HttpResponse ( content = body or '<STR_LIT>' , status = status ) <EOL> for k , v in headers . items ( ) : <EOL> response [ k ] = v <EOL> return response </s>
