Beispiel #1
0
def _user_needs_languages(context):
    user = context['user']
    if user.is_authenticated():
        return not user.userlanguage_set.exists()
    else:
        print get_user_languages_from_cookie(context['request'])
        return not get_user_languages_from_cookie(context['request'])
Beispiel #2
0
def facebook_login_done(request):
    """ for dialog users """
    try:
        code = request.GET['code']
    except KeyError:
        # code not passed as GET variable, redirect to login page
        return HttpResponseRedirect(reverse('socialauth_login_page'))
    params = {'client_id': settings.FACEBOOK_APP_ID,
        'redirect_uri': 'http://mirosubs.example.com:8000/en/auth/facebook_login_done/',
        'client_secret': settings.FACEBOOK_APP_SECRET,
        'code': code}
    token_url = 'https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(params)
    response = urllib.urlopen(token_url).read() # string containing token and time for expiry
    token = dict(urlparse.parse_qsl(response))
    user = authenticate(fb_access_token = token['access_token'])

    if user:
        if not user.userlanguage_set.exists():
            langs = get_user_languages_from_cookie(request)
            for l in langs:
                UserLanguage.objects.get_or_create(user=user, language=l)       
            auth_login(request, user)
        else:
            # We were not able to authenticate user
            # Redirect to login page
            return HttpResponseRedirect(reverse('socialauth_login_page'))
    # authentication was successful, use is now logged in
    # facebook doesnot allow get variables in its authorize url, no next parameter
    return HttpResponseRedirect('/widget/close_window/')
Beispiel #3
0
def facebook_login_done(request):
    """ for dialog users """
    try:
        code = request.GET['code']
    except KeyError:
        # code not passed as GET variable, redirect to login page
        return HttpResponseRedirect(reverse('socialauth_login_page'))
    params = {
        'client_id': settings.FACEBOOK_APP_ID,
        'redirect_uri':
        'http://mirosubs.example.com:8000/en/auth/facebook_login_done/',
        'client_secret': settings.FACEBOOK_APP_SECRET,
        'code': code
    }
    token_url = 'https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(
        params)
    response = urllib.urlopen(
        token_url).read()  # string containing token and time for expiry
    token = dict(urlparse.parse_qsl(response))
    user = authenticate(fb_access_token=token['access_token'])

    if user:
        if not user.userlanguage_set.exists():
            langs = get_user_languages_from_cookie(request)
            for l in langs:
                UserLanguage.objects.get_or_create(user=user, language=l)
            auth_login(request, user)
        else:
            # We were not able to authenticate user
            # Redirect to login page
            return HttpResponseRedirect(reverse('socialauth_login_page'))
    # authentication was successful, use is now logged in
    # facebook doesnot allow get variables in its authorize url, no next parameter
    return HttpResponseRedirect('/widget/close_window/')
Beispiel #4
0
def openid_done(request, provider=None, confirmed=True):
    """
    When the request reaches here, the user has completed the Openid
    authentication flow. He has authorised us to login via Openid, so
    request.openid is populated.
    After coming here, we want to check if we are seeing this openid first time.
    If we are, we will create a new Django user for this Openid, else login the
    existing openid.
    """
    if not provider:
        provider = request.session.get('openid_provider', '')
    if request.openid:
        #check for already existing associations
        openid_key = str(request.openid)
        #authenticate and login
        if not confirmed:
            (existing, suggested_email) = OpenIdBackend.pre_authenticate(
                openid_key=openid_key, request=request, provider=provider)
            if not existing:
                if provider == 'Udacity':
                    return redirect('auth:confirm_create_user', 'udacity',
                                    suggested_email)
                elif provider == 'Openid':
                    openid_url = request.GET.get('openid_url', '')
                    response = redirect('auth:confirm_create_user', 'openid',
                                        suggested_email)
                    if openid_url:
                        response['Location'] += '?' + urlencode(
                            {'openid_url': openid_url})
                    return response
                else:
                    return redirect(
                        reverse('auth:confirm_create_user', provider,
                                suggested_email))
        email = request.GET.get('email', None)
        user = authenticate(openid_key=openid_key,
                            request=request,
                            provider=provider,
                            email=email)
        if user:
            if not user.userlanguage_set.exists():
                langs = get_user_languages_from_cookie(request)
                for l in langs:
                    UserLanguage.objects.get_or_create(user=user, language=l)

            login(request, user)
            next = None
            if 'openid_next' in request.session:
                next = request.session.get('openid_next')
            if 'next' in request.GET:
                next = request.GET['next']
            if next is not None and len(next.strip()) > 0:
                return HttpResponseRedirect(next)
            redirect_url = reverse('profiles:profile', args=(user, ))
            return HttpResponseRedirect(redirect_url)
        else:
            return HttpResponseRedirect(settings.LOGIN_URL)
    else:
        return HttpResponseRedirect(settings.LOGIN_URL)
Beispiel #5
0
def create_user(request):
    redirect_to = make_redirect_to(request)
    form = CustomUserCreationForm(request.POST, label_suffix="")
    if form.is_valid():
        new_user = form.save()
        user = authenticate(username=new_user.username,
                            password=form.cleaned_data['password1'])
        langs = get_user_languages_from_cookie(request)
        for l in langs:
            UserLanguage.objects.get_or_create(user=user, language=l)        
        auth_login(request, user)
        return HttpResponseRedirect(redirect_to)
    else:
        return render_login(request, form, AuthenticationForm(label_suffix=""), redirect_to)
Beispiel #6
0
def create_user(request):
    redirect_to = make_redirect_to(request)
    form = CustomUserCreationForm(request.POST, label_suffix="")
    if form.is_valid():
        new_user = form.save()
        user = authenticate(username=new_user.username,
                            password=form.cleaned_data['password1'])
        langs = get_user_languages_from_cookie(request)
        for l in langs:
            UserLanguage.objects.get_or_create(user=user, language=l)
        auth_login(request, user)
        return HttpResponseRedirect(redirect_to)
    else:
        return render_login(request, form, AuthenticationForm(label_suffix=""), redirect_to)
Beispiel #7
0
def twitter_login_done(request):
    request_token = request.session.get('request_token', None)
    oauth_verifier = request.GET.get("oauth_verifier", None)

    # If there is no request_token for session,
    # Means we didn't redirect user to twitter
    if not request_token:
            # Redirect the user to the login page,
            # So the user can click on the sign-in with twitter button
            return HttpResponse("We didn't redirect you to twitter...")
    
    token = oauth.OAuthToken.from_string(request_token)
    
    # If the token from session and token from twitter does not match
    #   means something bad happened to tokens
    if token.key != request.GET.get('oauth_token', 'no-token'):
            del request.session['request_token']
            # Redirect the user to the login page
            return HttpResponse("Something wrong! Tokens do not match...")
    
    twitter = oauthtwitter.TwitterOAuthClient(settings.TWITTER_CONSUMER_KEY, settings.TWITTER_CONSUMER_SECRET)  
    access_token = twitter.fetch_access_token(token, oauth_verifier)
    
    request.session['access_token'] = access_token.to_string()
    user = authenticate(access_token=access_token)
    
    # if user is authenticated then login user
    if user:
        if not user.userlanguage_set.exists():
            langs = get_user_languages_from_cookie(request)
            for l in langs:
                UserLanguage.objects.get_or_create(user=user, language=l)        
        login(request, user)
    else:
        # We were not able to authenticate user
        # Redirect to login page
        del request.session['access_token']
        del request.session['request_token']
        return HttpResponseRedirect(reverse('socialauth_login_page'))

    # authentication was successful, use is now logged in
    return HttpResponseRedirect(request.GET.get('next', settings.LOGIN_REDIRECT_URL))
Beispiel #8
0
def openid_done(request, provider=None):
    """
    When the request reaches here, the user has completed the Openid
    authentication flow. He has authorised us to login via Openid, so
    request.openid is populated.
    After coming here, we want to check if we are seeing this openid first time.
    If we are, we will create a new Django user for this Openid, else login the
    existing openid.
    """
    if not provider:
        provider = request.session.get('openid_provider', '')
    if request.openid:
        #check for already existing associations
        openid_key = str(request.openid)
        #authenticate and login
        user = authenticate(openid_key=openid_key,
                            request=request,
                            provider=provider)
        if user:
            if not user.userlanguage_set.exists():
                langs = get_user_languages_from_cookie(request)
                for l in langs:
                    UserLanguage.objects.get_or_create(user=user, language=l)

            login(request, user)
            next = None
            if 'openid_next' in request.session:
                next = request.session.get('openid_next')
            if 'next' in request.GET:
                next = request.GET['next']
            if next is not None and len(next.strip()) > 0:
                return HttpResponseRedirect(next)
            redirect_url = reverse('profiles:my_profile')
            return HttpResponseRedirect(redirect_url)
        else:
            return HttpResponseRedirect(settings.LOGIN_URL)
    else:
        return HttpResponseRedirect(settings.LOGIN_URL)
Beispiel #9
0
def openid_done(request, provider=None):
    """
    When the request reaches here, the user has completed the Openid
    authentication flow. He has authorised us to login via Openid, so
    request.openid is populated.
    After coming here, we want to check if we are seeing this openid first time.
    If we are, we will create a new Django user for this Openid, else login the
    existing openid.
    """
    if not provider:
        provider = request.session.get('openid_provider', '')
    if  request.openid:
        #check for already existing associations
        openid_key = str(request.openid)
        #authenticate and login
        user = authenticate(openid_key=openid_key, request=request, provider = provider)
        if user:
            if not user.userlanguage_set.exists():
                langs = get_user_languages_from_cookie(request)
                for l in langs:
                    UserLanguage.objects.get_or_create(user=user, language=l)
                    
            login(request, user)
            next = None
            if 'openid_next' in request.session:
                next = request.session.get('openid_next')
            if 'next' in request.GET:
                next = request.GET['next']
            if next is not None and len(next.strip()) >  0 :
                return HttpResponseRedirect(next)    
            redirect_url = reverse('profiles:my_profile')
            return HttpResponseRedirect(redirect_url)
        else:
            return HttpResponseRedirect(settings.LOGIN_URL)
    else:
        return HttpResponseRedirect(settings.LOGIN_URL)
Beispiel #10
0
def _user_needs_languages(context):
    user = context['user']
    if user.is_authenticated():
        return not user.userlanguage_set.exists()
    else:
        return not bool(get_user_languages_from_cookie(context['request']))
Beispiel #11
0
            TwitterAccount.objects.create(
                user=request.user,
                username=username,
                access_token=access_token.to_string())

        del request.session['no-login']
        messages.info(request, 'Successfully linked a Twitter account')
        return redirect('profiles:account')

    request.session['access_token'] = access_token.to_string()
    user = authenticate(access_token=access_token)

    # if user is authenticated then login user
    if user:
        if not user.userlanguage_set.exists():
            langs = get_user_languages_from_cookie(request)
            for l in langs:
                UserLanguage.objects.get_or_create(user=user, language=l)
        login(request, user)
    else:
        # We were not able to authenticate user
        # Redirect to login page
        del request.session['access_token']
        del request.session['request_token']
        return HttpResponseRedirect(reverse('socialauth_login_page'))

    # authentication was successful, use is now logged in
    return HttpResponseRedirect(
        request.GET.get('next', settings.LOGIN_REDIRECT_URL))