Skip to content

Handle early Responses #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion django_session_jwt/middleware/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def process_request(self, request):
request.session['jwt'] = fields

def process_response(self, request, response):
if not request.user.is_authenticated:
if not hasattr(request, 'user') or not request.user.is_authenticated:
# The user is unauthenticated. Try to determine the user by the
# session JWT
User = get_user_model()
Expand Down
3 changes: 2 additions & 1 deletion django_session_jwt/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@
)

MIDDLEWARE = (
'django.middleware.common.CommonMiddleware',
'django_session_jwt.middleware.SessionMiddleware',
# MiddlewareFailTestCase relies on this ordering.
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
Expand Down
10 changes: 10 additions & 0 deletions django_session_jwt/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ def test_unauthenicated_view(self):
r.cookies.get(settings.SESSION_COOKIE_NAME).value)
self.assertNotEqual(jwt1['iat'], jwt2['iat'])

def test_middleware_early_return(self):
"""
By passing an incorrect HOST, CommonMiddleware returns early before
AuthenticationMiddleware is reached. We test this to ensure we don't error in
process_response, and end up hiding the real error with a HTTP 500
"""
response = self.client.post('/login/', {'username': 'john', 'password': 'password'},
HTTP_HOST="blahblah.com")
self.assertEqual(response.status_code, 400)


class TestClientTestCase(BaseTestCase):
def test_login(self):
Expand Down