<s> from urllib import urlencode <EOL> from urlparse import urlparse <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . http import HttpResponseRedirect <EOL> try : <EOL> from django . http . request import absolute_http_url_re <EOL> except ImportError : <EOL> from django . http import absolute_http_url_re <EOL> from django . shortcuts import render <EOL> from django . forms import Form <EOL> from django . views . decorators . http import require_http_methods <EOL> from djoauth2 . conf import settings <EOL> from djoauth2 . errors import DJOAuthError <EOL> from djoauth2 . errors import get_error_details <EOL> from djoauth2 . helpers import update_parameters <EOL> from djoauth2 . models import AuthorizationCode <EOL> from djoauth2 . models import Client <EOL> from djoauth2 . models import Scope <EOL> class AuthorizationCodeGenerator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , missing_redirect_uri ) : <EOL> """<STR_LIT>""" <EOL> self . missing_redirect_uri = missing_redirect_uri <EOL> self . user = None <EOL> self . client = None <EOL> self . redirect_uri = None <EOL> self . request_redirect_uri = None <EOL> self . valid_scope_objects = None <EOL> self . state = None <EOL> self . request = None <EOL> def validate ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if not request . method in [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] : <EOL> raise InvalidRequest ( '<STR_LIT>' ) <EOL> if settings . DJOAUTH2_SSL_ONLY and not request . is_secure ( ) : <EOL> raise InvalidRequest ( '<STR_LIT>' ) <EOL> self . request = request <EOL> self . user = request . user <EOL> if not self . user . is_authenticated ( ) : <EOL> raise UnauthenticatedUser ( '<STR_LIT>' ) <EOL> client_id = request . REQUEST . get ( '<STR_LIT>' ) <EOL> if not client_id : <EOL> raise InvalidRequest ( '<STR_LIT>' ) <EOL> try : <EOL> self . client = Client . objects . get ( key = client_id ) <EOL> except Client . DoesNotExist : <EOL> raise InvalidRequest ( '<STR_LIT>' ) <EOL> self . request_redirect_uri = request . REQUEST . get ( '<STR_LIT>' ) <EOL> if not ( self . client . redirect_uri or self . request_redirect_uri ) : <EOL> raise InvalidRequest ( '<STR_LIT>' ) <EOL> if ( self . client . redirect_uri and <EOL> self . request_redirect_uri and <EOL> self . client . redirect_uri != self . request_redirect_uri ) : <EOL> raise InvalidRequest ( '<STR_LIT>' ) <EOL> redirect_uri = self . client . redirect_uri or self . request_redirect_uri <EOL> if not absolute_http_url_re . match ( redirect_uri ) : <EOL> raise InvalidRequest ( '<STR_LIT>' ) <EOL> if urlparse ( redirect_uri ) . fragment : <EOL> raise InvalidRequest ( '<STR_LIT>' ) <EOL> if ( settings . DJOAUTH2_SSL_ONLY and <EOL> urlparse ( redirect_uri ) . scheme != '<STR_LIT>' ) : <EOL> raise InvalidRequest ( '<STR_LIT>' ) <EOL> self . redirect_uri = redirect_uri <EOL> response_type = request . REQUEST . get ( '<STR_LIT>' ) <EOL> if response_type != '<STR_LIT:code>' : <EOL> raise UnsupportedResponseType ( '<STR_LIT>' ) <EOL> self . state = request . REQUEST . get ( '<STR_LIT:state>' ) <EOL> if settings . DJOAUTH2_REQUIRE_STATE and not self . state : <EOL> raise InvalidRequest ( '<STR_LIT>' ) <EOL> requested_scope_string = request . REQUEST . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not requested_scope_string : <EOL> raise InvalidScope ( '<STR_LIT>' ) <EOL> requested_scope_names = set ( requested_scope_string . split ( '<STR_LIT:U+0020>' ) ) <EOL> self . valid_scope_objects = Scope . objects . filter ( <EOL> name__in = requested_scope_names ) <EOL> valid_scope_names = { scope . name for scope in self . valid_scope_objects } <EOL> if valid_scope_names < requested_scope_names : <EOL> raise InvalidScope ( '<STR_LIT>' . format ( <EOL> '<STR_LIT:U+002CU+0020>' . join ( '<STR_LIT>' . format ( name ) <EOL> for name in ( requested_scope_names - valid_scope_names ) ) ) ) <EOL> def get_request_uri_parameters ( self , as_dict = False ) : <EOL> """<STR_LIT>""" <EOL> if not self . request : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return ( dict if as_dict else urlencode ) ( self . request . REQUEST . items ( ) ) <EOL> def make_error_redirect ( self , authorization_error = None ) : <EOL> """<STR_LIT>""" <EOL> if not self . redirect_uri : <EOL> return HttpResponseRedirect ( self . missing_redirect_uri ) <EOL> authorization_error = ( authorization_error or <EOL> AccessDenied ( '<STR_LIT>' ) ) <EOL> response_params = get_error_details ( authorization_error ) <EOL> if self . state is not None : <EOL> response_params [ '<STR_LIT:state>' ] = self . state <EOL> return HttpResponseRedirect ( <EOL> update_parameters ( self . redirect_uri , response_params ) ) <EOL> def make_success_redirect ( self ) : <EOL> """<STR_LIT>""" <EOL> new_authorization_code = AuthorizationCode . objects . create ( <EOL> user = self . user , <EOL> client = self . client , <EOL> redirect_uri = ( self . redirect_uri if self . request_redirect_uri else None ) <EOL> ) <EOL> new_authorization_code . scopes = self . valid_scope_objects <EOL> new_authorization_code . save ( ) <EOL> response_params = { '<STR_LIT:code>' : new_authorization_code . value } <EOL> if self . state is not None : <EOL> response_params [ '<STR_LIT:state>' ] = self . state <EOL> return HttpResponseRedirect ( <EOL> update_parameters ( self . redirect_uri , response_params ) ) <EOL> def make_authorization_endpoint ( missing_redirect_uri , <EOL> authorization_endpoint_uri , <EOL> authorization_template_name ) : <EOL> """<STR_LIT>""" <EOL> @ login_required <EOL> @ require_http_methods ( [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def authorization_endpoint ( request ) : <EOL> auth_code_generator = AuthorizationCodeGenerator ( missing_redirect_uri ) <EOL> try : <EOL> auth_code_generator . validate ( request ) <EOL> except AuthorizationError as authorization_error : <EOL> return auth_code_generator . make_error_redirect ( authorization_error ) <EOL> if request . method == '<STR_LIT:GET>' : <EOL> return render ( request , authorization_template_name , { <EOL> '<STR_LIT>' : Form ( ) , <EOL> '<STR_LIT>' : auth_code_generator . client , <EOL> '<STR_LIT>' : auth_code_generator . valid_scope_objects , <EOL> '<STR_LIT>' : update_parameters ( <EOL> authorization_endpoint_uri , <EOL> auth_code_generator . get_request_uri_parameters ( as_dict = True ) ) , <EOL> } ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = Form ( request ) <EOL> if form . is_valid ( ) and request . POST . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> return auth_code_generator . make_success_redirect ( ) <EOL> else : <EOL> return auth_code_generator . make_error_redirect ( ) <EOL> return authorization_endpoint <EOL> class AuthorizationError ( DJOAuthError ) : <EOL> """<STR_LIT>""" <EOL> class UnauthenticatedUser ( AuthorizationError ) : <EOL> """<STR_LIT>""" <EOL> class InvalidRequest ( AuthorizationError ) : <EOL> """<STR_LIT>""" <EOL> error_name = '<STR_LIT>' <EOL> class UnauthorizedClient ( AuthorizationError ) : <EOL> """<STR_LIT>""" <EOL> error_name = '<STR_LIT>' <EOL> class AccessDenied ( AuthorizationError ) : <EOL> """<STR_LIT>""" <EOL> error_name = '<STR_LIT>' <EOL> class UnsupportedResponseType ( AuthorizationError ) : <EOL> """<STR_LIT>""" <EOL> error_name = '<STR_LIT>' <EOL> class InvalidScope ( AuthorizationError ) : <EOL> """<STR_LIT>""" <EOL> error_name = '<STR_LIT>' <EOL> class ServerError ( AuthorizationError ) : <EOL> """<STR_LIT>""" <EOL> error_name = '<STR_LIT>' <EOL> class TemporarilyUnavailable ( AuthorizationError ) : <EOL> """<STR_LIT>""" <EOL> error_name = '<STR_LIT>' </s>
