ASGI-Sessions
asgi-sessions – Cookie-Based HTTP sessions for ASGI applications (Asyncio / Trio, / Curio)
Features
Supports base64 sessions
Supports JWT signed sessions
Supports Fernet encrypted sessions
Requirements
python >= 3.10
Installation
asgi-sessions should be installed using pip:
pip install asgi-sessions
To install optional JWT, Fernet support:
pip install asgi-sessions[jwt] pip install asgi-sessions[fernet]
Usage
Common ASGI applications:
from asgi_sessions import SessionMiddleware
async def my_app(scope, receive, send):
"""Read session and get the current user data from it or from request query."""
# The middleware puts a session into scope['session]
session = scope['session']
status, headers = 200, []
if scope['query_string']:
# Store any information inside the session
session['user'] = scope['query_string'].decode()
status, headers = 307, [(b"location", b"/")]
# Read a stored info from the session
user = (session.get('user') or 'anonymous').title().encode()
await send({"type": "http.response.start", "status": status, "headers": headers})
await send({"type": "http.response.body", "body": b"Hello %s" % user})
app = SessionMiddleware(my_app, session_type='jwt', secret_key="sessions-secret")
# http GET / -> Hello Anonymous
# http GET /?tom -> Hello Tom
# http GET / -> Hello Tom
As ASGI-Tools Internal middleware
from asgi_tools import App
from asgi_sessions import SessionMiddleware
app = App()
app.middleware(SessionMiddleware.setup(session_type='jwt', secret_key='SESSION-SECRET'))
@app.route('/')
async def index(request):
user = request.session.get('user', 'Anonymous')
return 'Hello %s' % user.title()
@app.route('/login/{user}')
async def login(request):
request.session['user'] = request.path_params.get('user', 'Anonymous')
return 'Done'
@app.route('/logout')
async def logout(request, *args):
del request.session['user']
return 'Done'
# http GET / -> Hello Anonymous
# http GET /login/tom -> Done
# http GET / -> Hello Tom
# http GET /logout -> Done
# http GET / -> Hello Anonymous
Options
from asgi_sessions import SessionMiddleware
app = SessionMiddleware(
# Your ASGI application
app,
# Session type (base64|jwt|fernet)
session_type="base64",
# Secret Key for the session (required for JWT/Fernet sessions)
secret_key=None,
# Cookie name to keep the session (optional)
cookie_name='session',
# Cookie max age (in seconds) (optional)
max_age=14 * 24 * 3600,
# Cookie samesite (optional) # Python 3.8+ only
samesite='lax',
# Cookie secure (https only) (optional)
secure=False,
)
Bug tracker
If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/asgi-sessions/issues
Contributing
Development of the project happens at: https://github.com/klen/asgi-sessions
License
Licensed under a MIT license.
Metadata
Release files for asgi-sessions 1.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| asgi_sessions-1.3.0.tar.gz | 6.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| asgi_sessions-1.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.5 kB
Release files / asgi_sessions-1.3.0.tar.gz
| Download URL | asgi_sessions-1.3.0.tar.gz |
|---|---|
| Size | 6.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e6072547e2477fadcb2e54b31c1e2edb2df1d6cf1c4ff9bfc3cc3b1a93fe6861
|
|
BLAKE2b-256 checksum How to use checksums |
abe12ea20ccd5f589e5b2228f3ee92c5f32a113c503fa30fc4f73f8a5d0c5016
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Release files / asgi_sessions-1.3.0-py3-none-any.whl
| Download URL | asgi_sessions-1.3.0-py3-none-any.whl |
|---|---|
| Size | 6.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d9b13b41c46078031a3e555e1af6c7c226a8d3410417754d3069dfa6a2c24e33
|
|
BLAKE2b-256 checksum How to use checksums |
ab8ffdc18f5832370b88741b3d8284d45ca6ec0bf49c1accd288a4e315670272
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|