2007-10-30 07:55:08 +08:00
|
|
|
import time
|
2015-01-28 20:35:27 +08:00
|
|
|
from importlib import import_module
|
2007-10-30 07:55:08 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import settings
|
2016-01-09 00:08:08 +08:00
|
|
|
from django.contrib.sessions.backends.base import UpdateError
|
2016-10-25 19:23:14 +08:00
|
|
|
from django.core.exceptions import SuspiciousOperation
|
2005-10-09 08:55:08 +08:00
|
|
|
from django.utils.cache import patch_vary_headers
|
2015-11-07 23:12:37 +08:00
|
|
|
from django.utils.deprecation import MiddlewareMixin
|
2017-12-28 05:49:46 +08:00
|
|
|
from django.utils.http import http_date
|
2005-08-17 06:54:05 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2015-11-07 23:12:37 +08:00
|
|
|
class SessionMiddleware(MiddlewareMixin):
|
|
|
|
def __init__(self, get_response=None):
|
|
|
|
self.get_response = get_response
|
2009-03-19 00:55:59 +08:00
|
|
|
engine = import_module(settings.SESSION_ENGINE)
|
2013-06-30 14:26:10 +08:00
|
|
|
self.SessionStore = engine.SessionStore
|
|
|
|
|
|
|
|
def process_request(self, request):
|
2015-05-14 02:51:18 +08:00
|
|
|
session_key = request.COOKIES.get(settings.SESSION_COOKIE_NAME)
|
2013-06-30 14:26:10 +08:00
|
|
|
request.session = self.SessionStore(session_key)
|
2005-08-17 06:54:05 +08:00
|
|
|
|
|
|
|
def process_response(self, request, response):
|
2008-09-17 16:10:55 +08:00
|
|
|
"""
|
|
|
|
If request.session was modified, or if the configuration is to save the
|
2013-08-19 13:36:36 +08:00
|
|
|
session every time, save the changes and set a session cookie or delete
|
|
|
|
the session cookie if the session has been emptied.
|
2008-09-17 16:10:55 +08:00
|
|
|
"""
|
2005-08-17 06:54:05 +08:00
|
|
|
try:
|
2007-03-08 16:46:59 +08:00
|
|
|
accessed = request.session.accessed
|
2005-08-17 06:54:05 +08:00
|
|
|
modified = request.session.modified
|
2013-08-19 13:36:36 +08:00
|
|
|
empty = request.session.is_empty()
|
2005-08-17 06:54:05 +08:00
|
|
|
except AttributeError:
|
2019-05-29 20:04:42 +08:00
|
|
|
return response
|
|
|
|
# First check if we need to delete this cookie.
|
|
|
|
# The session should be deleted only if the session is entirely empty.
|
|
|
|
if settings.SESSION_COOKIE_NAME in request.COOKIES and empty:
|
|
|
|
response.delete_cookie(
|
|
|
|
settings.SESSION_COOKIE_NAME,
|
|
|
|
path=settings.SESSION_COOKIE_PATH,
|
|
|
|
domain=settings.SESSION_COOKIE_DOMAIN,
|
|
|
|
)
|
|
|
|
patch_vary_headers(response, ('Cookie',))
|
2006-01-15 14:18:03 +08:00
|
|
|
else:
|
2019-05-29 20:04:42 +08:00
|
|
|
if accessed:
|
2019-03-18 23:15:06 +08:00
|
|
|
patch_vary_headers(response, ('Cookie',))
|
2019-05-29 20:04:42 +08:00
|
|
|
if (modified or settings.SESSION_SAVE_EVERY_REQUEST) and not empty:
|
|
|
|
if request.session.get_expire_at_browser_close():
|
|
|
|
max_age = None
|
|
|
|
expires = None
|
|
|
|
else:
|
|
|
|
max_age = request.session.get_expiry_age()
|
|
|
|
expires_time = time.time() + max_age
|
|
|
|
expires = http_date(expires_time)
|
|
|
|
# Save the session data and refresh the client cookie.
|
|
|
|
# Skip session save for 500 responses, refs #3881.
|
|
|
|
if response.status_code != 500:
|
|
|
|
try:
|
|
|
|
request.session.save()
|
|
|
|
except UpdateError:
|
|
|
|
raise SuspiciousOperation(
|
|
|
|
"The request's session was deleted before the "
|
|
|
|
"request completed. The user may have logged "
|
|
|
|
"out in a concurrent request, for example."
|
2016-03-29 06:33:29 +08:00
|
|
|
)
|
2019-05-29 20:04:42 +08:00
|
|
|
response.set_cookie(
|
|
|
|
settings.SESSION_COOKIE_NAME,
|
|
|
|
request.session.session_key, max_age=max_age,
|
|
|
|
expires=expires, domain=settings.SESSION_COOKIE_DOMAIN,
|
|
|
|
path=settings.SESSION_COOKIE_PATH,
|
|
|
|
secure=settings.SESSION_COOKIE_SECURE or None,
|
|
|
|
httponly=settings.SESSION_COOKIE_HTTPONLY or None,
|
|
|
|
samesite=settings.SESSION_COOKIE_SAMESITE,
|
|
|
|
)
|
2005-08-17 06:54:05 +08:00
|
|
|
return response
|