Beispiel #1
0
def openid_chalange(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect("/")

    class OpenidForm(Form):
        openid = CharField(required=True, max_length=32)

        def clean_openid(self):
            openid = self.cleaned_data['openid']
            import re
            p = re.compile('^[a-zA-Z._-]+$')
            if not p.match(openid):
                raise ValidationError(_("Invalid openid string."))

    form = None

    if (request.POST):
        form = OpenidForm(request.POST)

        if form.is_valid():
            store = getOpenIDStore("/tmp/taverna_openid", "c_")
            c = consumer.Consumer(request.session, store)
            openid_url = request.POST['openid']


            # Google ...
            if openid_url == "google":
                openid_url = 'https://www.google.com/accounts/o8/id'
            try:
                auth_request = c.begin(openid_url)
            except consumer.DiscoveryFailure, exc:
                error = "OpenID discovery error: %s" % str(exc)
                return {'form': form, 'error': error}

            trust_root = getViewURL(request, openid_chalange)
            redirect_to = getViewURL(request, openid_finish)
            return HttpResponseRedirect(
                       auth_request.redirectURL(trust_root, redirect_to)
                   )
Beispiel #2
0
def getOpenIDStore():
    """
    Return an OpenID store object fit for the currently-chosen
    database backend, if any.
    """
    return util.getOpenIDStore('/tmp/djopenid_c_store', 'c_')
Beispiel #3
0
def openid_finish(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect("/")

    form = None
    error = None
    request_args = request.GET

    store = getOpenIDStore("/tmp/taverna_openid", "c_")
    c = consumer.Consumer(request.session, store)

    return_to = getViewURL(request, openid_finish)
    response = c.complete(request_args, return_to)

    if response.status == consumer.SUCCESS:
        openid_hash=sha512(response.getDisplayIdentifier()).hexdigest()
        sreg_response = sreg.SRegResponse.fromSuccessResponse(response)

        try:
            profile = Profile.objects.get(openid_hash=openid_hash)
            username = profile.user.username
            user = authenticate(username=username)
            if user is not None:
                login(request, user)

            return HttpResponseRedirect("/")
        except Profile.DoesNotExist:
            user = User(
                       username=openid_hash[:30],
                       is_staff=False,
                       is_active=True,
                       is_superuser=False
                   )
            user.save()
            profile = Profile(
                          user=user,
                          photo="",
                          openid=response.getDisplayIdentifier(),
                          openid_hash=openid_hash,
                          karma=settings.START_RATING,
                          force=settings.START_RATING
                      )
            profile.save()
            try:
                blog = Blog.objects.get(owner=user)
            except Blog.DoesNotExist:
                blog = Blog(owner=user, name=openid_hash[:30])
                blog.save()

            auth = authenticate(username=user.username)
            if user is not None:
                login(request, auth)

            return HttpResponseRedirect(
                       reverse("userauth.views.profile_edit")
                   )
    else:
        error = "Verification of %s failed: %s" % (
                    response.getDisplayIdentifier(),
                    response.message
                )

    return {'from': form, 'error': error}
Beispiel #4
0
def getOpenIDStore():
    """
    Return an OpenID store object fit for the currently-chosen
    database backend, if any.
    """
    return util.getOpenIDStore('/tmp/djopenid_c_store', 'c_')