<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals , absolute_import <EOL> import json <EOL> import logging <EOL> from oauthlib import common <EOL> from oauthlib . uri_validate import is_absolute_uri <EOL> from . base import GrantTypeBase <EOL> from . . import errors <EOL> from . . request_validator import RequestValidator <EOL> log = logging . getLogger ( __name__ ) <EOL> class AuthorizationCodeGrant ( GrantTypeBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request_validator = None , refresh_token = True ) : <EOL> self . request_validator = request_validator or RequestValidator ( ) <EOL> self . refresh_token = refresh_token <EOL> def create_authorization_code ( self , request ) : <EOL> """<STR_LIT>""" <EOL> grant = { '<STR_LIT:code>' : common . generate_token ( ) } <EOL> if hasattr ( request , '<STR_LIT:state>' ) and request . state : <EOL> grant [ '<STR_LIT:state>' ] = request . state <EOL> log . debug ( '<STR_LIT>' , <EOL> grant , request ) <EOL> return grant <EOL> def create_authorization_response ( self , request , token_handler ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if not request . scopes : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . validate_authorization_request ( request ) <EOL> log . debug ( '<STR_LIT>' , <EOL> request ) <EOL> except errors . FatalClientError as e : <EOL> log . debug ( '<STR_LIT>' , <EOL> request , e ) <EOL> raise <EOL> except errors . OAuth2Error as e : <EOL> log . debug ( '<STR_LIT>' , request , e ) <EOL> request . redirect_uri = request . redirect_uri or self . error_uri <EOL> return { '<STR_LIT>' : common . add_params_to_uri ( request . redirect_uri , e . twotuples ) } , None , <NUM_LIT> <EOL> grant = self . create_authorization_code ( request ) <EOL> log . debug ( '<STR_LIT>' , grant , request ) <EOL> self . request_validator . save_authorization_code ( <EOL> request . client_id , grant , request ) <EOL> return { '<STR_LIT>' : common . add_params_to_uri ( request . redirect_uri , grant . items ( ) ) } , None , <NUM_LIT> <EOL> def create_token_response ( self , request , token_handler ) : <EOL> """<STR_LIT>""" <EOL> headers = { <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> try : <EOL> self . validate_token_request ( request ) <EOL> log . debug ( '<STR_LIT>' , request ) <EOL> except errors . OAuth2Error as e : <EOL> log . debug ( '<STR_LIT>' , request , e ) <EOL> return headers , e . json , e . status_code <EOL> token = token_handler . create_token ( request , refresh_token = self . refresh_token ) <EOL> self . request_validator . invalidate_authorization_code ( <EOL> request . client_id , request . code , request ) <EOL> return headers , json . dumps ( token ) , <NUM_LIT:200> <EOL> def validate_authorization_request ( self , request ) : <EOL> """<STR_LIT>""" <EOL> for param in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:state>' ) : <EOL> try : <EOL> duplicate_params = request . duplicate_params <EOL> except ValueError : <EOL> raise errors . InvalidRequestFatalError ( description = '<STR_LIT>' , request = request ) <EOL> if param in duplicate_params : <EOL> raise errors . InvalidRequestFatalError ( description = '<STR_LIT>' % param , request = request ) <EOL> if not request . client_id : <EOL> raise errors . MissingClientIdError ( request = request ) <EOL> if not self . request_validator . validate_client_id ( request . client_id , request ) : <EOL> raise errors . InvalidClientIdError ( request = request ) <EOL> log . debug ( '<STR_LIT>' , <EOL> request . redirect_uri , request . client_id ) <EOL> if request . redirect_uri is not None : <EOL> request . using_default_redirect_uri = False <EOL> log . debug ( '<STR_LIT>' , request . redirect_uri ) <EOL> if not is_absolute_uri ( request . redirect_uri ) : <EOL> raise errors . InvalidRedirectURIError ( request = request ) <EOL> if not self . request_validator . validate_redirect_uri ( <EOL> request . client_id , request . redirect_uri , request ) : <EOL> raise errors . MismatchingRedirectURIError ( request = request ) <EOL> else : <EOL> request . redirect_uri = self . request_validator . get_default_redirect_uri ( <EOL> request . client_id , request ) <EOL> request . using_default_redirect_uri = True <EOL> log . debug ( '<STR_LIT>' , request . redirect_uri ) <EOL> if not request . redirect_uri : <EOL> raise errors . MissingRedirectURIError ( request = request ) <EOL> if request . response_type is None : <EOL> raise errors . MissingResponseTypeError ( request = request ) <EOL> elif request . response_type != '<STR_LIT:code>' : <EOL> raise errors . UnsupportedResponseTypeError ( request = request ) <EOL> if not self . request_validator . validate_response_type ( request . client_id , <EOL> request . response_type , <EOL> request . client , request ) : <EOL> log . debug ( '<STR_LIT>' , <EOL> request . client_id , request . response_type ) <EOL> raise errors . UnauthorizedClientError ( request = request ) <EOL> self . validate_scopes ( request ) <EOL> return request . scopes , { <EOL> '<STR_LIT>' : request . client_id , <EOL> '<STR_LIT>' : request . redirect_uri , <EOL> '<STR_LIT>' : request . response_type , <EOL> '<STR_LIT:state>' : request . state , <EOL> '<STR_LIT>' : request , <EOL> } <EOL> def validate_token_request ( self , request ) : <EOL> if request . grant_type != '<STR_LIT>' : <EOL> raise errors . UnsupportedGrantTypeError ( request = request ) <EOL> if request . code is None : <EOL> raise errors . InvalidRequestError ( <EOL> description = '<STR_LIT>' , request = request ) <EOL> for param in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if param in request . duplicate_params : <EOL> raise errors . InvalidRequestError ( description = '<STR_LIT>' % param , <EOL> request = request ) <EOL> if self . request_validator . client_authentication_required ( request ) : <EOL> if not self . request_validator . authenticate_client ( request ) : <EOL> log . debug ( '<STR_LIT>' , request ) <EOL> raise errors . InvalidClientError ( request = request ) <EOL> elif not self . request_validator . authenticate_client_id ( request . client_id , request ) : <EOL> log . debug ( '<STR_LIT>' , request ) <EOL> raise errors . InvalidClientError ( request = request ) <EOL> if not hasattr ( request . client , '<STR_LIT>' ) : <EOL> raise NotImplementedError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . validate_grant_type ( request ) <EOL> if not self . request_validator . validate_code ( request . client_id , <EOL> request . code , request . client , request ) : <EOL> log . debug ( '<STR_LIT>' , <EOL> request . client_id , request . client , request . scopes ) <EOL> raise errors . InvalidGrantError ( request = request ) <EOL> for attr in ( '<STR_LIT:user>' , '<STR_LIT>' ) : <EOL> if getattr ( request , attr , None ) is None : <EOL> log . debug ( '<STR_LIT>' , attr ) <EOL> if not self . request_validator . confirm_redirect_uri ( request . client_id , request . code , <EOL> request . redirect_uri , request . client ) : <EOL> log . debug ( '<STR_LIT>' , <EOL> request . redirect_uri , request . client_id , request . client ) <EOL> raise errors . AccessDeniedError ( request = request ) </s>
