diff options
| author | Philip Douglas | 2013-07-31 07:45:05 -0700 |
|---|---|---|
| committer | Philip Douglas | 2013-07-31 07:45:05 -0700 |
| commit | c058ab36b13a6979c57760d9af2eb21ec3165e7d (patch) | |
| tree | 9c352f0ebaddf18b03e45de9bf4ee3ad53377e17 /rest_framework/authentication.py | |
| parent | bf8e71c455a47a53898f8239ac7dad47e5f1d53a (diff) | |
| parent | 43a5f8183c90f1056bbf33bb1402e76883aeb1fd (diff) | |
| download | django-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.py | 28 |
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): """ |
