Example #1
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')
Example #2
0
def social(request):
    # Main social network manager view
    # Lists all supported social networking services
    social_center = SocialCenter()
    
    sites = social_center.get_sites()
    
    for site_id, site in sites.items():
        site["auth_uri"] = reverse("socialnetwork.views.social_auth", kwargs={ "site": site_id })
        site["logout_uri"] = reverse("socialnetwork.views.social_logout", kwargs={ "site": site_id })
    
    return render(request, "socialnetwork-main.html", {
        "sites" : sites
    })
Example #3
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.")
Example #4
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)
Example #5
0
def social_post(request, site=None):
    # Social post publishing test endpoint
    # Requests from social_test refer to this endpoint
    logger.debug(request.POST)
    title = request.POST["postTitle"]
    content = request.POST["postContent"]
    link = request.POST["postLink"]
    social_center = SocialCenter()
    results = social_center.publish(title, content, link, site=site)
    logger.debug(results)
    
    failed_sites = {}
    for site, result in results.items():
        if "error" in result:
            failed_site = {}
            failed_site["error"] = result["error"]
            failed_site["name"] = result["name"]
            failed_sites[site] = failed_site
    if failed_sites:
        return HttpResponseServerError(json.dumps(failed_sites))
    return HttpResponse("OK")
Example #6
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")