aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/views.py
diff options
context:
space:
mode:
authorTom Christie2013-01-26 21:37:43 +0000
committerTom Christie2013-01-26 21:37:43 +0000
commitb5d8f50f9dcace3ad3c708ed518f23ff260f6bea (patch)
tree7a0fb931ad5841b863359719075dd55f8370b3a1 /rest_framework/views.py
parent4eb5861f3676781493af29f8e9fd87ec22e591aa (diff)
parenta75db4cfb8ed756c451bfda7ea0c73a73859216f (diff)
downloaddjango-rest-framework-b5d8f50f9dcace3ad3c708ed518f23ff260f6bea.tar.bz2
Merge branch 'master' into many-fields
Diffstat (limited to 'rest_framework/views.py')
-rw-r--r--rest_framework/views.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/rest_framework/views.py b/rest_framework/views.py
index 10bdd5a5..ac9b3385 100644
--- a/rest_framework/views.py
+++ b/rest_framework/views.py
@@ -148,6 +148,8 @@ class APIView(View):
"""
If request is not permitted, determine what kind of exception to raise.
"""
+ if not self.request.successful_authenticator:
+ raise exceptions.NotAuthenticated()
raise exceptions.PermissionDenied()
def throttled(self, request, wait):
@@ -156,6 +158,15 @@ class APIView(View):
"""
raise exceptions.Throttled(wait)
+ def get_authenticate_header(self, request):
+ """
+ If a request is unauthenticated, determine the WWW-Authenticate
+ header to use for 401 responses, if any.
+ """
+ authenticators = self.get_authenticators()
+ if authenticators:
+ return authenticators[0].authenticate_header(request)
+
def get_parser_context(self, http_request):
"""
Returns a dict that is passed through to Parser.parse(),
@@ -319,6 +330,16 @@ class APIView(View):
# Throttle wait header
self.headers['X-Throttle-Wait-Seconds'] = '%d' % exc.wait
+ if isinstance(exc, (exceptions.NotAuthenticated,
+ exceptions.AuthenticationFailed)):
+ # WWW-Authenticate header for 401 responses, else coerce to 403
+ auth_header = self.get_authenticate_header(self.request)
+
+ if auth_header:
+ self.headers['WWW-Authenticate'] = auth_header
+ else:
+ exc.status_code = status.HTTP_403_FORBIDDEN
+
if isinstance(exc, exceptions.APIException):
return Response({'detail': exc.detail},
status=exc.status_code,