Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

base.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. import base64
  2. import logging
  3. import string
  4. from datetime import datetime, timedelta
  5. from django.conf import settings
  6. from django.contrib.sessions.exceptions import SuspiciousSession
  7. from django.core.exceptions import SuspiciousOperation
  8. from django.utils import timezone
  9. from django.utils.crypto import (
  10. constant_time_compare, get_random_string, salted_hmac,
  11. )
  12. from django.utils.encoding import force_bytes
  13. from django.utils.module_loading import import_string
  14. # session_key should not be case sensitive because some backends can store it
  15. # on case insensitive file systems.
  16. VALID_KEY_CHARS = string.ascii_lowercase + string.digits
  17. class CreateError(Exception):
  18. """
  19. Used internally as a consistent exception type to catch from save (see the
  20. docstring for SessionBase.save() for details).
  21. """
  22. pass
  23. class UpdateError(Exception):
  24. """
  25. Occurs if Django tries to update a session that was deleted.
  26. """
  27. pass
  28. class SessionBase:
  29. """
  30. Base class for all Session classes.
  31. """
  32. TEST_COOKIE_NAME = 'testcookie'
  33. TEST_COOKIE_VALUE = 'worked'
  34. __not_given = object()
  35. def __init__(self, session_key=None):
  36. self._session_key = session_key
  37. self.accessed = False
  38. self.modified = False
  39. self.serializer = import_string(settings.SESSION_SERIALIZER)
  40. def __contains__(self, key):
  41. return key in self._session
  42. def __getitem__(self, key):
  43. return self._session[key]
  44. def __setitem__(self, key, value):
  45. self._session[key] = value
  46. self.modified = True
  47. def __delitem__(self, key):
  48. del self._session[key]
  49. self.modified = True
  50. def get(self, key, default=None):
  51. return self._session.get(key, default)
  52. def pop(self, key, default=__not_given):
  53. self.modified = self.modified or key in self._session
  54. args = () if default is self.__not_given else (default,)
  55. return self._session.pop(key, *args)
  56. def setdefault(self, key, value):
  57. if key in self._session:
  58. return self._session[key]
  59. else:
  60. self.modified = True
  61. self._session[key] = value
  62. return value
  63. def set_test_cookie(self):
  64. self[self.TEST_COOKIE_NAME] = self.TEST_COOKIE_VALUE
  65. def test_cookie_worked(self):
  66. return self.get(self.TEST_COOKIE_NAME) == self.TEST_COOKIE_VALUE
  67. def delete_test_cookie(self):
  68. del self[self.TEST_COOKIE_NAME]
  69. def _hash(self, value):
  70. key_salt = "django.contrib.sessions" + self.__class__.__name__
  71. return salted_hmac(key_salt, value).hexdigest()
  72. def encode(self, session_dict):
  73. "Return the given session dictionary serialized and encoded as a string."
  74. serialized = self.serializer().dumps(session_dict)
  75. hash = self._hash(serialized)
  76. return base64.b64encode(hash.encode() + b":" + serialized).decode('ascii')
  77. def decode(self, session_data):
  78. encoded_data = base64.b64decode(force_bytes(session_data))
  79. try:
  80. # could produce ValueError if there is no ':'
  81. hash, serialized = encoded_data.split(b':', 1)
  82. expected_hash = self._hash(serialized)
  83. if not constant_time_compare(hash.decode(), expected_hash):
  84. raise SuspiciousSession("Session data corrupted")
  85. else:
  86. return self.serializer().loads(serialized)
  87. except Exception as e:
  88. # ValueError, SuspiciousOperation, unpickling exceptions. If any of
  89. # these happen, just return an empty dictionary (an empty session).
  90. if isinstance(e, SuspiciousOperation):
  91. logger = logging.getLogger('django.security.%s' % e.__class__.__name__)
  92. logger.warning(str(e))
  93. return {}
  94. def update(self, dict_):
  95. self._session.update(dict_)
  96. self.modified = True
  97. def has_key(self, key):
  98. return key in self._session
  99. def keys(self):
  100. return self._session.keys()
  101. def values(self):
  102. return self._session.values()
  103. def items(self):
  104. return self._session.items()
  105. def clear(self):
  106. # To avoid unnecessary persistent storage accesses, we set up the
  107. # internals directly (loading data wastes time, since we are going to
  108. # set it to an empty dict anyway).
  109. self._session_cache = {}
  110. self.accessed = True
  111. self.modified = True
  112. def is_empty(self):
  113. "Return True when there is no session_key and the session is empty."
  114. try:
  115. return not self._session_key and not self._session_cache
  116. except AttributeError:
  117. return True
  118. def _get_new_session_key(self):
  119. "Return session key that isn't being used."
  120. while True:
  121. session_key = get_random_string(32, VALID_KEY_CHARS)
  122. if not self.exists(session_key):
  123. return session_key
  124. def _get_or_create_session_key(self):
  125. if self._session_key is None:
  126. self._session_key = self._get_new_session_key()
  127. return self._session_key
  128. def _validate_session_key(self, key):
  129. """
  130. Key must be truthy and at least 8 characters long. 8 characters is an
  131. arbitrary lower bound for some minimal key security.
  132. """
  133. return key and len(key) >= 8
  134. def _get_session_key(self):
  135. return self.__session_key
  136. def _set_session_key(self, value):
  137. """
  138. Validate session key on assignment. Invalid values will set to None.
  139. """
  140. if self._validate_session_key(value):
  141. self.__session_key = value
  142. else:
  143. self.__session_key = None
  144. session_key = property(_get_session_key)
  145. _session_key = property(_get_session_key, _set_session_key)
  146. def _get_session(self, no_load=False):
  147. """
  148. Lazily load session from storage (unless "no_load" is True, when only
  149. an empty dict is stored) and store it in the current instance.
  150. """
  151. self.accessed = True
  152. try:
  153. return self._session_cache
  154. except AttributeError:
  155. if self.session_key is None or no_load:
  156. self._session_cache = {}
  157. else:
  158. self._session_cache = self.load()
  159. return self._session_cache
  160. _session = property(_get_session)
  161. def get_expiry_age(self, **kwargs):
  162. """Get the number of seconds until the session expires.
  163. Optionally, this function accepts `modification` and `expiry` keyword
  164. arguments specifying the modification and expiry of the session.
  165. """
  166. try:
  167. modification = kwargs['modification']
  168. except KeyError:
  169. modification = timezone.now()
  170. # Make the difference between "expiry=None passed in kwargs" and
  171. # "expiry not passed in kwargs", in order to guarantee not to trigger
  172. # self.load() when expiry is provided.
  173. try:
  174. expiry = kwargs['expiry']
  175. except KeyError:
  176. expiry = self.get('_session_expiry')
  177. if not expiry: # Checks both None and 0 cases
  178. return settings.SESSION_COOKIE_AGE
  179. if not isinstance(expiry, datetime):
  180. return expiry
  181. delta = expiry - modification
  182. return delta.days * 86400 + delta.seconds
  183. def get_expiry_date(self, **kwargs):
  184. """Get session the expiry date (as a datetime object).
  185. Optionally, this function accepts `modification` and `expiry` keyword
  186. arguments specifying the modification and expiry of the session.
  187. """
  188. try:
  189. modification = kwargs['modification']
  190. except KeyError:
  191. modification = timezone.now()
  192. # Same comment as in get_expiry_age
  193. try:
  194. expiry = kwargs['expiry']
  195. except KeyError:
  196. expiry = self.get('_session_expiry')
  197. if isinstance(expiry, datetime):
  198. return expiry
  199. expiry = expiry or settings.SESSION_COOKIE_AGE # Checks both None and 0 cases
  200. return modification + timedelta(seconds=expiry)
  201. def set_expiry(self, value):
  202. """
  203. Set a custom expiration for the session. ``value`` can be an integer,
  204. a Python ``datetime`` or ``timedelta`` object or ``None``.
  205. If ``value`` is an integer, the session will expire after that many
  206. seconds of inactivity. If set to ``0`` then the session will expire on
  207. browser close.
  208. If ``value`` is a ``datetime`` or ``timedelta`` object, the session
  209. will expire at that specific future time.
  210. If ``value`` is ``None``, the session uses the global session expiry
  211. policy.
  212. """
  213. if value is None:
  214. # Remove any custom expiration for this session.
  215. try:
  216. del self['_session_expiry']
  217. except KeyError:
  218. pass
  219. return
  220. if isinstance(value, timedelta):
  221. value = timezone.now() + value
  222. self['_session_expiry'] = value
  223. def get_expire_at_browser_close(self):
  224. """
  225. Return ``True`` if the session is set to expire when the browser
  226. closes, and ``False`` if there's an expiry date. Use
  227. ``get_expiry_date()`` or ``get_expiry_age()`` to find the actual expiry
  228. date/age, if there is one.
  229. """
  230. if self.get('_session_expiry') is None:
  231. return settings.SESSION_EXPIRE_AT_BROWSER_CLOSE
  232. return self.get('_session_expiry') == 0
  233. def flush(self):
  234. """
  235. Remove the current session data from the database and regenerate the
  236. key.
  237. """
  238. self.clear()
  239. self.delete()
  240. self._session_key = None
  241. def cycle_key(self):
  242. """
  243. Create a new session key, while retaining the current session data.
  244. """
  245. data = self._session
  246. key = self.session_key
  247. self.create()
  248. self._session_cache = data
  249. if key:
  250. self.delete(key)
  251. # Methods that child classes must implement.
  252. def exists(self, session_key):
  253. """
  254. Return True if the given session_key already exists.
  255. """
  256. raise NotImplementedError('subclasses of SessionBase must provide an exists() method')
  257. def create(self):
  258. """
  259. Create a new session instance. Guaranteed to create a new object with
  260. a unique key and will have saved the result once (with empty data)
  261. before the method returns.
  262. """
  263. raise NotImplementedError('subclasses of SessionBase must provide a create() method')
  264. def save(self, must_create=False):
  265. """
  266. Save the session data. If 'must_create' is True, create a new session
  267. object (or raise CreateError). Otherwise, only update an existing
  268. object and don't create one (raise UpdateError if needed).
  269. """
  270. raise NotImplementedError('subclasses of SessionBase must provide a save() method')
  271. def delete(self, session_key=None):
  272. """
  273. Delete the session data under this key. If the key is None, use the
  274. current session key value.
  275. """
  276. raise NotImplementedError('subclasses of SessionBase must provide a delete() method')
  277. def load(self):
  278. """
  279. Load the session data and return a dictionary.
  280. """
  281. raise NotImplementedError('subclasses of SessionBase must provide a load() method')
  282. @classmethod
  283. def clear_expired(cls):
  284. """
  285. Remove expired sessions from the session store.
  286. If this operation isn't possible on a given backend, it should raise
  287. NotImplementedError. If it isn't necessary, because the backend has
  288. a built-in expiration mechanism, it should be a no-op.
  289. """
  290. raise NotImplementedError('This backend does not support clear_expired().')