Skip to content

Commit 29d5030

Browse files
committed
Make compatible with Django 1.10. Replace _get_new_csrf_key with _get_new_csrf_string, use Middleware Deprecation Mixin, fix urlpatterns
1 parent 98626d3 commit 29d5030

File tree

5 files changed

+54
-32
lines changed

5 files changed

+54
-32
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ python:
44
- "2.7"
55

66
env:
7-
- DJANGO="Django==1.7.11"
8-
- DJANGO="Django==1.8.11"
9-
- DJANGO="Django==1.9.4"
7+
- DJANGO="Django==1.8.15"
8+
- DJANGO="Django==1.9.10"
9+
- DJANGO="Django==1.10.1"
1010

1111
install: pip install $DJANGO
1212

runtests.sh

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,29 @@ DATABASES = {
1010
},
1111
}
1212
13-
MIDDLEWARE_CLASSES = (
13+
MIDDLEWARE_CLASSES = MIDDLEWARE = (
1414
'django.middleware.common.CommonMiddleware',
1515
'django.contrib.sessions.middleware.SessionMiddleware',
1616
'django.contrib.auth.middleware.AuthenticationMiddleware',
1717
'session_csrf.CsrfMiddleware',
1818
)
1919
20-
TEMPLATE_CONTEXT_PROCESSORS = (
21-
'django.contrib.auth.context_processors.auth',
22-
'django.core.context_processors.debug',
23-
'django.core.context_processors.i18n',
24-
'django.core.context_processors.media',
25-
'django.core.context_processors.static',
26-
'django.contrib.messages.context_processors.messages',
27-
'session_csrf.context_processor',
28-
)
20+
TEMPLATES = [
21+
{
22+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
23+
'OPTIONS': {
24+
'context_processors': [
25+
'django.contrib.auth.context_processors.auth',
26+
'django.template.context_processors.debug',
27+
'django.template.context_processors.i18n',
28+
'django.template.context_processors.media',
29+
'django.template.context_processors.static',
30+
'django.contrib.messages.context_processors.messages',
31+
'session_csrf.context_processor',
32+
],
33+
},
34+
},
35+
]
2936
3037
ROOT_URLCONF = 'session_csrf.tests'
3138

session_csrf/__init__.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
import hashlib
44

55
from django.conf import settings
6+
from django import VERSION as DJANGO_VERSION
67
from django.core.cache import cache
78
from 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
914
from 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)

session_csrf/tests.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import django.test
44
from django import http
5-
from django.conf.urls import patterns
5+
from django.conf.urls import url
66
from django.contrib.auth import logout
77
from django.contrib.auth.middleware import AuthenticationMiddleware
88
from django.contrib.auth.models import User
@@ -20,12 +20,12 @@
2020
CsrfMiddleware, prep_key)
2121

2222

23-
urlpatterns = patterns('',
24-
('^$', lambda r: http.HttpResponse()),
25-
('^anon$', anonymous_csrf(lambda r: http.HttpResponse())),
26-
('^no-anon-csrf$', anonymous_csrf_exempt(lambda r: http.HttpResponse())),
27-
('^logout$', anonymous_csrf(lambda r: logout(r) or http.HttpResponse())),
28-
)
23+
urlpatterns = [
24+
url('^$', lambda r: http.HttpResponse()),
25+
url('^anon$', anonymous_csrf(lambda r: http.HttpResponse())),
26+
url('^no-anon-csrf$', anonymous_csrf_exempt(lambda r: http.HttpResponse())),
27+
url('^logout$', anonymous_csrf(lambda r: logout(r) or http.HttpResponse())),
28+
]
2929

3030

3131
class TestCsrfToken(django.test.TestCase):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
setup(
88
name='django-session-csrf',
9-
version='0.6',
9+
version='0.7.0',
1010
description='CSRF protection for Django without cookies.',
1111
long_description=open(os.path.join(ROOT, 'README.rst')).read(),
1212
author='Jeff Balogh',

0 commit comments

Comments
 (0)