diff options
| author | Tom Christie | 2013-02-06 00:53:41 -0800 |
|---|---|---|
| committer | Tom Christie | 2013-02-06 00:53:41 -0800 |
| commit | 72fe686623f63b79fa2b7f7de177c6d98bc1da3f (patch) | |
| tree | 5d0834d4eb2caf21ed54a4dcba9a4a3a40e6fa99 /rest_framework/authentication.py | |
| parent | 221e77d3575c182eb49d50546f844f392a5f7ba6 (diff) | |
| parent | bdc97c561147130b59c8cd8cc6bc735eab8b223d (diff) | |
| download | django-rest-framework-72fe686623f63b79fa2b7f7de177c6d98bc1da3f.tar.bz2 | |
Merge pull request #636 from tomchristie/2.2
2.2 Release
Diffstat (limited to 'rest_framework/authentication.py')
| -rw-r--r-- | rest_framework/authentication.py | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/rest_framework/authentication.py b/rest_framework/authentication.py index fc169189..14b2136b 100644 --- a/rest_framework/authentication.py +++ b/rest_framework/authentication.py @@ -1,10 +1,10 @@ """ Provides a set of pluggable authentication policies. """ - +from __future__ import unicode_literals from django.contrib.auth import authenticate -from django.utils.encoding import smart_unicode, DjangoUnicodeDecodeError -from rest_framework import exceptions +from django.utils.encoding import DjangoUnicodeDecodeError +from rest_framework import exceptions, HTTP_HEADER_ENCODING from rest_framework.compat import CsrfViewMiddleware from rest_framework.authtoken.models import Token import base64 @@ -41,22 +41,25 @@ class BasicAuthentication(BaseAuthentication): Returns a `User` if a correct username and password have been supplied using HTTP Basic authentication. Otherwise returns `None`. """ - auth = request.META.get('HTTP_AUTHORIZATION', '').split() + auth = request.META.get('HTTP_AUTHORIZATION', b'') + if type(auth) == type(''): + # Work around django test client oddness + auth = auth.encode(HTTP_HEADER_ENCODING) + auth = auth.split() - if not auth or auth[0].lower() != "basic": + if not auth or auth[0].lower() != b'basic': return None if len(auth) != 2: raise exceptions.AuthenticationFailed('Invalid basic header') try: - auth_parts = base64.b64decode(auth[1]).partition(':') - except TypeError: + auth_parts = base64.b64decode(auth[1]).decode(HTTP_HEADER_ENCODING).partition(':') + except (TypeError, UnicodeDecodeError): raise exceptions.AuthenticationFailed('Invalid basic header') try: - userid = smart_unicode(auth_parts[0]) - password = smart_unicode(auth_parts[2]) + userid, password = auth_parts[0], auth_parts[2] except DjangoUnicodeDecodeError: raise exceptions.AuthenticationFailed('Invalid basic header') |
