I've been spending hours trying to find a way to have Basic HTTP Authentication for one of my views. These are several of the solutions I've tried but have had no success. The request is still be processed even with no authentication. I'm using version 1.4.3 of Django. Here is my Django view:
@csrf_exempt
def facebook(request):
if request.user.is_authenticated():
fb_value= ast.literal_eval(request.body)
queryset = Poster.objects.all().filter(fb_id__in = fb_value.values())
data = serializers.serialize('json', queryset, fields = ('picture','fb_id',))
return HttpResponse(data, 'application/javascript')
else:
return HttpResponse("This user is not authenticated")
I sent in the request without authentication, and it still returned results. This is not suppose to happen.
Another solution I tried was from a Django Snippet I found called, view by view basic authentication decorator
I made a httpauth.py and copied the code over from the snippet:
from mydjangoapp.httpauth import *
@csrf_exempt
@logged_in_or_basicauth()
def facebook(request):
fb_value= ast.literal_eval(request.body)
queryset = Poster.objects.all().filter(fb_id__in = fb_value.values())
data = serializers.serialize('json', queryset, fields = ('picture','fb_id',))
return HttpResponse(data, 'application/javascript')
I sent the request without authentication, and it still returned results. After exhausting all options, I turned to Django's very own @login_required decorator:
from django.contrib.auth.decorators import login_required
@csrf_exempt
@login_required
def facebook(request):
fb_value= ast.literal_eval(request.body)
queryset = Poster.objects.all().filter(fb_id__in = fb_value.values())
data = serializers.serialize('json', queryset, fields = ('picture','fb_id',))
return HttpResponse(data, 'application/javascript')
Here is more information about my settings.py:
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
)
After trying all of these options, I don't know what to do. Is there anything I am missing here?!