<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import copy <EOL> from datetime import datetime <EOL> import json <EOL> import urllib <EOL> import config <EOL> from controllers . util import users_helper <EOL> from google . appengine . api import urlfetch <EOL> from google . appengine . api import users <EOL> OAUTH_CLIENT_ID = config . AUTH_CONFIG [ '<STR_LIT>' ] <EOL> OAUTH_CLIENT_SECRET = config . AUTH_CONFIG [ '<STR_LIT>' ] <EOL> OAUTH_REDIRECT_URI = config . AUTH_CONFIG [ '<STR_LIT>' ] <EOL> OAUTH_ENDPOINT = '<STR_LIT>' <EOL> OAUTH_TOKEN_ENDPOINT = '<STR_LIT>' <EOL> OAUTH_REVOKE_ENDPOINT = '<STR_LIT>' <EOL> OAUTH_SCOPE = '<STR_LIT>' <EOL> OAUTH_ACCESS_TYPE = '<STR_LIT>' <EOL> ISSUED_AUTH_TOKENS_WEB_URL = ( <EOL> '<STR_LIT>' ) <EOL> OAUTH_PARAMS = urllib . urlencode ( { <EOL> '<STR_LIT>' : '<STR_LIT:code>' , <EOL> '<STR_LIT>' : OAUTH_CLIENT_ID , <EOL> '<STR_LIT>' : OAUTH_REDIRECT_URI , <EOL> '<STR_LIT>' : OAUTH_SCOPE , <EOL> '<STR_LIT>' : OAUTH_ACCESS_TYPE <EOL> } ) <EOL> OAUTH_URL = '<STR_LIT>' % ( OAUTH_ENDPOINT , OAUTH_PARAMS ) <EOL> AUTH_MESSAGES = { <EOL> '<STR_LIT>' : ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ISSUED_AUTH_TOKENS_WEB_URL ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ISSUED_AUTH_TOKENS_WEB_URL ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ISSUED_AUTH_TOKENS_WEB_URL ) <EOL> } <EOL> def AuthorizeApiQuery ( fn ) : <EOL> """<STR_LIT>""" <EOL> def Wrapper ( api_query ) : <EOL> """<STR_LIT>""" <EOL> access_token = GetAccessTokenForApiQuery ( api_query ) <EOL> query = api_query . request <EOL> if access_token : <EOL> query = ( '<STR_LIT>' % ( <EOL> urllib . unquote ( api_query . request ) , access_token ) ) <EOL> new_api_query = copy . copy ( api_query ) <EOL> new_api_query . request = query <EOL> return fn ( new_api_query ) <EOL> return Wrapper <EOL> def FetchAccessToken ( refresh_token ) : <EOL> """<STR_LIT>""" <EOL> auth_params = { <EOL> '<STR_LIT>' : refresh_token , <EOL> '<STR_LIT>' : OAUTH_CLIENT_ID , <EOL> '<STR_LIT>' : OAUTH_CLIENT_SECRET , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> return FetchCredentials ( auth_params ) <EOL> def FetchCredentials ( auth_params ) : <EOL> """<STR_LIT>""" <EOL> auth_status = { <EOL> '<STR_LIT>' : <NUM_LIT> <EOL> } <EOL> auth_payload = urllib . urlencode ( auth_params ) <EOL> try : <EOL> response = urlfetch . fetch ( url = OAUTH_TOKEN_ENDPOINT , <EOL> payload = auth_payload , <EOL> method = urlfetch . POST , <EOL> headers = { <EOL> '<STR_LIT:Content-Type>' : <EOL> '<STR_LIT>' <EOL> } ) <EOL> response_content = json . loads ( response . content ) <EOL> if response . status_code == <NUM_LIT:200> : <EOL> auth_status [ '<STR_LIT>' ] = <NUM_LIT:200> <EOL> auth_status [ '<STR_LIT:content>' ] = response_content <EOL> except ( ValueError , TypeError , AttributeError , urlfetch . Error ) , e : <EOL> auth_status [ '<STR_LIT:content>' ] = str ( e ) <EOL> return auth_status <EOL> def GetAccessTokenForApiQuery ( api_query ) : <EOL> """<STR_LIT>""" <EOL> user_settings = users_helper . GetGaSuperProxyUser ( api_query . user . key ( ) . name ( ) ) <EOL> if user_settings . ga_refresh_token and user_settings . ga_access_token : <EOL> access_token = user_settings . ga_access_token <EOL> if datetime . utcnow ( ) > user_settings . ga_token_expiry : <EOL> response = FetchAccessToken ( user_settings . ga_refresh_token ) <EOL> if ( response . get ( '<STR_LIT>' ) == <NUM_LIT:200> and response . get ( '<STR_LIT:content>' ) <EOL> and response . get ( '<STR_LIT:content>' ) . get ( '<STR_LIT>' ) ) : <EOL> access_token = response . get ( '<STR_LIT:content>' ) . get ( '<STR_LIT>' ) <EOL> expires_in = int ( response . get ( '<STR_LIT:content>' ) . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> users_helper . SetUserCredentials ( api_query . user . key ( ) . name ( ) , <EOL> user_settings . ga_refresh_token , <EOL> access_token , <EOL> expires_in ) <EOL> return access_token <EOL> return None <EOL> def GetOAuthCredentials ( code ) : <EOL> """<STR_LIT>""" <EOL> auth_params = { <EOL> '<STR_LIT:code>' : code , <EOL> '<STR_LIT>' : OAUTH_CLIENT_ID , <EOL> '<STR_LIT>' : OAUTH_CLIENT_SECRET , <EOL> '<STR_LIT>' : OAUTH_REDIRECT_URI , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> return FetchCredentials ( auth_params ) <EOL> def OAuthHandler ( request ) : <EOL> """<STR_LIT>""" <EOL> if request . get ( '<STR_LIT:code>' ) : <EOL> code_response = SaveAuthTokensForUser ( request . get ( '<STR_LIT:code>' ) ) <EOL> if code_response . get ( '<STR_LIT:success>' ) : <EOL> auth_values = { <EOL> '<STR_LIT:status>' : '<STR_LIT:success>' , <EOL> '<STR_LIT:message>' : AUTH_MESSAGES [ '<STR_LIT>' ] , <EOL> } <EOL> else : <EOL> auth_values = { <EOL> '<STR_LIT:status>' : '<STR_LIT:error>' , <EOL> '<STR_LIT:message>' : AUTH_MESSAGES [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : code_response . get ( '<STR_LIT:message>' ) <EOL> } <EOL> elif request . get ( '<STR_LIT>' ) : <EOL> revoked = RevokeAuthTokensForUser ( ) <EOL> if revoked : <EOL> auth_values = { <EOL> '<STR_LIT:status>' : '<STR_LIT:success>' , <EOL> '<STR_LIT:message>' : AUTH_MESSAGES [ '<STR_LIT>' ] <EOL> } <EOL> else : <EOL> auth_values = { <EOL> '<STR_LIT:status>' : '<STR_LIT:error>' , <EOL> '<STR_LIT:message>' : AUTH_MESSAGES [ '<STR_LIT>' ] <EOL> } <EOL> elif request . get ( '<STR_LIT:error>' ) : <EOL> auth_values = { <EOL> '<STR_LIT:status>' : '<STR_LIT:error>' , <EOL> '<STR_LIT:message>' : AUTH_MESSAGES [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : request . get ( '<STR_LIT:error>' ) <EOL> } <EOL> else : <EOL> auth_values = { <EOL> '<STR_LIT:status>' : '<STR_LIT:error>' , <EOL> '<STR_LIT:message>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : AUTH_MESSAGES [ '<STR_LIT>' ] <EOL> } <EOL> return auth_values <EOL> def RevokeAuthTokensForUser ( ) : <EOL> """<STR_LIT>""" <EOL> user = users_helper . GetGaSuperProxyUser ( users . get_current_user ( ) . user_id ( ) ) <EOL> if user and user . ga_refresh_token : <EOL> RevokeOAuthCredentials ( user . ga_refresh_token ) <EOL> users_helper . SetUserCredentials ( users . get_current_user ( ) . user_id ( ) ) <EOL> return True <EOL> return False <EOL> def RevokeOAuthCredentials ( token ) : <EOL> """<STR_LIT>""" <EOL> if token : <EOL> revoke_url = OAUTH_REVOKE_ENDPOINT + token <EOL> try : <EOL> response = urlfetch . fetch ( url = revoke_url ) <EOL> if response . status_code == <NUM_LIT:200> : <EOL> return True <EOL> except urlfetch . Error : <EOL> return False <EOL> return False <EOL> def SaveAuthTokensForUser ( code ) : <EOL> """<STR_LIT>""" <EOL> response = { <EOL> '<STR_LIT:success>' : False <EOL> } <EOL> auth_response = GetOAuthCredentials ( code ) <EOL> response_content = auth_response . get ( '<STR_LIT:content>' ) <EOL> if ( auth_response . get ( '<STR_LIT>' ) == <NUM_LIT:200> <EOL> and response_content <EOL> and response_content . get ( '<STR_LIT>' ) ) : <EOL> refresh_token = response_content . get ( '<STR_LIT>' ) <EOL> access_token = response_content . get ( '<STR_LIT>' ) <EOL> users_helper . SetUserCredentials ( <EOL> users . get_current_user ( ) . user_id ( ) , <EOL> refresh_token , access_token ) <EOL> response [ '<STR_LIT:success>' ] = True <EOL> else : <EOL> response [ '<STR_LIT:message>' ] = response_content <EOL> return response </s>
