signed_cookies.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from django.contrib.sessions.backends.base import SessionBase
  2. from django.core import signing
  3. class SessionStore(SessionBase):
  4. def load(self):
  5. """
  6. Load the data from the key itself instead of fetching from some
  7. external data store. Opposite of _get_session_key(), raise BadSignature
  8. if signature fails.
  9. """
  10. try:
  11. return signing.loads(
  12. self.session_key,
  13. serializer=self.serializer,
  14. # This doesn't handle non-default expiry dates, see #19201
  15. max_age=self.get_session_cookie_age(),
  16. salt="django.contrib.sessions.backends.signed_cookies",
  17. )
  18. except Exception:
  19. # BadSignature, ValueError, or unpickling exceptions. If any of
  20. # these happen, reset the session.
  21. self.create()
  22. return {}
  23. async def aload(self):
  24. return self.load()
  25. def create(self):
  26. """
  27. To create a new key, set the modified flag so that the cookie is set
  28. on the client for the current request.
  29. """
  30. self.modified = True
  31. async def acreate(self):
  32. return self.create()
  33. def save(self, must_create=False):
  34. """
  35. To save, get the session key as a securely signed string and then set
  36. the modified flag so that the cookie is set on the client for the
  37. current request.
  38. """
  39. self._session_key = self._get_session_key()
  40. self.modified = True
  41. async def asave(self, must_create=False):
  42. return self.save(must_create=must_create)
  43. def exists(self, session_key=None):
  44. """
  45. This method makes sense when you're talking to a shared resource, but
  46. it doesn't matter when you're storing the information in the client's
  47. cookie.
  48. """
  49. return False
  50. async def aexists(self, session_key=None):
  51. return self.exists(session_key=session_key)
  52. def delete(self, session_key=None):
  53. """
  54. To delete, clear the session key and the underlying data structure
  55. and set the modified flag so that the cookie is set on the client for
  56. the current request.
  57. """
  58. self._session_key = ""
  59. self._session_cache = {}
  60. self.modified = True
  61. async def adelete(self, session_key=None):
  62. return self.delete(session_key=session_key)
  63. def cycle_key(self):
  64. """
  65. Keep the same data but with a new key. Call save() and it will
  66. automatically save a cookie with a new key at the end of the request.
  67. """
  68. self.save()
  69. async def acycle_key(self):
  70. return self.cycle_key()
  71. def _get_session_key(self):
  72. """
  73. Instead of generating a random string, generate a secure url-safe
  74. base64-encoded string of data as our session key.
  75. """
  76. return signing.dumps(
  77. self._session,
  78. compress=True,
  79. salt="django.contrib.sessions.backends.signed_cookies",
  80. serializer=self.serializer,
  81. )
  82. @classmethod
  83. def clear_expired(cls):
  84. pass
  85. @classmethod
  86. async def aclear_expired(cls):
  87. pass