aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/authentication.py
diff options
context:
space:
mode:
authorPhilip Douglas2013-07-31 07:45:05 -0700
committerPhilip Douglas2013-07-31 07:45:05 -0700
commitc058ab36b13a6979c57760d9af2eb21ec3165e7d (patch)
tree9c352f0ebaddf18b03e45de9bf4ee3ad53377e17 /rest_framework/authentication.py
parentbf8e71c455a47a53898f8239ac7dad47e5f1d53a (diff)
parent43a5f8183c90f1056bbf33bb1402e76883aeb1fd (diff)
downloaddjango-rest-framework-c058ab36b13a6979c57760d9af2eb21ec3165e7d.tar.bz2
Merge pull request #2 from tomchristie/master
Update to latest
Diffstat (limited to 'rest_framework/authentication.py')
-rw-r--r--rest_framework/authentication.py28
1 files changed, 17 insertions, 11 deletions
diff --git a/rest_framework/authentication.py b/rest_framework/authentication.py
index 10298027..cf001a24 100644
--- a/rest_framework/authentication.py
+++ b/rest_framework/authentication.py
@@ -26,6 +26,12 @@ def get_authorization_header(request):
return auth
+class CSRFCheck(CsrfViewMiddleware):
+ def _reject(self, request, reason):
+ # Return the failure reason instead of an HttpResponse
+ return reason
+
+
class BaseAuthentication(object):
"""
All authentication classes should extend BaseAuthentication.
@@ -103,27 +109,27 @@ class SessionAuthentication(BaseAuthentication):
"""
# Get the underlying HttpRequest object
- http_request = request._request
- user = getattr(http_request, 'user', None)
+ request = request._request
+ user = getattr(request, 'user', None)
# Unauthenticated, CSRF validation not required
if not user or not user.is_active:
return None
- # Enforce CSRF validation for session based authentication.
- class CSRFCheck(CsrfViewMiddleware):
- def _reject(self, request, reason):
- # Return the failure reason instead of an HttpResponse
- return reason
+ self.enforce_csrf(request)
+
+ # CSRF passed with authenticated user
+ return (user, None)
- reason = CSRFCheck().process_view(http_request, None, (), {})
+ def enforce_csrf(self, request):
+ """
+ Enforce CSRF validation for session based authentication.
+ """
+ reason = CSRFCheck().process_view(request, None, (), {})
if reason:
# CSRF failed, bail with explicit error message
raise exceptions.AuthenticationFailed('CSRF Failed: %s' % reason)
- # CSRF passed with authenticated user
- return (user, None)
-
class TokenAuthentication(BaseAuthentication):
"""