Esempio n. 1
0
def social_page_select(request, site):
    # Social site page selection endpoint
    social_center = SocialCenter()
    
    if not social_center.has_site(site):
        return HttpResponseNotFound("Site not found")
    
    if not social_center.must_select_page(site):
        return HttpResponseServerError("Site does not support pages.")
    
    main_token_key = "%s_main_token" % site
    
    main_token = request.session[main_token_key]
    del request.session[main_token_key]
    
    page_id = request.POST["pageId"]
    
    social_center = SocialCenter()
    result = social_center.authenticate(site, main_token, page_id)
    
    logger.debug(result)
    
    if "main_token" in result:
        return HttpResponse("OK")
    else:
        return HttpResponseServerError("Could not obtain page token.")
Esempio n. 2
0
def social_logout(request, site):
    # Social site logout endpoint
    # Causes the authentication tokens of the site to be deleted.
    social_center = SocialCenter()
    if not social_center.has_site(site):
        return HttpResponseNotFound("Site not found")
    
    social_center.logout(site)
    return redirect('socialnetwork.views.social')
Esempio n. 3
0
def social_auth(request, site):
    # Social site authentication view
    # Redirects to the site after some simple processing.
    
    callback_url = request.build_absolute_uri(reverse('socialnetwork.views.social_callback', kwargs={ "site" : site }))
    auth_data_key = "%s_auth_data" % site
    
    social_center = SocialCenter()
    if not social_center.has_site(site):
        return HttpResponseNotFound("Site not found")
    if social_center.is_logged_in(site):
        return redirect("socialnetwork.views.social")
    
    oauth_url, auth_data = social_center.start_authentication(site, callback_url)
    
    request.session[auth_data_key] = auth_data
    
    return redirect(oauth_url)
Esempio n. 4
0
def social_callback(request, site):
    # Social site callback view
    # Retrieves the tokens from the authentication response.
    social_center = SocialCenter()
    
    if not social_center.has_site(site):
        return HttpResponseNotFound("Site not found")
    
    if "error" in request.GET:
        return HttpResponseServerError("ERROR: " + request.GET["error"])
    
    auth_data_key = "%s_auth_data" % site
    main_token_key = "%s_main_token" % site
    client_token_name = social_center.get_client_token_name(site)
    
    auth_data = request.session[auth_data_key]
    client_token = request.GET[client_token_name]
    
    del request.session[auth_data_key]
    
    result = social_center.process_client_token(site, client_token, auth_data)
    logger.debug(result)
    
    if "main_token" not in result:
        return HttpResponseServerError("Could not retrieve token")
    
    main_token = result["main_token"]
    
    if social_center.must_select_page(site):
        pages = social_center.get_pages(site, main_token)
        request.session[main_token_key] = main_token
            
        return render(request, "socialnetwork-select-page.html", {
            "pages" : pages,
            "root_uri" : reverse("socialnetwork.views.social"),
            "process_uri" : reverse("socialnetwork.views.social_page_select", kwargs={ "site" : site })
        })
    else:
        social_center.authenticate(site,
                                    main_token,
                                    result.get("sub_token", None))
        return redirect("socialnetwork.views.social")