33import hashlib
44
55from django .conf import settings
6+ from django import VERSION as DJANGO_VERSION
67from django .core .cache import cache
78from django .middleware import csrf as django_csrf
8- from django .utils import crypto
9+ try :
10+ from django .middleware .csrf import _get_new_csrf_key as django_get_new_csrf_string
11+ except ImportError :
12+ from django .middleware .csrf import _get_new_csrf_string as django_get_new_csrf_string
13+ from django .utils import crypto , deprecation
914from django .utils .cache import patch_vary_headers
1015
1116
@@ -32,7 +37,17 @@ def prep_key(key):
3237 prefixed = PREFIX + key
3338 return hashlib .md5 (prefixed ).hexdigest ()
3439
35- class CsrfMiddleware (object ):
40+
41+ def is_user_authenticated (request ):
42+ if DJANGO_VERSION < (1 , 10 , 0 ):
43+ return request .user .is_authenticated ()
44+ else :
45+ return request .user .is_authenticated
46+
47+ # Inherit from deprecation.MiddlewareMixin to ensure it works
48+ # with the new style middleware in Django 1.10 - see
49+ # https://docs.djangoproject.com/en/1.10/topics/http/middleware/#django.utils.deprecation.MiddlewareMixin
50+ class CsrfMiddleware (deprecation .MiddlewareMixin if DJANGO_VERSION >= (1 , 10 , 0 ) else object ):
3651
3752 # csrf_processing_done prevents checking CSRF more than once. That could
3853 # happen if the requires_csrf_token decorator is used.
@@ -50,9 +65,9 @@ def process_request(self, request):
5065 """
5166 if hasattr (request , 'csrf_token' ):
5267 return
53- if request . user . is_authenticated ( ):
68+ if is_user_authenticated ( request ):
5469 if 'csrf_token' not in request .session :
55- token = django_csrf . _get_new_csrf_key ()
70+ token = django_get_new_csrf_string ()
5671 request .csrf_token = request .session ['csrf_token' ] = token
5772 else :
5873 request .csrf_token = request .session ['csrf_token' ]
@@ -64,9 +79,9 @@ def process_request(self, request):
6479 token = cache .get (prep_key (key ), '' )
6580 if ANON_ALWAYS :
6681 if not key :
67- key = django_csrf . _get_new_csrf_key ()
82+ key = django_get_new_csrf_string ()
6883 if not token :
69- token = django_csrf . _get_new_csrf_key ()
84+ token = django_get_new_csrf_string ()
7085 request ._anon_csrf_key = key
7186 cache .set (prep_key (key ), token , ANON_TIMEOUT )
7287 request .csrf_token = token
@@ -81,7 +96,7 @@ def process_view(self, request, view_func, args, kwargs):
8196 return
8297
8398 if (getattr (view_func , 'anonymous_csrf_exempt' , False )
84- and not request . user . is_authenticated ( )):
99+ and not is_user_authenticated ( request )):
85100 return
86101
87102 # Bail if this is a safe method.
@@ -125,14 +140,14 @@ def anonymous_csrf(f):
125140 """Decorator that assigns a CSRF token to an anonymous user."""
126141 @functools .wraps (f )
127142 def wrapper (request , * args , ** kw ):
128- use_anon_cookie = not (request . user . is_authenticated ( ) or ANON_ALWAYS )
143+ use_anon_cookie = not (is_user_authenticated ( request ) or ANON_ALWAYS )
129144 if use_anon_cookie :
130145 if ANON_COOKIE in request .COOKIES :
131146 key = request .COOKIES [ANON_COOKIE ]
132- token = cache .get (prep_key (key )) or django_csrf . _get_new_csrf_key ()
147+ token = cache .get (prep_key (key )) or django_get_new_csrf_string ()
133148 else :
134- key = django_csrf . _get_new_csrf_key ()
135- token = django_csrf . _get_new_csrf_key ()
149+ key = django_get_new_csrf_string ()
150+ token = django_get_new_csrf_string ()
136151 cache .set (prep_key (key ), token , ANON_TIMEOUT )
137152 request .csrf_token = token
138153 response = f (request , * args , ** kw )
0 commit comments