Beispiel #1
0
def process_trust_result(request):
    """
    Handle the result of a trust decision and respond to the RP
    accordingly.
    """
    # Get the request from the session so we can construct the
    # appropriate response.
    openid_request = request.session.get('openid_request')

    # The identifier that this server can vouch for
    my_url = reverse(profile, kwargs={'username': request.user.username})
    response_identity = request.build_absolute_uri(my_url)

    # If the decision was to allow the verification, respond
    # accordingly.
    allowed = 'allow' in request.POST

    # Generate a response with the appropriate answer.
    openid_response = openid_request.answer(allowed,
                                            identity=response_identity)

    # Send Simple Registration data in the response, if appropriate.
    if allowed:
        try:
            user_profile = request.user.get_profile()
        except Profile.DoesNotExist:
            user_profile = Profile(user=request.user)

        sreg_data = user_profile.as_sreg_data()
        ax_data = user_profile.as_ax_data()

        # We only got share_activity if show_decide_page() found the AX
        # request asked for the callback, so we can do heavy work like
        # saving a token.
        if 'share_activity' in request.POST:
            token = SaveActivityHookToken(user=request.user)
            token.save()
            callback = reverse('identity.views.save_activity_hook',
                kwargs={'token': token.token})
            callback = request.build_absolute_uri(callback)
            ax_data['http://activitystrea.ms/axschema/callback'] = callback
            log.debug('Adding %r to AX response as callback', callback)
        else:
            log.debug('User chose not to share activity, so not sending callback')

        sreg_req = sreg.SRegRequest.fromOpenIDRequest(openid_request)
        sreg_resp = sreg.SRegResponse.extractResponse(sreg_req, sreg_data)
        openid_response.addExtension(sreg_resp)

        ax_req = ax.FetchRequest.fromOpenIDRequest(openid_request)
        if ax_req is not None:
            ax_resp = ax.FetchResponse(ax_req)
            for uri, value in ax_data.items():
                if value and uri in ax_req:
                    ax_resp.addValue(uri, value)
        openid_response.addExtension(ax_resp)

    return display_response(request, openid_response)