2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.sessions.models import Session
|
2007-01-25 08:47:44 +08:00
|
|
|
from django.core.exceptions import SuspiciousOperation
|
2005-10-09 08:55:08 +08:00
|
|
|
from django.utils.cache import patch_vary_headers
|
2005-08-17 06:54:05 +08:00
|
|
|
import datetime
|
|
|
|
|
|
|
|
TEST_COOKIE_NAME = 'testcookie'
|
|
|
|
TEST_COOKIE_VALUE = 'worked'
|
|
|
|
|
|
|
|
class SessionWrapper(object):
|
|
|
|
def __init__(self, session_key):
|
|
|
|
self.session_key = session_key
|
2007-03-08 16:46:59 +08:00
|
|
|
self.accessed = False
|
2005-08-17 06:54:05 +08:00
|
|
|
self.modified = False
|
|
|
|
|
2005-12-30 03:17:32 +08:00
|
|
|
def __contains__(self, key):
|
|
|
|
return key in self._session
|
|
|
|
|
2005-08-17 06:54:05 +08:00
|
|
|
def __getitem__(self, key):
|
|
|
|
return self._session[key]
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
self._session[key] = value
|
|
|
|
self.modified = True
|
|
|
|
|
|
|
|
def __delitem__(self, key):
|
|
|
|
del self._session[key]
|
|
|
|
self.modified = True
|
|
|
|
|
2006-02-11 05:33:07 +08:00
|
|
|
def keys(self):
|
|
|
|
return self._session.keys()
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
return self._session.items()
|
|
|
|
|
2005-08-17 06:54:05 +08:00
|
|
|
def get(self, key, default=None):
|
|
|
|
return self._session.get(key, default)
|
|
|
|
|
|
|
|
def set_test_cookie(self):
|
|
|
|
self[TEST_COOKIE_NAME] = TEST_COOKIE_VALUE
|
|
|
|
|
|
|
|
def test_cookie_worked(self):
|
|
|
|
return self.get(TEST_COOKIE_NAME) == TEST_COOKIE_VALUE
|
|
|
|
|
2005-09-23 09:28:44 +08:00
|
|
|
def delete_test_cookie(self):
|
|
|
|
del self[TEST_COOKIE_NAME]
|
|
|
|
|
2005-08-17 06:54:05 +08:00
|
|
|
def _get_session(self):
|
|
|
|
# Lazily loads session from storage.
|
2007-03-08 16:46:59 +08:00
|
|
|
self.accessed = True
|
2005-08-17 06:54:05 +08:00
|
|
|
try:
|
|
|
|
return self._session_cache
|
|
|
|
except AttributeError:
|
|
|
|
if self.session_key is None:
|
|
|
|
self._session_cache = {}
|
|
|
|
else:
|
|
|
|
try:
|
2006-05-02 09:31:56 +08:00
|
|
|
s = Session.objects.get(session_key=self.session_key,
|
2005-08-17 06:54:05 +08:00
|
|
|
expire_date__gt=datetime.datetime.now())
|
|
|
|
self._session_cache = s.get_decoded()
|
2007-01-25 08:47:44 +08:00
|
|
|
except (Session.DoesNotExist, SuspiciousOperation):
|
2005-08-17 06:54:05 +08:00
|
|
|
self._session_cache = {}
|
2005-08-19 00:45:15 +08:00
|
|
|
# Set the session_key to None to force creation of a new
|
|
|
|
# key, for extra security.
|
|
|
|
self.session_key = None
|
2005-08-17 06:54:05 +08:00
|
|
|
return self._session_cache
|
|
|
|
|
|
|
|
_session = property(_get_session)
|
|
|
|
|
2006-06-08 13:00:13 +08:00
|
|
|
class SessionMiddleware(object):
|
2005-08-23 03:19:54 +08:00
|
|
|
def process_request(self, request):
|
2006-05-02 09:31:56 +08:00
|
|
|
request.session = SessionWrapper(request.COOKIES.get(settings.SESSION_COOKIE_NAME, None))
|
2005-08-17 06:54:05 +08:00
|
|
|
|
|
|
|
def process_response(self, request, response):
|
|
|
|
# If request.session was modified, or if response.session was set, save
|
|
|
|
# those changes and set a session cookie.
|
|
|
|
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
|
|
|
|
except AttributeError:
|
2006-01-15 14:18:03 +08:00
|
|
|
pass
|
|
|
|
else:
|
2007-03-08 16:46:59 +08:00
|
|
|
if accessed:
|
|
|
|
patch_vary_headers(response, ('Cookie',))
|
2006-05-02 09:31:56 +08:00
|
|
|
if modified or settings.SESSION_SAVE_EVERY_REQUEST:
|
2007-03-22 10:20:33 +08:00
|
|
|
if request.session.session_key:
|
|
|
|
session_key = request.session.session_key
|
|
|
|
else:
|
|
|
|
obj = Session.objects.get_new_session_object()
|
|
|
|
session_key = obj.session_key
|
|
|
|
|
2006-06-02 06:25:06 +08:00
|
|
|
if settings.SESSION_EXPIRE_AT_BROWSER_CLOSE:
|
|
|
|
max_age = None
|
|
|
|
expires = None
|
|
|
|
else:
|
|
|
|
max_age = settings.SESSION_COOKIE_AGE
|
|
|
|
expires = datetime.datetime.strftime(datetime.datetime.utcnow() + datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE), "%a, %d-%b-%Y %H:%M:%S GMT")
|
2006-05-02 09:31:56 +08:00
|
|
|
new_session = Session.objects.save(session_key, request.session._session,
|
|
|
|
datetime.datetime.now() + datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE))
|
|
|
|
response.set_cookie(settings.SESSION_COOKIE_NAME, session_key,
|
2006-08-12 14:02:28 +08:00
|
|
|
max_age=max_age, expires=expires, domain=settings.SESSION_COOKIE_DOMAIN,
|
|
|
|
secure=settings.SESSION_COOKIE_SECURE or None)
|
2005-08-17 06:54:05 +08:00
|
|
|
return response
|