Ejemplo n.º 1
0
def weibo_signin_check(request):
    logging.error(request)
    client = weibo_client(str(settings.WEIBO_API_KEY), str(settings.WEIBO_API_SECRET),urlparse.urljoin(settings.APP_URL,'/weibo/signin/check'))
    code = request.GET["code"]
    try:
        client.set_code(code)
        token = client.token 
    except:
         return HttpResponseRedirect(reverse('auth_signin'))

    try:
        weibo_uid = token["uid"]
        assoc = AuthKeyUserAssociation.objects.get(weibo_uid=weibo_uid)
        user_info=client.get('users/show', uid=token["uid"])
        user_ = assoc.user
        user_.weibo_gravatar=user_info["avatar_hd"]
        user_.save()
        assoc.weibo_remind_in = token["remind_in"]
        assoc.key = token["access_token"]
        assoc.weibo_expires_at = token["expires_at"]
        assoc.save()

        return login_and_forward(request, user_)

    except AuthKeyUserAssociation.DoesNotExist:
        assoc_key = str(token)
        request.session['assoc_key'] = assoc_key
        request.session['auth_provider'] = 'weibo'
        return HttpResponseRedirect(reverse('auth_external_register'))

    return HttpResponseRedirect(reverse('auth_signin'))
Ejemplo n.º 2
0
def weibo_get(user,method):
    try:
        assoc = AuthKeyUserAssociation.objects.filter(provider="weibo").get(user=user)
    except AuthKeyUserAssociation.DoesNotExist:
        return None

    token={}
    token["access_token"]=assoc.key
    token["uid"]=assoc.weibo_uid
    token["remind_in"]=assoc.weibo_remind_in
    token["expires_at"]=int(assoc.weibo_expires_at)
    client = weibo_client(str(settings.WEIBO_API_KEY), str(settings.WEIBO_API_SECRET),urlparse.urljoin(settings.APP_URL,'/weibo/signin/check'),token)
    return client.get(method, uid=token["uid"])
Ejemplo n.º 3
0
def weibo_post(user,status):
    try:
        assoc = AuthKeyUserAssociation.objects.filter(provider="weibo").get(user=user)
    except AuthKeyUserAssociation.DoesNotExist:
        return # the user is not registed by weibo

    token={}
    token["access_token"]=assoc.key
    token["uid"]=assoc.weibo_uid
    token["remind_in"]=assoc.weibo_remind_in
    token["expires_at"]=int(assoc.weibo_expires_at)
    client = weibo_client(str(settings.WEIBO_API_KEY), str(settings.WEIBO_API_SECRET),urlparse.urljoin(settings.APP_URL,'/weibo/signin/check'),token)
    client.post('statuses/update', status=status)
Ejemplo n.º 4
0
def external_register(request):
    if request.method == 'POST' and 'bnewaccount' in request.POST:
        form1 = SimpleRegistrationForm(request.POST)

        if form1.is_valid():
            user_ = User(username=form1.cleaned_data['username'], email=form1.cleaned_data['email'])
            user_.email_isvalid = request.session.get('auth_validated_email', '') == form1.cleaned_data['email']
            user_.set_unusable_password()

            if User.objects.all().count() == 0:
                user_.is_superuser = True
                user_.is_staff = True

            user_.save()

            try:
                assoc_key = request.session['assoc_key']
                auth_provider = request.session['auth_provider']
                if auth_provider == "weibo":
                    token = ast.literal_eval(assoc_key)
                    assoc_key = token["access_token"]
                    weibo_uid = token["uid"]
                    weibo_remind_in = token["remind_in"]
                    weibo_expires_at = token["expires_at"]

            except:
                request.session['auth_error'] = _(
                        "Oops, something went wrong in the middle of this process. Please try again. Note that you need to have cookies enabled for the authentication to work."
                        )
                logging.error("Missing session data when trying to complete user registration: %s" % ", ".join(
                        ["%s: %s" % (k, v) for k, v in request.META.items()]))
                return HttpResponseRedirect(reverse('auth_signin'))

            if auth_provider == "weibo":
                uassoc = AuthKeyUserAssociation(user=user_, key=assoc_key, provider=auth_provider,weibo_uid=weibo_uid,weibo_remind_in=weibo_remind_in,weibo_expires_at=weibo_expires_at)
            else:
                uassoc = AuthKeyUserAssociation(user=user_, key=assoc_key, provider=auth_provider)
            uassoc.save()

            UserJoinsAction(user=user_, ip=request.META['REMOTE_ADDR']).save()
            del request.session['assoc_key']
            del request.session['auth_provider']

            return login_and_forward(request, user_, message=_("A welcome email has been sent to your email address. "))
    else:
        auth_provider = request.session.get('auth_provider', None)
        if not auth_provider:
            request.session['auth_error'] = _(
                    "Oops, something went wrong in the middle of this process. Please try again.")
            logging.error("Missing session data when trying to complete user registration: %s" % ", ".join(
                    ["%s: %s" % (k, v) for k, v in request.META.items()]))
            return HttpResponseRedirect(reverse('auth_signin'))

        if auth_provider == 'weibo':
            assoc_key = request.session['assoc_key']
            token = ast.literal_eval(assoc_key)
            client = weibo_client(str(settings.WEIBO_API_KEY), str(settings.WEIBO_API_SECRET),urlparse.urljoin(settings.APP_URL,'/weibo/signin/check'),token)
            user=client.get('users/show', uid=token["uid"])
            username = user["name"]
            email = None
            provider_context = None

        else: # weibo user register
            provider_class = AUTH_PROVIDERS[auth_provider].consumer
            user_data = provider_class.get_user_data(request.session['assoc_key'])

            if not user_data:
                user_data = request.session.get('auth_consumer_data', {})

            username = user_data.get('username', '')
            email = user_data.get('email', '')

            provider_context = AUTH_PROVIDERS[request.session['auth_provider']].context

        if email:
            request.session['auth_validated_email'] = email

        form1 = SimpleRegistrationForm(initial={
        'next': '/',
        'username': username,
        'email': email,
        })


    return render_to_response('auth/complete.html', {
    'form1': form1,
    #'provider':provider_context and mark_safe(provider_context.human_name) or _('unknown'),
    #'login_type':provider_context.id,
    'gravatar_faq_url':reverse('faq') + '#gravatar',
    }, context_instance=RequestContext(request))
Ejemplo n.º 5
0
def weibo_signin(request):
    client = weibo_client(str(settings.WEIBO_API_KEY), str(settings.WEIBO_API_SECRET),urlparse.urljoin(settings.APP_URL,'/weibo/signin/check'))
    return HttpResponseRedirect(client.authorize_url)