<s> """<STR_LIT>""" <EOL> import django . conf <EOL> from django . core import exceptions <EOL> from django . core import urlresolvers <EOL> import httplib2 <EOL> from oauth2client import clientsecrets <EOL> from oauth2client . contrib . django_util import storage <EOL> from six . moves . urllib import parse <EOL> GOOGLE_OAUTH2_DEFAULT_SCOPES = ( '<STR_LIT:email>' , ) <EOL> GOOGLE_OAUTH2_REQUEST_ATTRIBUTE = '<STR_LIT>' <EOL> def _load_client_secrets ( filename ) : <EOL> """<STR_LIT>""" <EOL> client_type , client_info = clientsecrets . loadfile ( filename ) <EOL> if client_type != clientsecrets . TYPE_WEB : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( client_type ) ) <EOL> return client_info [ '<STR_LIT>' ] , client_info [ '<STR_LIT>' ] <EOL> def _get_oauth2_client_id_and_secret ( settings_instance ) : <EOL> """<STR_LIT>""" <EOL> secret_json = getattr ( django . conf . settings , <EOL> '<STR_LIT>' , None ) <EOL> if secret_json is not None : <EOL> return _load_client_secrets ( secret_json ) <EOL> else : <EOL> client_id = getattr ( settings_instance , "<STR_LIT>" , <EOL> None ) <EOL> client_secret = getattr ( settings_instance , <EOL> "<STR_LIT>" , None ) <EOL> if client_id is not None and client_secret is not None : <EOL> return client_id , client_secret <EOL> else : <EOL> raise exceptions . ImproperlyConfigured ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class OAuth2Settings ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , settings_instance ) : <EOL> self . scopes = getattr ( settings_instance , '<STR_LIT>' , <EOL> GOOGLE_OAUTH2_DEFAULT_SCOPES ) <EOL> self . request_prefix = getattr ( settings_instance , <EOL> '<STR_LIT>' , <EOL> GOOGLE_OAUTH2_REQUEST_ATTRIBUTE ) <EOL> self . client_id , self . client_secret = _get_oauth2_client_id_and_secret ( settings_instance ) <EOL> if ( '<STR_LIT>' <EOL> not in settings_instance . MIDDLEWARE_CLASSES ) : <EOL> raise exceptions . ImproperlyConfigured ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> oauth2_settings = OAuth2Settings ( django . conf . settings ) <EOL> def _redirect_with_params ( url_name , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> url = urlresolvers . reverse ( url_name , args = args ) <EOL> params = parse . urlencode ( kwargs , True ) <EOL> return "<STR_LIT>" . format ( url , params ) <EOL> class UserOAuth2 ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request , scopes = None , return_url = None ) : <EOL> """<STR_LIT>""" <EOL> self . request = request <EOL> self . return_url = return_url or request . get_full_path ( ) <EOL> self . scopes = set ( oauth2_settings . scopes ) <EOL> if scopes : <EOL> self . scopes |= set ( scopes ) <EOL> credentials = storage . get_storage ( self . request ) . get ( ) <EOL> if credentials : <EOL> self . scopes |= credentials . scopes <EOL> def get_authorize_redirect ( self ) : <EOL> """<STR_LIT>""" <EOL> get_params = { <EOL> '<STR_LIT>' : self . return_url , <EOL> '<STR_LIT>' : self . scopes <EOL> } <EOL> return _redirect_with_params ( '<STR_LIT>' , <EOL> ** get_params ) <EOL> def has_credentials ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . credentials and not self . credentials . invalid <EOL> and self . credentials . has_scopes ( self . scopes ) ) <EOL> @ property <EOL> def credentials ( self ) : <EOL> """<STR_LIT>""" <EOL> return storage . get_storage ( self . request ) . get ( ) <EOL> @ property <EOL> def http ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . has_credentials ( ) : <EOL> return self . credentials . authorize ( httplib2 . Http ( ) ) <EOL> return None </s>
