Commit bbab859a by Carmen Wick

Better error handling for Basic authentication. Catch exceptions that could be…

Better error handling for Basic authentication. Catch exceptions that could be thrown due to malformed input
parent 39c0c077
...@@ -58,10 +58,21 @@ class BaseAuthenticator(object): ...@@ -58,10 +58,21 @@ class BaseAuthenticator(object):
class BasicAuthenticator(BaseAuthenticator): class BasicAuthenticator(BaseAuthenticator):
"""Use HTTP Basic authentication""" """Use HTTP Basic authentication"""
def authenticate(self, request): def authenticate(self, request):
from django.utils.encoding import smart_unicode, DjangoUnicodeDecodeError
if 'HTTP_AUTHORIZATION' in request.META: if 'HTTP_AUTHORIZATION' in request.META:
auth = request.META['HTTP_AUTHORIZATION'].split() auth = request.META['HTTP_AUTHORIZATION'].split()
if len(auth) == 2 and auth[0].lower() == "basic": if len(auth) == 2 and auth[0].lower() == "basic":
uname, passwd = base64.b64decode(auth[1]).split(':') try:
auth_parts = base64.b64decode(auth[1]).partition(':')
except TypeError:
return None
try:
uname, passwd = smart_unicode(auth_parts[0]), smart_unicode(auth_parts[2])
except DjangoUnicodeDecodeError:
return None
user = authenticate(username=uname, password=passwd) user = authenticate(username=uname, password=passwd)
if user is not None and user.is_active: if user is not None and user.is_active:
return user return user
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment