Ejemplo n.º 1
0
def complete(request):
    csr = consumer.Consumer(request.session, OpenIDStore())
    resp = csr.complete(request.GET, request.build_absolute_uri())

    if isinstance(resp, consumer.CancelResponse):
        return HttpResponseRedirect(reverse('home'))

    elif isinstance(resp, consumer.FailureResponse):
        request.flash.put(error=resp.message)
        return HttpResponseRedirect(reverse('signin'))

    elif isinstance(resp, consumer.SuccessResponse):
        OpenIDStore.make_person_from_response(resp)
        request.session['openid'] = resp.identity_url

        # if the id provider returns an activity callback, 
        # we'll post the user's activity stream there
        fr = ax.FetchResponse.fromSuccessResponse(resp)
        if fr is not None:
            callback = fr.getSingle('http://activitystrea.ms/axschema/callback')
            if callback:
                log.debug("Posting user's activity feed back to %r", callback)

                # post the user's stream to the callback
                feed_url = reverse('activity_feed', kwargs={'openid': resp.identity_url})
                data = {
                    'feed_uri': request.build_absolute_uri(feed_url),
                }

                h = httplib2.Http()
                try:
                    resp, content = h.request(callback, method="POST", body=urlencode(data))
                except Exception, exc:
                    log.debug("From callback got %s: %s", type(exc).__name__, str(exc))
                else:
                    if resp['content-type'].startswith('text/plain'):
                        log.debug("From callback got %d %s response: %s", resp.status, resp.reason, content)
                    else:
                        log.debug("From callback got %d %s %s response", resp.status, resp.reason, resp['content-type'])
            else:
                log.debug("Callback was %r so not posting back", callback)
        else:
            log.debug("AX response was %r so not posting back", fr)

        return HttpResponseRedirect(reverse('home'))
Ejemplo n.º 2
0
    try:
        ar = csr.begin(to_url)
    except discover.DiscoveryFailure, exc:
        request.flash.put(error="That doesn't appear to be someone's OpenID: %s"
            % exc.message)
        return home(request, {
            'person_to': to_url,
            'message': message,
        })

    openid_url = ar.endpoint.claimed_id
    try:
        person_to = Person.objects.get(openid=openid_url)
    except Person.DoesNotExist:
        person_to = Person(openid=openid_url)
        person_to.name = OpenIDStore.default_name_for_url(openid_url)
        person_to.save()

    thanks = Thanks()
    thanks.person_from = request.user
    thanks.person_to = person_to
    thanks.message = message
    thanks.save()

    request.flash.put(message='Your thanks have been recorded!')

    # Tell the pubsub hub about it.
    activity_url = reverse('activity_feed',
        kwargs={'openid': request.user.openid})
    activity_url = request.build_absolute_uri(activity_url)
    publ_data = {