mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-08 06:14:47 +03:00
return AnonymousUser in SessionAuthentication if csrf check failed
This commit is contained in:
parent
86470b7813
commit
9c412426b5
|
@ -6,6 +6,7 @@ from __future__ import unicode_literals
|
||||||
import base64
|
import base64
|
||||||
|
|
||||||
from django.contrib.auth import authenticate, get_user_model
|
from django.contrib.auth import authenticate, get_user_model
|
||||||
|
from django.contrib.auth.models import AnonymousUser
|
||||||
from django.middleware.csrf import CsrfViewMiddleware
|
from django.middleware.csrf import CsrfViewMiddleware
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
|
@ -125,19 +126,21 @@ class SessionAuthentication(BaseAuthentication):
|
||||||
if not user or not user.is_active:
|
if not user or not user.is_active:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
self.enforce_csrf(request)
|
if self.check_csrf(request):
|
||||||
|
# CSRF passed with authenticated user
|
||||||
|
return (user, None)
|
||||||
|
else:
|
||||||
|
return (AnonymousUser(), None)
|
||||||
|
|
||||||
# CSRF passed with authenticated user
|
def check_csrf(self, request):
|
||||||
return (user, None)
|
|
||||||
|
|
||||||
def enforce_csrf(self, request):
|
|
||||||
"""
|
"""
|
||||||
Enforce CSRF validation for session based authentication.
|
return True if csrf is correct.
|
||||||
"""
|
"""
|
||||||
reason = CSRFCheck().process_view(request, None, (), {})
|
reason = CSRFCheck().process_view(request, None, (), {})
|
||||||
if reason:
|
if reason:
|
||||||
# CSRF failed, bail with explicit error message
|
request._csrf_failed_reason = reason
|
||||||
raise exceptions.PermissionDenied('CSRF Failed: %s' % reason)
|
|
||||||
|
return not reason
|
||||||
|
|
||||||
|
|
||||||
class TokenAuthentication(BaseAuthentication):
|
class TokenAuthentication(BaseAuthentication):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user