<s> """<STR_LIT>""" <EOL> import oauth2 as oauth <EOL> import cgi <EOL> from urllib import urlencode <EOL> from gluon import current <EOL> class OAuthAccount ( object ) : <EOL> """<STR_LIT>""" <EOL> def __redirect_uri ( self , next = None ) : <EOL> """<STR_LIT>""" <EOL> r = self . request <EOL> http_host = r . env . http_host <EOL> url_scheme = r . env . wsgi_url_scheme <EOL> if next : <EOL> path_info = next <EOL> else : <EOL> path_info = r . env . path_info <EOL> uri = '<STR_LIT>' % ( url_scheme , http_host , path_info ) <EOL> if r . get_vars and not next : <EOL> uri += '<STR_LIT:?>' + urlencode ( r . get_vars ) <EOL> return uri <EOL> def accessToken ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . session . access_token : <EOL> return self . session . access_token <EOL> if self . session . request_token : <EOL> token = self . session . request_token <EOL> self . session . request_token = None <EOL> token . set_verifier ( self . request . vars . oauth_verifier ) <EOL> client = oauth . Client ( self . consumer , token ) <EOL> resp , content = client . request ( self . access_token_url , "<STR_LIT:POST>" ) <EOL> if str ( resp [ '<STR_LIT:status>' ] ) != '<STR_LIT>' : <EOL> self . session . request_token = None <EOL> self . globals [ '<STR_LIT>' ] ( self . globals [ <EOL> '<STR_LIT>' ] ( f = '<STR_LIT:user>' , args = '<STR_LIT>' ) ) <EOL> self . session . access_token = oauth . Token . from_string ( content ) <EOL> return self . session . access_token <EOL> self . session . access_token = None <EOL> return None <EOL> def __init__ ( self , g , client_id , client_secret , auth_url , token_url , access_token_url , socket_timeout = <NUM_LIT> ) : <EOL> self . globals = g <EOL> self . client_id = client_id <EOL> self . client_secret = client_secret <EOL> self . code = None <EOL> self . request = current . request <EOL> self . session = current . session <EOL> self . auth_url = auth_url <EOL> self . token_url = token_url <EOL> self . access_token_url = access_token_url <EOL> self . socket_timeout = socket_timeout <EOL> self . consumer = oauth . Consumer ( self . client_id , self . client_secret ) <EOL> def login_url ( self , next = "<STR_LIT:/>" ) : <EOL> self . __oauth_login ( next ) <EOL> return next <EOL> def logout_url ( self , next = "<STR_LIT:/>" ) : <EOL> self . session . request_token = None <EOL> self . session . access_token = None <EOL> return next <EOL> def get_user ( self ) : <EOL> '''<STR_LIT>''' <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def __oauth_login ( self , next ) : <EOL> '''<STR_LIT>''' <EOL> if not self . accessToken ( ) : <EOL> client = oauth . Client ( self . consumer , None , timeout = self . socket_timeout ) <EOL> callback_url = self . __redirect_uri ( next ) <EOL> data = urlencode ( dict ( oauth_callback = callback_url ) ) <EOL> resp , content = client . request ( self . token_url , "<STR_LIT:POST>" , body = data ) <EOL> if resp [ '<STR_LIT:status>' ] != '<STR_LIT>' : <EOL> self . session . request_token = None <EOL> self . globals [ '<STR_LIT>' ] ( self . globals [ <EOL> '<STR_LIT>' ] ( f = '<STR_LIT:user>' , args = '<STR_LIT>' ) ) <EOL> request_token = self . session . request_token = oauth . Token . from_string ( content ) <EOL> data = urlencode ( dict ( oauth_token = request_token . key , <EOL> oauth_callback = callback_url ) ) <EOL> auth_request_url = self . auth_url + '<STR_LIT:?>' + data <EOL> HTTP = self . globals [ '<STR_LIT>' ] <EOL> raise HTTP ( <NUM_LIT> , <EOL> "<STR_LIT>" + auth_request_url + "<STR_LIT>" , <EOL> Location = auth_request_url ) <EOL> return None </s>
