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