mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-29 04:54:00 +03:00
decode base64 credentials as utf8; adjust tests (#7193)
* decode base64 credentials as utf8; adjust tests * basicauth: add dedicated test for utf8 credentials * basicauth: add fallback to latin-1 encoding if utf-8 fails
This commit is contained in:
parent
f81ca78642
commit
d7b218f5eb
|
@ -74,7 +74,11 @@ class BasicAuthentication(BaseAuthentication):
|
|||
raise exceptions.AuthenticationFailed(msg)
|
||||
|
||||
try:
|
||||
auth_parts = base64.b64decode(auth[1]).decode(HTTP_HEADER_ENCODING).partition(':')
|
||||
try:
|
||||
auth_decoded = base64.b64decode(auth[1]).decode('utf-8')
|
||||
except UnicodeDecodeError:
|
||||
auth_decoded = base64.b64decode(auth[1]).decode('latin-1')
|
||||
auth_parts = auth_decoded.partition(':')
|
||||
except (TypeError, UnicodeDecodeError, binascii.Error):
|
||||
msg = _('Invalid basic header. Credentials not correctly base64 encoded.')
|
||||
raise exceptions.AuthenticationFailed(msg)
|
||||
|
|
|
@ -159,6 +159,25 @@ class BasicAuthTests(TestCase):
|
|||
)
|
||||
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||
|
||||
def test_decoding_of_utf8_credentials(self):
|
||||
username = 'walterwhité'
|
||||
email = 'walterwhite@example.com'
|
||||
password = 'pässwörd'
|
||||
User.objects.create_user(
|
||||
username, email, password
|
||||
)
|
||||
credentials = ('%s:%s' % (username, password))
|
||||
base64_credentials = base64.b64encode(
|
||||
credentials.encode('utf-8')
|
||||
).decode(HTTP_HEADER_ENCODING)
|
||||
auth = 'Basic %s' % base64_credentials
|
||||
response = self.csrf_client.post(
|
||||
'/basic/',
|
||||
{'example': 'example'},
|
||||
HTTP_AUTHORIZATION=auth
|
||||
)
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
|
||||
@override_settings(ROOT_URLCONF=__name__)
|
||||
class SessionAuthTests(TestCase):
|
||||
|
|
Loading…
Reference in New Issue
Block a user