diff options
| author | Tom Christie | 2013-06-29 08:14:05 +0100 |
|---|---|---|
| committer | Tom Christie | 2013-06-29 08:14:05 +0100 |
| commit | 35022ca9213939a2f40c82facffa908a818efe0b (patch) | |
| tree | d9f26213b9c385e4e6f9cb2c621e7342d5f862ec /rest_framework/authentication.py | |
| parent | f7db06953bd8ad7f5e0211f49a04e8d5bb634380 (diff) | |
| download | django-rest-framework-35022ca9213939a2f40c82facffa908a818efe0b.tar.bz2 | |
Refactor SessionAuthentication slightly
Diffstat (limited to 'rest_framework/authentication.py')
| -rw-r--r-- | rest_framework/authentication.py | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/rest_framework/authentication.py b/rest_framework/authentication.py index 10298027..b42162dd 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. @@ -110,20 +116,20 @@ class SessionAuthentication(BaseAuthentication): 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(http_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): """ |
