<s> from django import http <EOL> from django . http import HttpResponseRedirect <EOL> from django . views . generic . simple import direct_to_template <EOL> from openid . consumer import consumer <EOL> from openid . consumer . discover import DiscoveryFailure <EOL> from openid . extensions import ax , pape , sreg <EOL> from openid . yadis . constants import YADIS_HEADER_NAME , YADIS_CONTENT_TYPE <EOL> from openid . server . trustroot import RP_RETURN_TO_URL_TYPE <EOL> from djopenid import util <EOL> PAPE_POLICIES = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> POLICY_PAIRS = [ ( p , getattr ( pape , p ) ) <EOL> for p in PAPE_POLICIES ] <EOL> def getOpenIDStore ( ) : <EOL> """<STR_LIT>""" <EOL> return util . getOpenIDStore ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def getConsumer ( request ) : <EOL> """<STR_LIT>""" <EOL> return consumer . Consumer ( request . session , getOpenIDStore ( ) ) <EOL> def renderIndexPage ( request , ** template_args ) : <EOL> template_args [ '<STR_LIT>' ] = util . getViewURL ( request , startOpenID ) <EOL> template_args [ '<STR_LIT>' ] = POLICY_PAIRS <EOL> response = direct_to_template ( <EOL> request , '<STR_LIT>' , template_args ) <EOL> response [ YADIS_HEADER_NAME ] = util . getViewURL ( request , rpXRDS ) <EOL> return response <EOL> def startOpenID ( request ) : <EOL> """<STR_LIT>""" <EOL> if request . POST : <EOL> openid_url = request . POST [ '<STR_LIT>' ] <EOL> c = getConsumer ( request ) <EOL> error = None <EOL> try : <EOL> auth_request = c . begin ( openid_url ) <EOL> except DiscoveryFailure , e : <EOL> error = "<STR_LIT>" % ( str ( e ) , ) <EOL> if error : <EOL> return renderIndexPage ( request , error = error ) <EOL> sreg_request = sreg . SRegRequest ( optional = [ '<STR_LIT:email>' , '<STR_LIT>' ] , <EOL> required = [ '<STR_LIT>' ] ) <EOL> auth_request . addExtension ( sreg_request ) <EOL> ax_request = ax . FetchRequest ( ) <EOL> ax_request . add ( <EOL> ax . AttrInfo ( '<STR_LIT>' , <EOL> required = True ) ) <EOL> ax_request . add ( <EOL> ax . AttrInfo ( '<STR_LIT>' , <EOL> required = False , count = ax . UNLIMITED_VALUES ) ) <EOL> auth_request . addExtension ( ax_request ) <EOL> requested_policies = [ ] <EOL> policy_prefix = '<STR_LIT>' <EOL> for k , v in request . POST . iteritems ( ) : <EOL> if k . startswith ( policy_prefix ) : <EOL> policy_attr = k [ len ( policy_prefix ) : ] <EOL> if policy_attr in PAPE_POLICIES : <EOL> requested_policies . append ( getattr ( pape , policy_attr ) ) <EOL> if requested_policies : <EOL> pape_request = pape . Request ( requested_policies ) <EOL> auth_request . addExtension ( pape_request ) <EOL> trust_root = util . getViewURL ( request , startOpenID ) <EOL> return_to = util . getViewURL ( request , finishOpenID ) <EOL> if auth_request . shouldSendRedirect ( ) : <EOL> url = auth_request . redirectURL ( trust_root , return_to ) <EOL> return HttpResponseRedirect ( url ) <EOL> else : <EOL> form_id = '<STR_LIT>' <EOL> form_html = auth_request . formMarkup ( trust_root , return_to , <EOL> False , { '<STR_LIT:id>' : form_id } ) <EOL> return direct_to_template ( <EOL> request , '<STR_LIT>' , { '<STR_LIT:html>' : form_html } ) <EOL> return renderIndexPage ( request ) <EOL> def finishOpenID ( request ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> request_args = util . normalDict ( request . GET ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> request_args . update ( util . normalDict ( request . POST ) ) <EOL> if request_args : <EOL> c = getConsumer ( request ) <EOL> return_to = util . getViewURL ( request , finishOpenID ) <EOL> response = c . complete ( request_args , return_to ) <EOL> sreg_response = { } <EOL> ax_items = { } <EOL> if response . status == consumer . SUCCESS : <EOL> sreg_response = sreg . SRegResponse . fromSuccessResponse ( response ) <EOL> ax_response = ax . FetchResponse . fromSuccessResponse ( response ) <EOL> if ax_response : <EOL> ax_items = { <EOL> '<STR_LIT>' : ax_response . get ( <EOL> '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ax_response . get ( <EOL> '<STR_LIT>' ) , <EOL> } <EOL> pape_response = None <EOL> if response . status == consumer . SUCCESS : <EOL> pape_response = pape . Response . fromSuccessResponse ( response ) <EOL> if not pape_response . auth_policies : <EOL> pape_response = None <EOL> results = { <EOL> consumer . CANCEL : <EOL> { '<STR_LIT:message>' : '<STR_LIT>' } , <EOL> consumer . FAILURE : <EOL> { '<STR_LIT:error>' : '<STR_LIT>' } , <EOL> consumer . SUCCESS : <EOL> { '<STR_LIT:url>' : response . getDisplayIdentifier ( ) , <EOL> '<STR_LIT>' : sreg_response and sreg_response . items ( ) , <EOL> '<STR_LIT>' : ax_items . items ( ) , <EOL> '<STR_LIT>' : pape_response } <EOL> } <EOL> result = results [ response . status ] <EOL> if isinstance ( response , consumer . FailureResponse ) : <EOL> result [ '<STR_LIT>' ] = response . message <EOL> return renderIndexPage ( request , ** result ) <EOL> def rpXRDS ( request ) : <EOL> """<STR_LIT>""" <EOL> return util . renderXRDS ( <EOL> request , <EOL> [ RP_RETURN_TO_URL_TYPE ] , <EOL> [ util . getViewURL ( request , finishOpenID ) ] ) </s>
