feat(redis): add native JSON session storage for 4.1.0
Build & Publish Package / publish (push) Successful in 49s
Build & Publish Package / publish (push) Successful in 49s
- Add opt-in SESSION_REDIS_JSON storage for asyncio Redis backends. - Check JSON command support at startup and reject JSON mode on Trio. - Read legacy strings and migrate sessions on their next save. - Validate JSON objects before transactional replacement and expiry. - Preserve tagged serialization for datetime, tuple, and other types. - Cover command contracts, error handling, and session round-trips. - Document server requirements, permissions, and migration limits. - Normalize indentation and whitespace across code, tests, and examples. - Bump the package and lockfile to 4.1.0 and update the changelog. - Refresh the locked pycparser dependency to 3.12.
This commit is contained in:
+131
-131
@@ -1,167 +1,167 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
quart_session
|
||||
~~~~~~~~~~~~~
|
||||
quart_session
|
||||
~~~~~~~~~~~~~
|
||||
|
||||
Adds server session support to your application.
|
||||
Adds server session support to your application.
|
||||
|
||||
:copyright: (c) 2014 by Shipeng Feng.
|
||||
:copyright: (c) 2020 by Kroket Ltd.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
:copyright: (c) 2014 by Shipeng Feng.
|
||||
:copyright: (c) 2020 by Kroket Ltd.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
import os
|
||||
from importlib.metadata import PackageNotFoundError, version as _pkg_version
|
||||
|
||||
try:
|
||||
__version__ = _pkg_version("quart-session")
|
||||
__version__ = _pkg_version("quart-session")
|
||||
except PackageNotFoundError: # running from a source tree without install
|
||||
__version__ = "unknown"
|
||||
__version__ = "unknown"
|
||||
|
||||
from quart import Quart
|
||||
|
||||
from .sessions import (
|
||||
RedisSessionInterface,
|
||||
RedisTrioSessionInterface,
|
||||
MemcachedSessionInterface,
|
||||
MongoDBSessionInterface,
|
||||
NullSessionInterface
|
||||
RedisSessionInterface,
|
||||
RedisTrioSessionInterface,
|
||||
MemcachedSessionInterface,
|
||||
MongoDBSessionInterface,
|
||||
NullSessionInterface
|
||||
)
|
||||
|
||||
|
||||
class Session(object):
|
||||
"""This class is used to add Server-side Session to one or more Quart
|
||||
applications.
|
||||
"""This class is used to add Server-side Session to one or more Quart
|
||||
applications.
|
||||
|
||||
There are two usage modes. One is initialize the instance with a very
|
||||
specific Quart application::
|
||||
There are two usage modes. One is initialize the instance with a very
|
||||
specific Quart application::
|
||||
|
||||
app = Quart(__name__)
|
||||
Session(app)
|
||||
app = Quart(__name__)
|
||||
Session(app)
|
||||
|
||||
The second possibility is to create the object once and configure the
|
||||
application later::
|
||||
The second possibility is to create the object once and configure the
|
||||
application later::
|
||||
|
||||
sess = Session()
|
||||
sess = Session()
|
||||
|
||||
def create_app():
|
||||
app = Quart(__name__)
|
||||
sess.init_app(app)
|
||||
return app
|
||||
def create_app():
|
||||
app = Quart(__name__)
|
||||
sess.init_app(app)
|
||||
return app
|
||||
|
||||
By default Quart-Session will use :class:`NullSessionInterface`, you
|
||||
really should configure your app to use a different SessionInterface.
|
||||
By default Quart-Session will use :class:`NullSessionInterface`, you
|
||||
really should configure your app to use a different SessionInterface.
|
||||
|
||||
.. note::
|
||||
.. note::
|
||||
|
||||
You can not use ``Session`` instance directly, what ``Session`` does
|
||||
is just change the :attr:`~quart.Quart.session_interface` attribute on
|
||||
your Quart applications.
|
||||
You can not use ``Session`` instance directly, what ``Session`` does
|
||||
is just change the :attr:`~quart.Quart.session_interface` attribute on
|
||||
your Quart applications.
|
||||
"""
|
||||
|
||||
def __init__(self, app: Quart = None) -> None:
|
||||
self._current_async_library = "asyncio"
|
||||
self.app = app
|
||||
if app is not None:
|
||||
self.init_app(app)
|
||||
|
||||
def init_app(self, app: Quart) -> None:
|
||||
"""This is used to set up session for your app object.
|
||||
|
||||
:param app: the Quart app object with proper configuration.
|
||||
"""
|
||||
try:
|
||||
import quart_trio
|
||||
if isinstance(app, quart_trio.QuartTrio):
|
||||
self._current_async_library = "trio"
|
||||
except ImportError:
|
||||
pass
|
||||
app.session_interface = self._get_interface(app)
|
||||
|
||||
def __init__(self, app: Quart = None) -> None:
|
||||
self._current_async_library = "asyncio"
|
||||
self.app = app
|
||||
if app is not None:
|
||||
self.init_app(app)
|
||||
@app.before_serving
|
||||
async def setup():
|
||||
await app.session_interface.create(app)
|
||||
|
||||
def init_app(self, app: Quart) -> None:
|
||||
"""This is used to set up session for your app object.
|
||||
def _get_interface(self, app: Quart):
|
||||
config = app.config.copy()
|
||||
config.setdefault('SESSION_TYPE', 'null')
|
||||
config.setdefault('SESSION_PERMANENT', True)
|
||||
config.setdefault('SESSION_USE_SIGNER', False)
|
||||
config.setdefault('SESSION_KEY_PREFIX', 'session:')
|
||||
config.setdefault('SESSION_PROTECTION', False)
|
||||
config.setdefault('SESSION_REVERSE_PROXY', False)
|
||||
config.setdefault('SESSION_STATIC_FILE', False)
|
||||
config.setdefault('SESSION_EXPLICIT', False)
|
||||
config.setdefault('SESSION_REDIS', None)
|
||||
config.setdefault('SESSION_REDIS_JSON', False)
|
||||
config.setdefault('SESSION_MEMCACHED', None)
|
||||
config.setdefault('SESSION_FILE_DIR',
|
||||
os.path.join(os.getcwd(), 'quart_session'))
|
||||
config.setdefault('SESSION_FILE_THRESHOLD', 500)
|
||||
config.setdefault('SESSION_FILE_MODE', 384)
|
||||
config = {k: v for k, v in config.items() if k.startswith('SESSION_')}
|
||||
|
||||
:param app: the Quart app object with proper configuration.
|
||||
"""
|
||||
try:
|
||||
import quart_trio
|
||||
if isinstance(app, quart_trio.QuartTrio):
|
||||
self._current_async_library = "trio"
|
||||
except ImportError:
|
||||
pass
|
||||
app.session_interface = self._get_interface(app)
|
||||
if isinstance(config.get("SESSION_HIJACK_PROTECTION"), bool):
|
||||
app.logger.warning("Deprecation: `SESSION_HIJACK_PROTECTION` "
|
||||
"has been renamed to `SESSION_PROTECTION`")
|
||||
|
||||
@app.before_serving
|
||||
async def setup():
|
||||
await app.session_interface.create(app)
|
||||
if isinstance(config.get("SESSION_HIJACK_REVERSE_PROXY"), str):
|
||||
app.logger.warning("Deprecation: `SESSION_HIJACK_REVERSE_PROXY` "
|
||||
"has been renamed to `SESSION_REVERSE_PROXY`")
|
||||
|
||||
def _get_interface(self, app: Quart):
|
||||
config = app.config.copy()
|
||||
config.setdefault('SESSION_TYPE', 'null')
|
||||
config.setdefault('SESSION_PERMANENT', True)
|
||||
config.setdefault('SESSION_USE_SIGNER', False)
|
||||
config.setdefault('SESSION_KEY_PREFIX', 'session:')
|
||||
config.setdefault('SESSION_PROTECTION', False)
|
||||
config.setdefault('SESSION_REVERSE_PROXY', False)
|
||||
config.setdefault('SESSION_STATIC_FILE', False)
|
||||
config.setdefault('SESSION_EXPLICIT', False)
|
||||
config.setdefault('SESSION_REDIS', None)
|
||||
config.setdefault('SESSION_MEMCACHED', None)
|
||||
config.setdefault('SESSION_FILE_DIR',
|
||||
os.path.join(os.getcwd(), 'quart_session'))
|
||||
config.setdefault('SESSION_FILE_THRESHOLD', 500)
|
||||
config.setdefault('SESSION_FILE_MODE', 384)
|
||||
config = {k: v for k, v in config.items() if k.startswith('SESSION_')}
|
||||
backend_warning = f"Please specify a session backend. " \
|
||||
f"Available interfaces: redis, redis+trio, " \
|
||||
f"memcached, null. e.g: app.config['SESSION_TYPE'] = 'redis'"
|
||||
|
||||
if isinstance(config.get("SESSION_HIJACK_PROTECTION"), bool):
|
||||
app.logger.warning("Deprecation: `SESSION_HIJACK_PROTECTION` "
|
||||
"has been renamed to `SESSION_PROTECTION`")
|
||||
if config['SESSION_TYPE'] == 'redis':
|
||||
options = {
|
||||
"redis": config['SESSION_REDIS'],
|
||||
"key_prefix": config['SESSION_KEY_PREFIX'],
|
||||
"use_signer": config['SESSION_USE_SIGNER'],
|
||||
"permanent": config['SESSION_PERMANENT'],
|
||||
**config
|
||||
}
|
||||
|
||||
if isinstance(config.get("SESSION_HIJACK_REVERSE_PROXY"), str):
|
||||
app.logger.warning("Deprecation: `SESSION_HIJACK_REVERSE_PROXY` "
|
||||
"has been renamed to `SESSION_REVERSE_PROXY`")
|
||||
if self._current_async_library == "asyncio":
|
||||
session_interface = RedisSessionInterface(**options)
|
||||
elif self._current_async_library == "trio":
|
||||
session_interface = RedisTrioSessionInterface(**options)
|
||||
else:
|
||||
raise NotImplementedError("Unknown eventloop")
|
||||
|
||||
backend_warning = f"Please specify a session backend. " \
|
||||
f"Available interfaces: redis, redis+trio, " \
|
||||
f"memcached, null. e.g: app.config['SESSION_TYPE'] = 'redis'"
|
||||
elif config['SESSION_TYPE'] == 'redis+trio':
|
||||
session_interface = RedisTrioSessionInterface(
|
||||
redis=config['SESSION_REDIS'],
|
||||
key_prefix=config['SESSION_KEY_PREFIX'],
|
||||
use_signer=config['SESSION_USE_SIGNER'],
|
||||
premanent=config['SESSION_PERMANENT'],
|
||||
**config
|
||||
)
|
||||
elif config['SESSION_TYPE'] == 'memcached':
|
||||
session_interface = MemcachedSessionInterface(
|
||||
memcached=config['SESSION_MEMCACHED'],
|
||||
key_prefix=config['SESSION_KEY_PREFIX'],
|
||||
use_signer=config['SESSION_USE_SIGNER'],
|
||||
permanent=config['SESSION_PERMANENT'],
|
||||
**config)
|
||||
elif config['SESSION_TYPE'] == 'mongodb':
|
||||
session_interface = MongoDBSessionInterface(
|
||||
mongodb_uri=config['SESSION_MONGODB_URI'],
|
||||
collection=config['SESSION_MONGODB_COLLECTION'],
|
||||
client_kwargs=config.get('SESSION_MONGODB_CLIENT_KWARGS', {}),
|
||||
set_callback=config.get('SESSION_MONGODB_SET_CALLBACK'),
|
||||
key_prefix=config['SESSION_KEY_PREFIX'],
|
||||
use_signer=config['SESSION_USE_SIGNER'],
|
||||
permanent=config['SESSION_PERMANENT'],
|
||||
**config)
|
||||
elif config['SESSION_TYPE'] == 'null':
|
||||
app.logger.warning(f"{backend_warning}. Currently using: null")
|
||||
session_interface = NullSessionInterface(
|
||||
key_prefix=config['SESSION_KEY_PREFIX'],
|
||||
use_signer=config['SESSION_USE_SIGNER'],
|
||||
permanent=config['SESSION_PERMANENT'],
|
||||
**config)
|
||||
else:
|
||||
raise NotImplementedError(f"No such session interface "
|
||||
f"\"{config['SESSION_TYPE']}\". {backend_warning}")
|
||||
|
||||
if config['SESSION_TYPE'] == 'redis':
|
||||
options = {
|
||||
"redis": config['SESSION_REDIS'],
|
||||
"key_prefix": config['SESSION_KEY_PREFIX'],
|
||||
"use_signer": config['SESSION_USE_SIGNER'],
|
||||
"permanent": config['SESSION_PERMANENT'],
|
||||
**config
|
||||
}
|
||||
|
||||
if self._current_async_library == "asyncio":
|
||||
session_interface = RedisSessionInterface(**options)
|
||||
elif self._current_async_library == "trio":
|
||||
session_interface = RedisTrioSessionInterface(**options)
|
||||
else:
|
||||
raise NotImplementedError("Unknown eventloop")
|
||||
|
||||
elif config['SESSION_TYPE'] == 'redis+trio':
|
||||
session_interface = RedisTrioSessionInterface(
|
||||
redis=config['SESSION_REDIS'],
|
||||
key_prefix=config['SESSION_KEY_PREFIX'],
|
||||
use_signer=config['SESSION_USE_SIGNER'],
|
||||
premanent=config['SESSION_PERMANENT'],
|
||||
**config
|
||||
)
|
||||
elif config['SESSION_TYPE'] == 'memcached':
|
||||
session_interface = MemcachedSessionInterface(
|
||||
memcached=config['SESSION_MEMCACHED'],
|
||||
key_prefix=config['SESSION_KEY_PREFIX'],
|
||||
use_signer=config['SESSION_USE_SIGNER'],
|
||||
permanent=config['SESSION_PERMANENT'],
|
||||
**config)
|
||||
elif config['SESSION_TYPE'] == 'mongodb':
|
||||
session_interface = MongoDBSessionInterface(
|
||||
mongodb_uri=config['SESSION_MONGODB_URI'],
|
||||
collection=config['SESSION_MONGODB_COLLECTION'],
|
||||
client_kwargs=config.get('SESSION_MONGODB_CLIENT_KWARGS', {}),
|
||||
set_callback=config.get('SESSION_MONGODB_SET_CALLBACK'),
|
||||
key_prefix=config['SESSION_KEY_PREFIX'],
|
||||
use_signer=config['SESSION_USE_SIGNER'],
|
||||
permanent=config['SESSION_PERMANENT'],
|
||||
**config)
|
||||
elif config['SESSION_TYPE'] == 'null':
|
||||
app.logger.warning(f"{backend_warning}. Currently using: null")
|
||||
session_interface = NullSessionInterface(
|
||||
key_prefix=config['SESSION_KEY_PREFIX'],
|
||||
use_signer=config['SESSION_USE_SIGNER'],
|
||||
permanent=config['SESSION_PERMANENT'],
|
||||
**config)
|
||||
else:
|
||||
raise NotImplementedError(f"No such session interface "
|
||||
f"\"{config['SESSION_TYPE']}\". {backend_warning}")
|
||||
|
||||
return session_interface
|
||||
return session_interface
|
||||
|
||||
Reference in New Issue
Block a user