aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/authentication.py
diff options
context:
space:
mode:
authorTom Christie2012-10-15 13:27:50 +0100
committerTom Christie2012-10-15 13:27:50 +0100
commit9c1fba3483b7e81da0744464dcf23a5f12711de2 (patch)
treed9370dc9fb9d2fea65192bf5ce4d7fb594d3ad0c /rest_framework/authentication.py
parente88ca9637bd4f49659dd80ca7afd0f38adf07746 (diff)
downloaddjango-rest-framework-9c1fba3483b7e81da0744464dcf23a5f12711de2.tar.bz2
Tweak parsers to take parser_context
Diffstat (limited to 'rest_framework/authentication.py')
-rw-r--r--rest_framework/authentication.py40
1 files changed, 9 insertions, 31 deletions
diff --git a/rest_framework/authentication.py b/rest_framework/authentication.py
index ee5bd2f2..d7624708 100644
--- a/rest_framework/authentication.py
+++ b/rest_framework/authentication.py
@@ -1,10 +1,9 @@
"""
-The :mod:`authentication` module provides a set of pluggable authentication classes.
-
-Authentication behavior is provided by mixing the :class:`mixins.RequestMixin` class into a :class:`View` class.
+Provides a set of pluggable authentication policies.
"""
from django.contrib.auth import authenticate
+from django.utils.encoding import smart_unicode, DjangoUnicodeDecodeError
from rest_framework.compat import CsrfViewMiddleware
from rest_framework.authtoken.models import Token
import base64
@@ -17,25 +16,14 @@ class BaseAuthentication(object):
def authenticate(self, request):
"""
- Authenticate the :obj:`request` and return a :obj:`User` or :const:`None`. [*]_
-
- .. [*] The authentication context *will* typically be a :obj:`User`,
- but it need not be. It can be any user-like object so long as the
- permissions classes (see the :mod:`permissions` module) on the view can
- handle the object and use it to determine if the request has the required
- permissions or not.
-
- This can be an important distinction if you're implementing some token
- based authentication mechanism, where the authentication context
- may be more involved than simply mapping to a :obj:`User`.
+ Authenticate the request and return a two-tuple of (user, token).
"""
- return None
+ raise NotImplementedError(".authenticate() must be overridden.")
class BasicAuthentication(BaseAuthentication):
"""
- Base class for HTTP Basic authentication.
- Subclasses should implement `.authenticate_credentials()`.
+ HTTP Basic authentication against username/password.
"""
def authenticate(self, request):
@@ -43,8 +31,6 @@ class BasicAuthentication(BaseAuthentication):
Returns a `User` if a correct username and password have been supplied
using HTTP Basic authentication. Otherwise returns `None`.
"""
- from django.utils.encoding import smart_unicode, DjangoUnicodeDecodeError
-
if 'HTTP_AUTHORIZATION' in request.META:
auth = request.META['HTTP_AUTHORIZATION'].split()
if len(auth) == 2 and auth[0].lower() == "basic":
@@ -54,7 +40,8 @@ class BasicAuthentication(BaseAuthentication):
return None
try:
- userid, password = smart_unicode(auth_parts[0]), smart_unicode(auth_parts[2])
+ userid = smart_unicode(auth_parts[0])
+ password = smart_unicode(auth_parts[2])
except DjangoUnicodeDecodeError:
return None
@@ -62,15 +49,6 @@ class BasicAuthentication(BaseAuthentication):
def authenticate_credentials(self, userid, password):
"""
- Given the Basic authentication userid and password, authenticate
- and return a user instance.
- """
- raise NotImplementedError('.authenticate_credentials() must be overridden')
-
-
-class UserBasicAuthentication(BasicAuthentication):
- def authenticate_credentials(self, userid, password):
- """
Authenticate the userid and password against username and password.
"""
user = authenticate(username=userid, password=password)
@@ -85,8 +63,8 @@ class SessionAuthentication(BaseAuthentication):
def authenticate(self, request):
"""
- Returns a :obj:`User` if the request session currently has a logged in user.
- Otherwise returns :const:`None`.
+ Returns a `User` if the request session currently has a logged in user.
+ Otherwise returns `None`.
"""
# Get the underlying HttpRequest object