Example #1
0
def oauth_start(request, scope=None, redirect_to=None):
    """Redirect to appropriate OAuth view in order to start
    OAuth login procedure / permission asking.
    
    :param scope: List of permissions to require
    :param redirect_to: Page where to redirect after OAuth.
        Defaults to current page.
    """
    
    ## Redirect URL, where to go after click in dialog
    ## This is our fb_oauth view
    redirect_to = request.build_absolute_uri(next)
    _oauth_page = reverse('django_fbcanvas.views.fb_oauth')
    _args = QueryDict("", True)
    _args['next'] = redirect_to
    _oauth_page += "?%s" % _args.urlencode()
    
    ## Permissions to be asked (aka SCOPE)
    if scope is None:
        scope = fb_settings.FACEBOOK_DEFAULT_SCOPE
    else:
        scope = str_to_list(scope)
    
    ## State, for CSRF prevention
    _state = str(hashlib.md5(uuid.uuid1()).hexdigest())
    request.session['facebook_oauth_state'] = _state
    
    ## Build OAuth dialog URL
    qd = QueryDict('', True)
    qd['client_id'] = fb_settings.FACEBOOK_APP_ID
    qd['redirect_uri'] = _oauth_page
    qd['state'] = _state
    qd['scope'] = scope
    
    dialog_url = "https://www.facebook.com/dialog/oauth?%s" % qd.urlencode()
    
    return HttpResponseRedirect(dialog_url)
def facebook_required(view_func=None, scope=fb_settings.FACEBOOK_DEFAULT_SCOPE,
                      redirect_field_name=REDIRECT_FIELD_NAME, login_url=None,
                      extra_params=None):
    """Decorator which makes the view require the given Facebook
    permissions, redirecting to the authorization page if necessary.

    .. NOTE::
       This implementation sends a request to check that the user
       has the required permissions before executing the view.
       
       This should be able to prevent most failures, but it will slow
       down things as an additional HTTP request will be performed
       on each request..

    :param view_func: The view function that will be decorated
    :param scope: List of names of permissions that will be required
    :param redirect_field_name:
    :param login_url: URL of the login page, in case permissions
        checking fails.
    :param extra_params: Extra paramters to be added to redirect_uri
    """
    #from django_facebook.utils import test_permissions
    if scope:
        scope_list = str_to_list(scope, separator=",")
    else:
        scope_list = fb_settings.FACEBOOK_DEFAULT_SCOPE
    
    def actual_decorator(view_func):
        @wraps(view_func, assigned=available_attrs(view_func))
        def _wrapped_view(request, *args, **kwargs):
            
            ## If the user is not logged in -> go to oauth
            ## If the logged-in user is not connected with facebook -> go to oauth
            
            if not request.user.is_authenticated() \
                or not request.user.facebookuser.access_token:
                return oauth_start(request, scope=scope_list)
            
            
            return view_func(request, *args, **kwargs)
            
            try:
                return view_func(request, *args, **kwargs)
            except OpenFacebookException, e:
                ## Check whether the user has required permissions
                ## if so, re-raise exception.
                ## Else, redirect to oauth url
                
                return oauth_start(request, scope=scope_list)
            
#            oauth_url, redirect_uri = get_oauth_url(request, scope_list)
#            if test_permissions(request, scope_list, redirect_uri):
#                return view_func(request, *args, **kwargs)
#            else:
#                logger.info('requesting access with redirect uri: %s', redirect_uri)
#                _canvas = canvas # Bring into local scope
#                if _canvas is None:
#                    _canvas = getattr(request, 'fb_info', {}).get('is_canvas', False)
#                response = response_redirect(oauth_url, canvas=_canvas)
#                return response
        return _wrapped_view