aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/views.py
diff options
context:
space:
mode:
authorTom Christie2013-01-22 09:12:48 -0800
committerTom Christie2013-01-22 09:12:48 -0800
commitdd10d538ffc8f76ccc670f65da2220b09c22688c (patch)
tree1af09c7dbcc939c749d30adf25b14d232200f44f /rest_framework/views.py
parente29ba356f054222893655901923811bd9675d4cc (diff)
parentb7ab2aee46c718f683b19eefba1b48f233da40e4 (diff)
downloaddjango-rest-framework-dd10d538ffc8f76ccc670f65da2220b09c22688c.tar.bz2
Merge pull request #416 from tomchristie/unauthenticated_response
Unauthenticated requests - 401 vs 403 responses
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,