<s> """<STR_LIT>""" <EOL> from pyramid . httpexceptions import HTTPFound <EOL> from pyramid . security import NO_PERMISSION_REQUIRED <EOL> import requests <EOL> from requests_oauthlib import OAuth1 <EOL> from . . api import ( <EOL> AuthenticationComplete , <EOL> AuthenticationDenied , <EOL> register_provider , <EOL> ) <EOL> from . . compat import parse_qsl <EOL> from . . exceptions import ThirdPartyFailure <EOL> from . . settings import ProviderSettings <EOL> from . . utils import flat_url <EOL> REQUEST_URL = '<STR_LIT>' <EOL> AUTH_URL = '<STR_LIT>' <EOL> ACCESS_URL = '<STR_LIT>' <EOL> DATA_URL = '<STR_LIT>' <EOL> class TwitterAuthenticationComplete ( AuthenticationComplete ) : <EOL> """<STR_LIT>""" <EOL> def includeme ( config ) : <EOL> config . add_directive ( '<STR_LIT>' , add_twitter_login ) <EOL> config . add_directive ( '<STR_LIT>' , <EOL> add_twitter_login_from_settings ) <EOL> def add_twitter_login_from_settings ( config , prefix = '<STR_LIT>' ) : <EOL> settings = config . registry . settings <EOL> p = ProviderSettings ( settings , prefix ) <EOL> p . update ( '<STR_LIT>' , required = True ) <EOL> p . update ( '<STR_LIT>' , required = True ) <EOL> p . update ( '<STR_LIT>' ) <EOL> p . update ( '<STR_LIT>' ) <EOL> config . add_twitter_login ( ** p . kwargs ) <EOL> def add_twitter_login ( config , <EOL> consumer_key , <EOL> consumer_secret , <EOL> login_path = '<STR_LIT>' , <EOL> callback_path = '<STR_LIT>' , <EOL> name = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> provider = TwitterProvider ( name , consumer_key , consumer_secret ) <EOL> config . add_route ( provider . login_route , login_path ) <EOL> config . add_view ( provider , attr = '<STR_LIT>' , <EOL> route_name = provider . login_route , <EOL> permission = NO_PERMISSION_REQUIRED ) <EOL> config . add_route ( provider . callback_route , callback_path , <EOL> use_global_views = True , <EOL> factory = provider . callback ) <EOL> register_provider ( config , name , provider ) <EOL> class TwitterProvider ( object ) : <EOL> def __init__ ( self , name , consumer_key , consumer_secret ) : <EOL> self . name = name <EOL> self . type = '<STR_LIT>' <EOL> self . consumer_key = consumer_key <EOL> self . consumer_secret = consumer_secret <EOL> self . login_route = '<STR_LIT>' % name <EOL> self . callback_route = '<STR_LIT>' % name <EOL> def login ( self , request ) : <EOL> """<STR_LIT>""" <EOL> oauth = OAuth1 ( <EOL> self . consumer_key , <EOL> client_secret = self . consumer_secret , <EOL> callback_uri = request . route_url ( self . callback_route ) ) <EOL> resp = requests . post ( REQUEST_URL , auth = oauth ) <EOL> if resp . status_code != <NUM_LIT:200> : <EOL> raise ThirdPartyFailure ( "<STR_LIT>" % ( <EOL> resp . status_code , resp . content ) ) <EOL> request_token = dict ( parse_qsl ( resp . text ) ) <EOL> request . session [ '<STR_LIT>' ] = request_token <EOL> auth_url = flat_url ( AUTH_URL , oauth_token = request_token [ '<STR_LIT>' ] ) <EOL> return HTTPFound ( location = auth_url ) <EOL> def callback ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in request . GET : <EOL> return AuthenticationDenied ( "<STR_LIT>" , <EOL> provider_name = self . name , <EOL> provider_type = self . type ) <EOL> verifier = request . GET . get ( '<STR_LIT>' ) <EOL> if not verifier : <EOL> raise ThirdPartyFailure ( "<STR_LIT>" ) <EOL> request_token = request . session . pop ( '<STR_LIT>' ) <EOL> oauth = OAuth1 ( <EOL> self . consumer_key , <EOL> client_secret = self . consumer_secret , <EOL> resource_owner_key = request_token [ '<STR_LIT>' ] , <EOL> resource_owner_secret = request_token [ '<STR_LIT>' ] , <EOL> verifier = verifier ) <EOL> resp = requests . post ( ACCESS_URL , auth = oauth ) <EOL> if resp . status_code != <NUM_LIT:200> : <EOL> raise ThirdPartyFailure ( "<STR_LIT>" % ( <EOL> resp . status_code , resp . content ) ) <EOL> access_token = dict ( parse_qsl ( resp . text ) ) <EOL> creds = { <EOL> '<STR_LIT>' : access_token [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : access_token [ '<STR_LIT>' ] , <EOL> } <EOL> username = access_token [ '<STR_LIT>' ] <EOL> profile = { } <EOL> profile [ '<STR_LIT>' ] = [ { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : access_token [ '<STR_LIT>' ] , <EOL> '<STR_LIT:username>' : username , <EOL> } ] <EOL> profile [ '<STR_LIT>' ] = username <EOL> profile [ '<STR_LIT>' ] = username <EOL> oauth = OAuth1 ( <EOL> self . consumer_key , <EOL> client_secret = self . consumer_secret , <EOL> resource_owner_key = access_token [ '<STR_LIT>' ] , <EOL> resource_owner_secret = access_token [ '<STR_LIT>' ] ) <EOL> resp = requests . get ( DATA_URL % username , auth = oauth ) <EOL> if resp . status_code == <NUM_LIT:200> : <EOL> data = resp . json ( ) <EOL> if '<STR_LIT:name>' in data : <EOL> profile [ '<STR_LIT>' ] = data [ '<STR_LIT:name>' ] <EOL> profile [ '<STR_LIT:name>' ] = { '<STR_LIT>' : profile [ '<STR_LIT>' ] } <EOL> if '<STR_LIT:url>' in data : <EOL> profile [ '<STR_LIT>' ] = [ { '<STR_LIT:value>' : data [ '<STR_LIT:url>' ] } ] <EOL> if '<STR_LIT:location>' in data : <EOL> profile [ '<STR_LIT>' ] = [ { '<STR_LIT>' : data [ '<STR_LIT:location>' ] } ] <EOL> if '<STR_LIT>' in data : <EOL> profile [ '<STR_LIT>' ] = [ { '<STR_LIT:value>' : data [ '<STR_LIT>' ] } ] <EOL> if data . get ( '<STR_LIT>' ) : <EOL> offset = float ( data [ '<STR_LIT>' ] ) / <NUM_LIT> <EOL> h = int ( offset ) <EOL> m = int ( abs ( offset - h ) * <NUM_LIT> ) <EOL> profile [ '<STR_LIT>' ] = '<STR_LIT>' . format ( h = h , m = m ) <EOL> return TwitterAuthenticationComplete ( profile = profile , <EOL> credentials = creds , <EOL> provider_name = self . name , <EOL> provider_type = self . type ) </s>
