Signed Cookie-Based HTTP sessions for ASGI applications
Project description
asgi-sessions – Signed Cookie-Based HTTP sessions for ASGI applications (Asyncio / Trio, / Curio)
Requirements
python >= 3.7
Installation
asgi-sessions should be installed using pip:
pip install asgi-sessions
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, 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(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,
# Secret Key for the session (required)
secret_key,
# 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
asgi-sessions-0.8.2.tar.gz
(5.5 kB
view details)
Built Distribution
File details
Details for the file asgi-sessions-0.8.2.tar.gz
.
File metadata
- Download URL: asgi-sessions-0.8.2.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8bc61616f8f86ccc700440c5d0bde39510a7888bf738a26cef87868f06d3c1f5 |
|
MD5 | e5f92e19c5e9259d2ed766f89f52c735 |
|
BLAKE2b-256 | e5738963f9a034953dd12cf62921f12ada6b9c2c58ea368ba26e6ce3d07d7ea2 |
File details
Details for the file asgi_sessions-0.8.2-py3-none-any.whl
.
File metadata
- Download URL: asgi_sessions-0.8.2-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1e000cc6b39c01de54e27a9d0a3396adb698bc48fb5ab0eb42db73b4a74ae710 |
|
MD5 | 0b9ce009a8cc868e260493828092ea71 |
|
BLAKE2b-256 | 18921141f9085ae1d949c2f1925f1b15e5666034b507d1f059a632ee4d213028 |