def google_authorized(request):
    '''
    Connect google account information to the current logged user or one with
    same email. If no user is matched, creates a new one.
    '''
    csrf_token = request.GET.get('state', None)
    if not csrf_token or csrf_token != request.session['state']:
        return HttpResponse(status=403)  # csrf attack! get that bastard!

    error = request.GET.get('error', None)
    if error:
        error_description = request.GET.get('error_description', None)
        return dict(login_error='google', error_msg=error_description)

    # Step 2: Exchange the authorization code for an access_token
    redirect_uri = request.build_absolute_uri(reverse('google_authorized'))
    params = {
        'client_id': settings.GOOGLE_APP_ID,
        'client_secret': settings.GOOGLE_APP_SECRET,
        'code': request.GET.get('code'),     # code to exchange access_token
        'redirect_uri': redirect_uri,        # must be the same as in step 1
        'grant_type': 'authorization_code',  # just to fulfill the OAuth2 spec
    }
    url = 'https://accounts.google.com/o/oauth2/token'
    resp = requests.post(url, data=params)   # google requires POST, not GET
    access_data = simplejson.loads(resp.text)
    access_token = access_data['access_token']

    # Step 3: Accessing the API
    params = {
        # 'scope': 'https://www.googleapis.com/auth/userinfo.email',
        'access_token': access_data['access_token'],
    }
    url = 'https://www.googleapis.com/oauth2/v1/userinfo/'
    url += '?' + encode_querystring(params)
    data = simplejson.loads(requests.get(url).text)

    if request.user.is_authenticated():
        # if a user is already logged, then just connect social auth account
        credential, created = SocialAuth.objects.get_or_create(
            email=data['email'], provider=PROVIDERS['google'])
        if created:
            credential.user = request.user
        else:
            # merge users information
            pass
    else:
        user, created = get_or_create_user_by_credentials(data['email'],
                            PROVIDERS['google'], access_data=access_data)
        if created:
            user.name = data['name']
            user.save()
        auth_login(request, user)

    return redirect(request.session['next'] or reverse('root'))
def google_authorized(request):
    """
    Connect google account information to the current logged user or one with
    same email. If no user is matched, creates a new one.
    """
    csrf_token = request.GET.get("state", None)
    if not csrf_token or csrf_token != request.session["state"]:
        return HttpResponse(status=403)  # csrf attack! get that bastard!

    error = request.GET.get("error", None)
    if error:
        error_description = request.GET.get("error_description", None)
        return dict(login_error="google", error_msg=error_description)

    # Step 2: Exchange the authorization code for an access_token
    redirect_uri = request.build_absolute_uri(reverse("google_authorized"))
    params = {
        "client_id": settings.GOOGLE_APP_ID,
        "client_secret": settings.GOOGLE_APP_SECRET,
        "code": request.GET.get("code"),  # code to exchange access_token
        "redirect_uri": redirect_uri,  # must be the same as in step 1
        "grant_type": "authorization_code",  # just to fulfill the OAuth2 spec
    }
    url = "https://accounts.google.com/o/oauth2/token"
    resp = requests.post(url, data=params)  # google requires POST, not GET
    access_data = simplejson.loads(resp.text)
    access_token = access_data["access_token"]

    # Step 3: Accessing the API
    params = {
        # 'scope': 'https://www.googleapis.com/auth/userinfo.email',
        "access_token": access_data["access_token"]
    }
    url = "https://www.googleapis.com/oauth2/v1/userinfo/"
    url += "?" + encode_querystring(params)
    data = simplejson.loads(requests.get(url).text)

    if request.user.is_authenticated():
        # if a user is already logged, then just connect social auth account
        credential, created = SocialAuth.objects.get_or_create(email=data["email"], provider=PROVIDERS["google"])
        if created:
            credential.user = request.user
        else:
            # merge users information
            pass
    else:
        user, created = get_or_create_user_by_credentials(data["email"], PROVIDERS["google"], access_data=access_data)
        if created:
            user.name = data["name"]
            user.save()
            send_explanations_mail(user)
        auth_login(request, user)

    return redirect(request.session["next"] or reverse("root"))
Exemple #3
0
def facebook_authorized(request):
    '''
    Connect facebook account information to the current logged user or one with
    same email. If no user is matched, creates a new one.
    '''
    csrf_token = request.GET.get('state', None)
    if not csrf_token or csrf_token != request.session['state']:
        return HttpResponse(status=403)  # csrf attack! get that bastard!

    error = request.GET.get('error', None)
    if error:
        error_description = request.GET.get('error_description', None)
        return dict(login_error='facebook', error_msg=error_description)

    # Step 2: Exchange the authorization code for an access_token
    redirect_uri = request.build_absolute_uri(reverse('facebook_authorized'))
    params = {
        'client_id': settings.FACEBOOK_APP_ID,
        'client_secret': settings.FACEBOOK_APP_SECRET,
        'code': request.GET.get('code'),  # code to exchange for access_token
        'redirect_uri': redirect_uri,     # must be the same as in step1
    }
    url = 'https://graph.facebook.com/oauth/access_token'
    url += '?' + encode_querystring(params)
    access_data = decode_querystring(requests.get(url).text)

    # Step 3: Accessing the API
    params = {
        'fields': 'email,name',
        'access_token': access_data['access_token'],
    }
    url = 'https://graph.facebook.com/me'
    url += '?' + encode_querystring(params)
    data = simplejson.loads(requests.get(url).text)

    if request.user.is_authenticated():
        # if a user is already logged, then just connect social auth account
        credential, created = SocialAuth.objects.get_or_create(
            email=data['email'], provider=PROVIDERS['facebook'])
        if created:
            credential.user = request.user
        else:
            # merge users information
            pass
    else:
        user, created = get_or_create_user_by_credentials(data['email'],
                            PROVIDERS['facebook'], access_data=access_data)
        if created:
            user.name = data['name']
            user.save()
            send_explanations_mail(user)
        auth_login(request, user)

    return redirect(request.session['next'] or reverse('root'))
def facebook_authorized(request):
    """
    Connect facebook account information to the current logged user or one with
    same email. If no user is matched, creates a new one.
    """
    csrf_token = request.GET.get("state", None)
    if not csrf_token or csrf_token != request.session["state"]:
        return HttpResponse(status=403)  # csrf attack! get that bastard!

    error = request.GET.get("error", None)
    if error:
        error_description = request.GET.get("error_description", None)
        return dict(login_error="facebook", error_msg=error_description)

    # Step 2: Exchange the authorization code for an access_token
    redirect_uri = request.build_absolute_uri(reverse("facebook_authorized"))
    params = {
        "client_id": settings.FACEBOOK_APP_ID,
        "client_secret": settings.FACEBOOK_APP_SECRET,
        "code": request.GET.get("code"),  # code to exchange for access_token
        "redirect_uri": redirect_uri,  # must be the same as in step1
    }
    url = "https://graph.facebook.com/oauth/access_token"
    url += "?" + encode_querystring(params)
    access_data = decode_querystring(requests.get(url).text)

    # Step 3: Accessing the API
    params = {"fields": "email,name", "access_token": access_data["access_token"]}
    url = "https://graph.facebook.com/me"
    url += "?" + encode_querystring(params)
    data = simplejson.loads(requests.get(url).text)

    if request.user.is_authenticated():
        # if a user is already logged, then just connect social auth account
        credential, created = SocialAuth.objects.get_or_create(email=data["email"], provider=PROVIDERS["facebook"])
        if created:
            credential.user = request.user
        else:
            # merge users information
            pass
    else:
        user, created = get_or_create_user_by_credentials(data["email"], PROVIDERS["facebook"], access_data=access_data)
        if created:
            user.name = data["name"]
            user.save()
        auth_login(request, user)

    return redirect(request.session["next"] or reverse("root"))