2007-06-15 08:22:16 +08:00
|
|
|
import base64, md5, random, sys, datetime, os, time
|
2006-05-02 09:31:56 +08:00
|
|
|
import cPickle as pickle
|
|
|
|
from django.db import models
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
class SessionManager(models.Manager):
|
|
|
|
def encode(self, session_dict):
|
|
|
|
"Returns the given session dictionary pickled and encoded as a string."
|
|
|
|
pickled = pickle.dumps(session_dict)
|
|
|
|
pickled_md5 = md5.new(pickled + settings.SECRET_KEY).hexdigest()
|
|
|
|
return base64.encodestring(pickled + pickled_md5)
|
|
|
|
|
|
|
|
def get_new_session_key(self):
|
|
|
|
"Returns session key that isn't being used."
|
|
|
|
# The random module is seeded when this Apache child is created.
|
2007-06-15 08:22:16 +08:00
|
|
|
# Use SECRET_KEY as added salt.
|
2007-09-15 17:51:41 +08:00
|
|
|
try:
|
|
|
|
pid = os.getpid()
|
|
|
|
except AttributeError:
|
|
|
|
# No getpid() in Jython, for example
|
|
|
|
pid = 1
|
2006-05-02 09:31:56 +08:00
|
|
|
while 1:
|
2007-09-15 17:51:41 +08:00
|
|
|
session_key = md5.new("%s%s%s%s" % (random.randint(0, sys.maxint - 1), pid, time.time(), settings.SECRET_KEY)).hexdigest()
|
2006-05-02 09:31:56 +08:00
|
|
|
try:
|
|
|
|
self.get(session_key=session_key)
|
|
|
|
except self.model.DoesNotExist:
|
|
|
|
break
|
|
|
|
return session_key
|
|
|
|
|
2007-03-22 10:20:33 +08:00
|
|
|
def get_new_session_object(self):
|
|
|
|
"""
|
|
|
|
Returns a new session object.
|
|
|
|
"""
|
|
|
|
# FIXME: There is a *small* chance of collision here, meaning we will
|
|
|
|
# return an existing object. That can be fixed when we add a way to
|
|
|
|
# validate (and guarantee) that non-auto primary keys are unique. For
|
|
|
|
# now, we save immediately in order to reduce the "window of
|
|
|
|
# misfortune" as much as possible.
|
|
|
|
created = False
|
|
|
|
while not created:
|
|
|
|
obj, created = self.get_or_create(session_key=self.get_new_session_key(),
|
|
|
|
expire_date = datetime.datetime.now())
|
|
|
|
# Collision in key generation, so re-seed the generator
|
|
|
|
random.seed()
|
|
|
|
return obj
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def save(self, session_key, session_dict, expire_date):
|
|
|
|
s = self.model(session_key, self.encode(session_dict), expire_date)
|
|
|
|
if session_dict:
|
|
|
|
s.save()
|
|
|
|
else:
|
|
|
|
s.delete() # Clear sessions with no data.
|
|
|
|
return s
|
|
|
|
|
|
|
|
class Session(models.Model):
|
2006-07-20 11:32:24 +08:00
|
|
|
"""
|
|
|
|
Django provides full support for anonymous sessions. The session
|
|
|
|
framework lets you store and retrieve arbitrary data on a
|
|
|
|
per-site-visitor basis. It stores data on the server side and
|
|
|
|
abstracts the sending and receiving of cookies. Cookies contain a
|
|
|
|
session ID -- not the data itself.
|
2006-06-20 12:07:32 +08:00
|
|
|
|
2006-07-20 11:32:24 +08:00
|
|
|
The Django sessions framework is entirely cookie-based. It does
|
|
|
|
not fall back to putting session IDs in URLs. This is an intentional
|
|
|
|
design decision. Not only does that behavior make URLs ugly, it makes
|
|
|
|
your site vulnerable to session-ID theft via the "Referer" header.
|
2006-06-20 12:07:32 +08:00
|
|
|
|
2006-07-20 11:32:24 +08:00
|
|
|
For complete documentation on using Sessions in your code, consult
|
|
|
|
the sessions documentation that is shipped with Django (also available
|
|
|
|
on the Django website).
|
2006-06-20 12:07:32 +08:00
|
|
|
"""
|
2007-08-05 13:14:46 +08:00
|
|
|
session_key = models.CharField(_('session key'), max_length=40, primary_key=True)
|
2006-05-02 09:31:56 +08:00
|
|
|
session_data = models.TextField(_('session data'))
|
|
|
|
expire_date = models.DateTimeField(_('expire date'))
|
|
|
|
objects = SessionManager()
|
|
|
|
class Meta:
|
|
|
|
db_table = 'django_session'
|
|
|
|
verbose_name = _('session')
|
|
|
|
verbose_name_plural = _('sessions')
|
|
|
|
|
|
|
|
def get_decoded(self):
|
|
|
|
encoded_data = base64.decodestring(self.session_data)
|
|
|
|
pickled, tamper_check = encoded_data[:-32], encoded_data[-32:]
|
|
|
|
if md5.new(pickled + settings.SECRET_KEY).hexdigest() != tamper_check:
|
|
|
|
from django.core.exceptions import SuspiciousOperation
|
|
|
|
raise SuspiciousOperation, "User tampered with session cookie."
|
|
|
|
try:
|
|
|
|
return pickle.loads(pickled)
|
|
|
|
# Unpickling can cause a variety of exceptions. If something happens,
|
|
|
|
# just return an empty dictionary (an empty session).
|
|
|
|
except:
|
|
|
|
return {}
|