<s> """<STR_LIT>""" <EOL> import urlparse <EOL> import openid <EOL> from openid . store import memstore , filestore , sqlstore <EOL> from openid . consumer import consumer <EOL> from openid . oidutil import appendArgs <EOL> from openid . cryptutil import randomString <EOL> from openid . fetchers import setDefaultFetcher , Urllib2Fetcher <EOL> from openid . extensions import pape , sreg , ax <EOL> from pyramid . httpexceptions import HTTPFound <EOL> from pyramid . httpexceptions import HTTPBadRequest <EOL> from pyramid . security import remember <EOL> from itertools import chain <EOL> import logging <EOL> log = logging . getLogger ( __name__ ) <EOL> def get_ax_required_from_settings ( settings ) : <EOL> ax_required = { } <EOL> ax_required_string = settings . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> for item in ax_required_string . split ( ) : <EOL> key , value = item . split ( "<STR_LIT:=>" ) <EOL> ax_required [ key ] = value <EOL> return ax_required <EOL> def get_ax_optional_from_settings ( settings ) : <EOL> ax_optional = { } <EOL> ax_optional_string = settings . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> for item in ax_optional_string . split ( ) : <EOL> key , value = item . split ( "<STR_LIT:=>" ) <EOL> ax_optional [ key ] = value <EOL> return ax_optional <EOL> def get_sreg_required_from_settings ( settings ) : <EOL> sreg_required = settings . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> sreg_required = [ a . strip ( ) for a in sreg_required . split ( ) ] <EOL> return sreg_required <EOL> def get_sreg_optional_from_settings ( settings ) : <EOL> sreg_optional = settings . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> sreg_optional = [ a . strip ( ) for a in sreg_optional . split ( ) ] <EOL> return sreg_optional <EOL> def verify_openid ( context , request ) : <EOL> settings = request . registry . settings <EOL> openid_field = settings . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> log . info ( '<STR_LIT>' % openid_field ) <EOL> incoming_openid_url = request . params . get ( openid_field , None ) <EOL> openid_mode = request . params . get ( '<STR_LIT>' , None ) <EOL> if incoming_openid_url is not None : <EOL> return process_incoming_request ( context , request , incoming_openid_url ) <EOL> elif openid_mode == '<STR_LIT>' : <EOL> return process_provider_response ( context , request ) <EOL> return HTTPBadRequest ( ) <EOL> def worthless_callback ( request , success_dict , success_dict = { } ) : <EOL> pass <EOL> def build_consumer_from_request ( request ) : <EOL> settings = request . registry . settings <EOL> store_type = settings . get ( '<STR_LIT>' ) <EOL> log . info ( '<STR_LIT>' % store_type ) <EOL> store = None <EOL> if store_type == '<STR_LIT:file>' : <EOL> store_file_path = settings . get ( '<STR_LIT>' ) <EOL> log . info ( '<STR_LIT>' % store_file_path ) <EOL> store = filestore . FileOpenIDStore ( store_file_path ) <EOL> elif store_type == '<STR_LIT>' : <EOL> store = memstore . MemoryStore ( ) <EOL> elif store_type == '<STR_LIT>' : <EOL> sql_connstring = settings . get ( '<STR_LIT>' ) <EOL> sql_associations_table = settings . get ( <EOL> '<STR_LIT>' ) <EOL> store = sqlstore . SQLStore ( sql_connstring , <EOL> sql_associations_table , <EOL> sql_connstring ) <EOL> log . info ( '<STR_LIT>' % store ) <EOL> openid_consumer = consumer . Consumer ( request . session , store ) <EOL> return openid_consumer <EOL> def process_incoming_request ( context , request , incoming_openid_url ) : <EOL> settings = request . registry . settings <EOL> log . info ( '<STR_LIT>' % incoming_openid_url ) <EOL> openid_consumer = build_consumer_from_request ( request ) <EOL> try : <EOL> openid_request = openid_consumer . begin ( incoming_openid_url ) <EOL> ax_required = get_ax_required_from_settings ( settings ) <EOL> ax_optional = get_ax_optional_from_settings ( settings ) <EOL> log . info ( '<STR_LIT>' % ax_required ) <EOL> log . info ( '<STR_LIT>' % ax_optional ) <EOL> if len ( ax_required . values ( ) ) or len ( ax_optional . values ( ) ) : <EOL> fetch_request = ax . FetchRequest ( ) <EOL> for value in ax_required . values ( ) : <EOL> fetch_request . add ( ax . AttrInfo ( value , required = True ) ) <EOL> for value in ax_optional . values ( ) : <EOL> fetch_request . add ( ax . AttrInfo ( value , required = False ) ) <EOL> openid_request . addExtension ( fetch_request ) <EOL> sreg_required = get_sreg_required_from_settings ( settings ) <EOL> sreg_optional = get_sreg_optional_from_settings ( settings ) <EOL> log . info ( '<STR_LIT>' % sreg_required ) <EOL> log . info ( '<STR_LIT>' % sreg_optional ) <EOL> if len ( sreg_required ) or len ( sreg_optional ) : <EOL> sreq = sreg . SRegRequest ( required = sreg_required , <EOL> optional = sreg_optional ) <EOL> openid_request . addExtension ( sreq ) <EOL> except consumer . DiscoveryFailure , exc : <EOL> return error_to_login_form ( request , '<STR_LIT>' % exc [ <NUM_LIT:0> ] ) <EOL> except KeyError , exc : <EOL> return error_to_login_form ( request , '<STR_LIT>' % exc [ <NUM_LIT:0> ] ) <EOL> if openid_request is None : <EOL> return error_to_login_form ( <EOL> request , <EOL> '<STR_LIT>' % incoming_openid_url ) <EOL> realm_name = settings . get ( '<STR_LIT>' , request . host_url ) <EOL> temp_url = urlparse . urlparse ( request . url ) <EOL> temp_url_qs = urlparse . parse_qs ( temp_url . query ) <EOL> temp_url_qs . pop ( settings . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> return_url = urlparse . urlunsplit ( ( temp_url . scheme , temp_url . netloc , temp_url . path , temp_url_qs , temp_url . fragment ) ) <EOL> redirect_url = openid_request . redirectURL ( realm_name , return_url ) <EOL> log . info ( '<STR_LIT>' % realm_name ) <EOL> log . info ( '<STR_LIT>' % return_url ) <EOL> log . info ( '<STR_LIT>' % redirect_url ) <EOL> return HTTPFound ( location = redirect_url ) <EOL> def process_provider_response ( context , request ) : <EOL> settings = request . registry . settings <EOL> openid_consumer = build_consumer_from_request ( request ) <EOL> info = openid_consumer . complete ( request . params , request . url ) <EOL> log . info ( '<STR_LIT>' % info . status ) <EOL> if info . status == consumer . SUCCESS : <EOL> log . info ( '<STR_LIT>' ) <EOL> success_dict = { <EOL> '<STR_LIT>' : info . identity_url , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } } <EOL> fr = ax . FetchResponse . fromSuccessResponse ( info ) <EOL> if fr is not None : <EOL> ax_required = get_ax_required_from_settings ( settings ) <EOL> ax_optional = get_ax_optional_from_settings ( settings ) <EOL> items = chain ( ax_required . items ( ) , ax_optional . items ( ) ) <EOL> for key , value in items : <EOL> try : <EOL> success_dict [ '<STR_LIT>' ] [ key ] = fr . get ( value ) <EOL> except KeyError : <EOL> pass <EOL> fr = sreg . SRegResponse . fromSuccessResponse ( info ) <EOL> if fr is not None : <EOL> sreg_required = get_sreg_required_from_settings ( settings ) <EOL> sreg_optional = get_sreg_optional_from_settings ( settings ) <EOL> items = chain ( sreg_required , sreg_optional ) <EOL> for key in items : <EOL> try : <EOL> success_dict [ '<STR_LIT>' ] [ key ] = fr . get ( key ) <EOL> except KeyError : <EOL> pass <EOL> callback = settings . get ( '<STR_LIT>' , None ) <EOL> if callback is not None : <EOL> log . info ( '<STR_LIT>' % callback ) <EOL> callback_function = get_callback ( callback ) <EOL> else : <EOL> callback_function = worthless_callback <EOL> return callback_function ( context , request , success_dict ) <EOL> def get_callback ( callback_string ) : <EOL> callback = callback_string . split ( '<STR_LIT::>' ) <EOL> try : <EOL> callback_module = __import__ ( callback [ <NUM_LIT:0> ] , fromlist = [ callback [ <NUM_LIT:1> ] ] ) <EOL> except ImportError : <EOL> return None <EOL> try : <EOL> callback_function = getattr ( callback_module , callback [ <NUM_LIT:1> ] ) <EOL> except AttributeError : <EOL> return None <EOL> return callback_function <EOL> def error_to_login_form ( request , message ) : <EOL> log . info ( '<STR_LIT>' % message ) <EOL> settings = request . registry . settings <EOL> error_url = settings . get ( '<STR_LIT>' , request . referrer ) <EOL> if error_url is None : <EOL> error_url = '<STR_LIT:/>' <EOL> error_flash_queue = settings . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> request . session . flash ( message , error_flash_queue ) <EOL> return HTTPFound ( location = error_url ) </s>
