diff options
| author | Tom Christie | 2013-01-21 21:29:49 +0000 |
|---|---|---|
| committer | Tom Christie | 2013-01-21 21:29:49 +0000 |
| commit | 65b62d64ec54b528b62a1500b8f6ffe216d45c09 (patch) | |
| tree | eb30f11fdb82a7940070cd9dca2d276c00cfb2ee /rest_framework/views.py | |
| parent | 36fa722ebb1b438b710b90fe470fbdbf82fd676e (diff) | |
| download | django-rest-framework-65b62d64ec54b528b62a1500b8f6ffe216d45c09.tar.bz2 | |
WWW-Authenticate responses
Diffstat (limited to 'rest_framework/views.py')
| -rw-r--r-- | rest_framework/views.py | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/rest_framework/views.py b/rest_framework/views.py index fdb373da..ac9b3385 100644 --- a/rest_framework/views.py +++ b/rest_framework/views.py @@ -148,7 +148,7 @@ class APIView(View): """ If request is not permitted, determine what kind of exception to raise. """ - if self.request.successful_authenticator: + if not self.request.successful_authenticator: raise exceptions.NotAuthenticated() raise exceptions.PermissionDenied() @@ -158,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(), @@ -321,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, |
