Skip to content

Fix the response status code when authenticating with wrong credentials #9655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions rest_framework/authtoken/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.utils.translation import gettext_lazy as _

from rest_framework import serializers
from rest_framework.exceptions import AuthenticationFailed


class AuthTokenSerializer(serializers.Serializer):
Expand Down Expand Up @@ -32,8 +33,7 @@ def validate(self, attrs):
# users. (Assuming the default ModelBackend authentication
# backend.)
if not user:
msg = _('Unable to log in with provided credentials.')
raise serializers.ValidationError(msg, code='authorization')
raise AuthenticationFailed()
else:
msg = _('Must include "username" and "password".')
raise serializers.ValidationError(msg, code='authorization')
Expand Down
2 changes: 0 additions & 2 deletions rest_framework/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,6 @@ def handle_exception(self, exc):

if auth_header:
exc.auth_header = auth_header
else:
exc.status_code = status.HTTP_403_FORBIDDEN

exception_handler = self.get_exception_handler()

Expand Down
6 changes: 3 additions & 3 deletions tests/authentication/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def test_post_form_session_auth_failing(self):
Ensure POSTing form over session authentication without logged in user fails.
"""
response = self.csrf_client.post('/session/', {'example': 'example'})
assert response.status_code == status.HTTP_403_FORBIDDEN
assert response.status_code == status.HTTP_401_UNAUTHORIZED


class BaseTokenAuthTests:
Expand Down Expand Up @@ -440,7 +440,7 @@ def test_token_login_json_bad_creds(self):
{'username': self.username, 'password': "badpass"},
format='json'
)
assert response.status_code == 400
assert response.status_code == status.HTTP_401_UNAUTHORIZED

def test_token_login_json_missing_fields(self):
"""Ensure token login view using JSON POST fails if missing fields."""
Expand Down Expand Up @@ -490,7 +490,7 @@ def authenticate(self, request):
permission_classes=()
)
response = view(request)
assert response.status_code == status.HTTP_403_FORBIDDEN
assert response.status_code == status.HTTP_401_UNAUTHORIZED
assert response.data == {'detail': 'Bad credentials'}


Expand Down
5 changes: 3 additions & 2 deletions tests/browsable_api/test_browsable_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib.auth.models import User
from django.test import TestCase, override_settings

from rest_framework import status
from rest_framework.permissions import IsAuthenticated
from rest_framework.test import APIClient

Expand All @@ -21,14 +22,14 @@ def test_get_raises_typeerror_when_anonymous_user_in_queryset_filter(self):
with self.assertRaises(TypeError):
self.client.get('/basicviewset')

def test_get_returns_http_forbidden_when_anonymous_user(self):
def test_get_returns_http_unauthorized_when_anonymous_user(self):
old_permissions = BasicModelWithUsersViewSet.permission_classes
BasicModelWithUsersViewSet.permission_classes = [IsAuthenticated, OrganizationPermissions]

response = self.client.get('/basicviewset')

BasicModelWithUsersViewSet.permission_classes = old_permissions
self.assertEqual(response.status_code, 403)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)


@override_settings(ROOT_URLCONF='tests.browsable_api.auth_urls')
Expand Down
2 changes: 1 addition & 1 deletion tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def view(request):

request = self.factory.get('/')
response = view(request)
assert response.status_code == status.HTTP_403_FORBIDDEN
assert response.status_code == status.HTTP_401_UNAUTHORIZED

def test_throttle_classes(self):
class OncePerDayUserThrottle(UserRateThrottle):
Expand Down
Loading