| 1 |
import warnings |
| 2 |
from datetime import timedelta as TimeDelta |
| 3 |
from typing import Optional |
| 4 |
|
| 5 |
from cachelib.file import FileSystemCache |
| 6 |
from flask import Flask |
| 7 |
|
| 8 |
from .._utils import total_seconds |
| 9 |
from ..base import ServerSideSession, ServerSideSessionInterface |
| 10 |
from ..defaults import Defaults |
| 11 |
|
| 12 |
|
| 13 |
class FileSystemSession(ServerSideSession): |
| 14 |
pass |
| 15 |
|
| 16 |
|
| 17 |
class FileSystemSessionInterface(ServerSideSessionInterface): |
| 18 |
"""Uses the :class:`cachelib.file.FileSystemCache` as a session storage. |
| 19 |
|
| 20 |
:param key_prefix: A prefix that is added to storage keys. |
| 21 |
:param use_signer: Whether to sign the session id cookie or not. |
| 22 |
:param permanent: Whether to use permanent session or not. |
| 23 |
:param sid_length: The length of the generated session id in bytes. |
| 24 |
:param serialization_format: The serialization format to use for the session data. |
| 25 |
:param cache_dir: the directory where session files are stored. |
| 26 |
:param threshold: the maximum number of items the session stores before it |
| 27 |
:param mode: the file mode wanted for the session files, default 0600 |
| 28 |
|
| 29 |
.. versionadded:: 0.7 |
| 30 |
The `serialization_format` and `app` parameters were added. |
| 31 |
|
| 32 |
.. versionadded:: 0.6 |
| 33 |
The `sid_length` parameter was added. |
| 34 |
|
| 35 |
.. versionadded:: 0.2 |
| 36 |
The `use_signer` parameter was added. |
| 37 |
""" |
| 38 |
|
| 39 |
session_class = FileSystemSession |
| 40 |
ttl = True |
| 41 |
|
| 42 |
def __init__( |
| 43 |
self, |
| 44 |
app: Flask, |
| 45 |
key_prefix: str = Defaults.SESSION_KEY_PREFIX, |
| 46 |
use_signer: bool = Defaults.SESSION_USE_SIGNER, |
| 47 |
permanent: bool = Defaults.SESSION_PERMANENT, |
| 48 |
sid_length: int = Defaults.SESSION_ID_LENGTH, |
| 49 |
serialization_format: str = Defaults.SESSION_SERIALIZATION_FORMAT, |
| 50 |
cache_dir: str = Defaults.SESSION_FILE_DIR, |
| 51 |
threshold: int = Defaults.SESSION_FILE_THRESHOLD, |
| 52 |
mode: int = Defaults.SESSION_FILE_MODE, |
| 53 |
): |
| 54 |
|
| 55 |
# Deprecation warnings |
| 56 |
if cache_dir != Defaults.SESSION_FILE_DIR: |
| 57 |
warnings.warn( |
| 58 |
"'SESSION_FILE_DIR' is deprecated and will be removed in a future release. Instead pass FileSystemCache(directory, threshold, mode) instance as SESSION_CACHELIB.", |
| 59 |
DeprecationWarning, |
| 60 |
stacklevel=1, |
| 61 |
) |
| 62 |
if threshold != Defaults.SESSION_FILE_THRESHOLD: |
| 63 |
warnings.warn( |
| 64 |
"'SESSION_FILE_THRESHOLD' is deprecated and will be removed in a future release. Instead pass FileSystemCache(directory, threshold, mode) instance as SESSION_CLIENT.", |
| 65 |
DeprecationWarning, |
| 66 |
stacklevel=1, |
| 67 |
) |
| 68 |
if mode != Defaults.SESSION_FILE_MODE: |
| 69 |
warnings.warn( |
| 70 |
"'SESSION_FILE_MODE' is deprecated and will be removed in a future release. Instead pass FileSystemCache(directory, threshold, mode) instance as SESSION_CLIENT.", |
| 71 |
DeprecationWarning, |
| 72 |
stacklevel=1, |
| 73 |
) |
| 74 |
|
| 75 |
warnings.warn( |
| 76 |
"FileSystemSessionInterface is deprecated and will be removed in a future release. Instead use the CacheLib backend directly.", |
| 77 |
DeprecationWarning, |
| 78 |
stacklevel=1, |
| 79 |
) |
| 80 |
|
| 81 |
self.cache = FileSystemCache( |
| 82 |
cache_dir=cache_dir, threshold=threshold, mode=mode |
| 83 |
) |
| 84 |
|
| 85 |
super().__init__( |
| 86 |
app, key_prefix, use_signer, permanent, sid_length, serialization_format |
| 87 |
) |
| 88 |
|
| 89 |
def _retrieve_session_data(self, store_id: str) -> Optional[dict]: |
| 90 |
# Get the saved session (item) from the database |
| 91 |
return self.cache.get(store_id) |
| 92 |
|
| 93 |
def _delete_session(self, store_id: str) -> None: |
| 94 |
self.cache.delete(store_id) |
| 95 |
|
| 96 |
def _upsert_session( |
| 97 |
self, session_lifetime: TimeDelta, session: ServerSideSession, store_id: str |
| 98 |
) -> None: |
| 99 |
storage_time_to_live = total_seconds(session_lifetime) |
| 100 |
|
| 101 |
# Serialize the session data (or just cast into dictionary in this case) |
| 102 |
session_data = dict(session) |
| 103 |
|
| 104 |
# Update existing or create new session in the database |
| 105 |
self.cache.set( |
| 106 |
key=store_id, |
| 107 |
value=session_data, |
| 108 |
timeout=storage_time_to_live, |
| 109 |
) |