<s> """<STR_LIT>""" <EOL> import datetime <EOL> from pyramid . httpexceptions import HTTPFound <EOL> from pyramid . security import NO_PERMISSION_REQUIRED <EOL> import requests <EOL> from . . api import ( <EOL> AuthenticationComplete , <EOL> AuthenticationDenied , <EOL> register_provider , <EOL> ) <EOL> from . . exceptions import ThirdPartyFailure <EOL> from . . settings import ProviderSettings <EOL> from . . utils import flat_url <EOL> class LiveAuthenticationComplete ( AuthenticationComplete ) : <EOL> """<STR_LIT>""" <EOL> def includeme ( config ) : <EOL> config . add_directive ( '<STR_LIT>' , add_live_login ) <EOL> config . add_directive ( '<STR_LIT>' , <EOL> add_live_login_from_settings ) <EOL> def add_live_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> p . update ( '<STR_LIT>' ) <EOL> config . add_live_login ( ** p . kwargs ) <EOL> def add_live_login ( config , <EOL> consumer_key , <EOL> consumer_secret , <EOL> scope = None , <EOL> login_path = '<STR_LIT>' , <EOL> callback_path = '<STR_LIT>' , <EOL> name = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> provider = LiveProvider ( name , consumer_key , consumer_secret , scope ) <EOL> config . add_route ( provider . login_route , login_path ) <EOL> config . add_view ( provider , attr = '<STR_LIT>' , 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 LiveProvider ( object ) : <EOL> def __init__ ( self , name , consumer_key , consumer_secret , scope ) : <EOL> self . name = name <EOL> self . type = '<STR_LIT>' <EOL> self . consumer_key = consumer_key <EOL> self . consumer_secret = consumer_secret <EOL> self . scope = scope <EOL> self . login_route = '<STR_LIT>' % name <EOL> self . callback_route = '<STR_LIT>' % name <EOL> def login ( self , request ) : <EOL> """<STR_LIT>""" <EOL> scope = request . POST . get ( '<STR_LIT>' , self . scope or <EOL> '<STR_LIT>' ) <EOL> fb_url = flat_url ( '<STR_LIT>' , scope = scope , <EOL> client_id = self . consumer_key , <EOL> redirect_uri = request . route_url ( self . callback_route ) , <EOL> response_type = "<STR_LIT:code>" ) <EOL> return HTTPFound ( location = fb_url ) <EOL> def callback ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:error>' in request . GET : <EOL> raise ThirdPartyFailure ( request . GET . get ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) <EOL> code = request . GET . get ( '<STR_LIT:code>' ) <EOL> if not code : <EOL> reason = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return AuthenticationDenied ( reason , <EOL> provider_name = self . name , <EOL> provider_type = self . type ) <EOL> access_url = flat_url ( <EOL> '<STR_LIT>' , <EOL> client_id = self . consumer_key , <EOL> client_secret = self . consumer_secret , <EOL> redirect_uri = request . route_url ( self . callback_route ) , <EOL> grant_type = "<STR_LIT>" , <EOL> code = code ) <EOL> r = requests . get ( access_url ) <EOL> if r . status_code != <NUM_LIT:200> : <EOL> raise ThirdPartyFailure ( "<STR_LIT>" % ( <EOL> r . status_code , r . content ) ) <EOL> data = r . json ( ) <EOL> access_token = data [ '<STR_LIT>' ] <EOL> graph_url = flat_url ( '<STR_LIT>' , <EOL> access_token = access_token ) <EOL> r = requests . get ( graph_url ) <EOL> if r . status_code != <NUM_LIT:200> : <EOL> raise ThirdPartyFailure ( "<STR_LIT>" % ( <EOL> r . status_code , r . content ) ) <EOL> live_profile = r . json ( ) <EOL> profile = extract_live_data ( live_profile ) <EOL> cred = { '<STR_LIT>' : access_token } <EOL> if '<STR_LIT>' in data : <EOL> cred [ '<STR_LIT>' ] = data [ '<STR_LIT>' ] <EOL> return LiveAuthenticationComplete ( profile = profile , <EOL> credentials = cred , <EOL> provider_name = self . name , <EOL> provider_type = self . type ) <EOL> def extract_live_data ( data ) : <EOL> """<STR_LIT>""" <EOL> emails = data . get ( '<STR_LIT>' , { } ) <EOL> profile = { <EOL> '<STR_LIT>' : [ { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : data [ '<STR_LIT:id>' ] } ] , <EOL> '<STR_LIT>' : data . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : emails . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : data . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT:name>' : { <EOL> '<STR_LIT>' : data . get ( '<STR_LIT:name>' ) , <EOL> '<STR_LIT>' : data . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : data . get ( '<STR_LIT>' ) , <EOL> } , <EOL> '<STR_LIT>' : data . get ( '<STR_LIT:name>' ) , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> } <EOL> if emails . get ( '<STR_LIT>' ) : <EOL> profile [ '<STR_LIT>' ] . append ( <EOL> { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:value>' : emails [ '<STR_LIT>' ] } ) <EOL> if emails . get ( '<STR_LIT>' ) : <EOL> profile [ '<STR_LIT>' ] . append ( <EOL> { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:value>' : emails [ '<STR_LIT>' ] } ) <EOL> if emails . get ( '<STR_LIT>' ) : <EOL> profile [ '<STR_LIT>' ] . append ( <EOL> { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:value>' : emails [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : True } ) <EOL> if emails . get ( '<STR_LIT>' ) : <EOL> profile [ '<STR_LIT>' ] . append ( <EOL> { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:value>' : emails [ '<STR_LIT>' ] } ) <EOL> if '<STR_LIT>' in data : <EOL> profile [ '<STR_LIT>' ] . append ( <EOL> { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:value>' : data [ '<STR_LIT>' ] } ) <EOL> if '<STR_LIT>' in data : <EOL> try : <EOL> profile [ '<STR_LIT>' ] = datetime . date ( <EOL> int ( data [ '<STR_LIT>' ] ) , <EOL> int ( data [ '<STR_LIT>' ] ) , <EOL> int ( data [ '<STR_LIT>' ] ) , <EOL> ) . strftime ( '<STR_LIT>' ) <EOL> except ValueError : <EOL> pass <EOL> return profile </s>
