Commit 35022ca9 by Tom Christie

Refactor SessionAuthentication slightly

parent f7db0695
...@@ -26,6 +26,12 @@ def get_authorization_header(request): ...@@ -26,6 +26,12 @@ def get_authorization_header(request):
return auth return auth
class CSRFCheck(CsrfViewMiddleware):
def _reject(self, request, reason):
# Return the failure reason instead of an HttpResponse
return reason
class BaseAuthentication(object): class BaseAuthentication(object):
""" """
All authentication classes should extend BaseAuthentication. All authentication classes should extend BaseAuthentication.
...@@ -110,20 +116,20 @@ class SessionAuthentication(BaseAuthentication): ...@@ -110,20 +116,20 @@ class SessionAuthentication(BaseAuthentication):
if not user or not user.is_active: if not user or not user.is_active:
return None return None
# Enforce CSRF validation for session based authentication. self.enforce_csrf(http_request)
class CSRFCheck(CsrfViewMiddleware):
def _reject(self, request, reason): # CSRF passed with authenticated user
# Return the failure reason instead of an HttpResponse return (user, None)
return reason
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: if reason:
# CSRF failed, bail with explicit error message # CSRF failed, bail with explicit error message
raise exceptions.AuthenticationFailed('CSRF Failed: %s' % reason) raise exceptions.AuthenticationFailed('CSRF Failed: %s' % reason)
# CSRF passed with authenticated user
return (user, None)
class TokenAuthentication(BaseAuthentication): class TokenAuthentication(BaseAuthentication):
""" """
......
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